Tiled parser library with Cardiac Runner as example

Started by JaViS, January 30, 2013, 02:35:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

JaViS

I'm sorry guys that I couldn't advance with the example. Last friday a thief broke into my house and tied up my girlfriend.


I've been busy the whole weekend trying to be with her the most I could.


I'll try to do an example tody for you
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

I am sorry to hear that  :(

Well I hope you can get it done soon  :)
werg

handsource-dyko

That's a horrible story. :o Is she allright? It sounds pretty shocking. Just the thought that someone sneaks into your house and hurts your loved one is pretty devistating.

JaViS

Quote from: handsource-dyko on March 04, 2013, 03:33:18 PM
That's a horrible story. :o Is she allright? It sounds pretty shocking. Just the thought that someone sneaks into your house and hurts your loved one is pretty devistating.


yeah it was horrible. fortunately he didin't youch her and she is fine. obviously it was shoking for her and she is still afraid, now she want us to move to another place :P
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

werg

JaViS

Hey sorry during the week I can't advance anything, I'm working on it now. thanks for waiting
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

werg

JaViS




Finaly, here it's!


First thing you need to know, this is not a basic example of how to use the library, and this is not an example made from your code.


This is, what I believe, what you are having problems with, and need to understand before coding a character or a player on our library.


This is an hardness map example. This contains the basic logic of a player character movement over a hardness map. Once you understand this you should be able to start coding your character using our tiled library.


I can't think on a better, or a simpler example than this one. So please, put all the time you need to study and understand the logic. I think it'll be very useful for your future projects.

Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

werg

MisterN

#54
I just straight ported the code into my player.prg and i just fall through the floor, i analyze it and i try to fix things cause the movement is so similar to what you have in cardiac, but nothing is colliding. So I reverted it back to a straight port for you to look at. main.prg should be the same so just replace the player.rpg with this one. I did change char_height to player_dim_y and char_width to player_dim_x. Thanks
process player(x,y);
public
    string status = "standing";
private
    //location
    int inc_x = 0; // horizontal speed
    int inc_y = 0; // vertical speed
    int y_prev = 0; //for collision
    //key presses
    bool key_right = false;
    bool key_left = false;
    bool key_jump = false;
    bool pressed_jump = false;
    //movement
    byte gravity = 1;
    byte jump_height = -12;
    byte max_vertical_speed = 2;
    byte char_speed = 0;
    byte char_max_speed = 2;
    //status
    int dir = 0;
    //collision
    int collision_bottom = 0; // down collision
    int collision_top = 0; // up collision
    int collision_left = 0; // left collision
    int collision_right = 0; // right collision
    int i = 0;
    int j = 0;
begin
graph = load_png("player.png");
ctype = c_scroll;
    loop
        write(0,0,0,0,"STATS:");
        write(0,0,8,0,"dir: "); write_var(0,56,8,0,dir);
        write(0,0,32,0,"status: "); write_string(0,56,32,0,&status);

        // Gravity
        inc_y += gravity;
        if(inc_y > max_vertical_speed)
            // Change the max_vertical_speed value if you want to jump higher
            inc_y = max_vertical_speed;
        end
        y_prev = y;
        y += inc_y;
        x += inc_x;

        // Collision checking for all directions

        // BOTTOM
        for(i = 0; i <= player_dim_y; i++)
            for(j = -(player_dim_y/2); j <= (player_dim_y/2); j++)
                collision_bottom=map_get_pixel(map,map,x+j,y+i);
                if(collision_bottom == RGB(255,0,0) or (collision_bottom == RGB(0,255,0) and (y+i-player_dim_y>y_prev) and inc_y >= 0))
                    // Went through the floor, let's fix the position
                    y--;
                    inc_y = 0;
                    if(status == "jumping")
                        // Fix for slopes, change the 3 to something higher if you have problems jumping on slopes
                        status = "standing";
                    end
                end
            end
        end
        // TOP
        for(i = 0; i <= player_dim_y; i++)
            for(j = -(player_dim_y/2); j <= (player_dim_y/2); j++)
                collision_top=map_get_pixel(map,map,x+j,y-i);
                if(collision_top == RGB(255,0,0))
                    y++;
                    if(inc_y < 0)
                        inc_y *= -1;
                    end
                end
            end
        end
        // LEFT
        for(i = 0; i <= player_dim_x; i++)
            for(j = -(player_dim_x/2); j <= (player_dim_x/2); j++)
                collision_left=map_get_pixel(map,map,x-i,y+j);
                if(collision_left == RGB(255,0,0))
                    x++;
                    if(inc_x < 0)
                        inc_x = 0;
                    end
                end
            end
        end
        // RIGHT
        for(i = 0; i <= player_dim_x; i++)
            for(j = -(player_dim_x/2); j <= (player_dim_x/2); j++)
                collision_right=map_get_pixel(map,map,x+i,y+j);
                if(collision_right == RGB(255,0,0))
                    x--;
                    if(inc_x > 0)
                        inc_x = 0;
                    end
                end
            end
        end
        // End collision checking

        if(!key(_K) and (status == "standing" or status == "running") and inc_y == 0)
            // Lets the player jump if it's standing
            pressed_jump = 0;
            inc_y++;
        elseif(key(_K) and inc_y > 5)
            // Avoids jumping until the jump button is released after a jump
            pressed_jump = 1;
        elseif(!key(_K) and (status == "standing" or status == "running") and inc_y > 3)
            // Jumping status if you're falling
            status = "jumping";
        end

        if(!key(_K) and status == "jumping")
            // Going down, the player released the jump button
            inc_y++;
        end
       
        // Status engine
        switch(status):
            case "running":
                if(key(_K) and status != "jumping" and pressed_jump == 0 and inc_y <= 0)
                    // The player can jump only if: it's not already jumping, the button has been released from the previous jump, and it's standing on the floor
                    status = "jumping";
                   
                    pressed_jump = 1;
                    inc_y = jump_height;
                   
                elseif(!key(_D) and !key(_A))
                    // Not pressing any key, the player is standing
                    status = "standing";
                   
                   
                else
                    // Player is running
                    char_speed = char_max_speed;
                    if(key(_D))
                        if(inc_x < char_speed)
                            inc_x++;
                        end
                        flags = 0;
                    elseif(key(_A))
                        flags = 1;
                        if(inc_x > (char_speed*(-1)))
                            inc_x--;
                        end
                    end
                   
                end
            end
            case "standing":
                if(key(_K) and status != "jumping" and pressed_jump == 0 and inc_y <= 0)
                    // The player can jump only if: it's not already jumping, the button has been released from the previous jump, and it's standing on the floor
                    status = "jumping";
                   
                    pressed_jump = 1;
                    inc_y = jump_height;
                    if(inc_x > 0)
                        inc_x--;
                    elseif(inc_x < 0)
                        inc_x++;
                    end
                elseif(key(_D) or key(_A))
                    // Player is running
                    status = "running";
                   
                else
                    if(inc_x > 0)
                        // A little inertia if the player has been running
                        inc_x--;
                       
                    elseif(inc_x < 0)
                        inc_x++;
                       
                   
                    end
                end
            end
            case "jumping":
                if(key(_D) or key(_A))
                    // Player is moving in the air
                    char_speed = char_max_speed;
                    if(key(_D))
                        if(inc_x < char_speed)
                            inc_x++;
                        end
                        flags = 0;
                    elseif(key(_A))
                        flags = 1;
                        if(inc_x > (char_speed*(-1)))
                            inc_x--;
                        end
                    end
                end
                if(collision_bottom == RGB(255,0,0) )                   
                    // Player hit the floor
                    inc_y = 0;                   
                    status = "standing";
                end;
               
            end
        end

    frame;end
end


UPDATE:
this is the code ive tweaked around with. only colliding into the walls will work now.
process player(x,y);
public
    string status = "standing";
private
    //location
    int inc_x = 0; // horizontal speed
    int inc_y = 0; // vertical speed
    int y_prev = 0; //for collision
    //key presses
    bool key_right = false;
    bool key_left = false;
    bool key_jump = false;
    bool pressed_jump = false;
    //movement
    int gravity = 1;
    int jump_height = -11;
    int max_vertical_speed = 12;
    int char_speed = 0;
    int char_max_speed = 2;
    //status
    int dir = 0;
    //collision
    int collision_bottom = 0; // down collision
    int collision_top = 0; // up collision
    int collision_left = 0; // left collision
    int collision_right = 0; // right collision
    int i = 0;
    int j = 0;
begin
graph = load_png("player.png");
ctype = c_scroll;
    loop
        write(0,0,0,0,"STATS:");
        write(0,0,8,0,"dir: "); write_var(0,56,8,0,dir);
        write(0,0,32,0,"status: "); write_string(0,56,32,0,&status);
       
        x+=inc_x;
        y+=inc_y;
       
        inc_y += gravity;
        if(inc_y > max_vertical_speed)
            // Change the max_vertical_speed value if you want to jump higher
            inc_y = max_vertical_speed;
        end
       
        y_prev = y;

        if(key(_right) and !key(_left))
            key_right=true;
        else
            key_right=false;
        end
        if(key(_left) and !key(_right))
            key_left=true;
        else
            key_left=false;
        end
       
        if(key(_control))
            key_jump = true;
        else
            key_jump = false;
        end
       
        //moving
        for(i = -(player_dim_y/2)-1; i <= (player_dim_y/2)+1; i += (player_dim_y/2))
            //right
            collision_right=tilemap_get_pixel(map,0,x+(player_dim_x/2),y+i);
            if(collision_right != RGB(255,0,0))
                if(key_right) collision_left=0; inc_x = 2; end
            else
                if(key_right) collision_left=0; inc_x = 0; end
            end
            //left
            collision_left=tilemap_get_pixel(map,0,x-(player_dim_x/2)-2,y+i);
            if(collision_left != RGB(255,0,0))
                if(key_left) collision_right=0; inc_x = -2; end
            else
                if(key_left) collision_right=0; inc_x = 0; end
            end
        end
        //if both keys are pressed (or none), inc_x will be 0 as well as the collisions
        if((!key_right and !key_left) or (key_right and key_left))
            inc_x = 0;
            collision_right=0;
            collision_left=0;
        end
       
        //jumping
       
        //falling
        for(i = -(player_dim_y/2); i <= 0; i++)
            for(j = -(player_dim_x/2)-1; j <= (player_dim_x/2)+1; j += (player_dim_x/2))
                collision_bottom=map_get_pixel(map,0,x+j,y+i);
                if(collision_bottom == RGB(255,0,0))
                    y--;
                    inc_y = 0;
                end
            end
        end
       
        //ceiling
        for(i = 0; i <= player_dim_y; i++)
            for(j = -(player_dim_x/2)-1; j <= (player_dim_x/2)+1; j += (player_dim_x/2))
                collision_top=map_get_pixel(map,0,x+j,y-i);
                if(collision_top == RGB(255,0,0))
                    y++;
                    if(inc_y < 0)
                        inc_y *= -1;
                    end
                end
            end
        end

    frame;end
end


ill upload my level too. and heres main if you need it
//Velocity And Movement Tester
//By: Nicholas Wolf
//Date: 1/29/2013
program velocity_test;

import "mod_grproc";
import "mod_wm"
import "mod_key"
import "mod_proc"
import "mod_video"
import "mod_text"
import "mod_map"
import "mod_draw"
import "mod_debug"
import "mod_say";
import "mod_screen";
import "mod_math";
import "mod_time";
import "mod_dir";
import "mod_joy";
import "mod_sound";

include "libraries/tiledmap/tiledmap.lib";

global
//the name we will give our level
_tiled_map map; // the map we'll load
_tiledmap_viewport viewport; //a scroller window

//the dimmensions of objects
player_dim_x = 32;
player_dim_y = 32;

//the object of the player
character_process player_s;

private
//The resolution of the game
game_width = 320;
game_height = 240;
game_color_depth = 16;
//The resolution of the screen
screen_width = 960;
screen_height = 720;

begin
//get the size of the monitor you are using
//get_desktop_size(&screen_width, &screen_height);
//say("Monitor is: " + screen_width + "x" + screen_height);
//set the games resolution
scale_resolution = screen_width * 10000 + screen_height ;
scale_resolution_aspectratio = SRA_PRESERVE;
//full_screen = true;
set_mode(game_width,game_height,game_color_depth);
set_fps(60,0);

render_level();

    loop
        //the scroller
        viewport.x = player_s.x - (game_width/2);
        viewport.y = player_s.y-160;
        //press esc or click X to exit game
        if(key(_esc) or exit_status)
            exit("goodbye",0);
        end
        write(0,0,230,0,"FPS: ");write_int(0,30,230,0,&fps);
    frame;end
   
    tiledmap_stop(viewport);
    tilemap_unload_map(map);
end

include "player.prg"

process render_level();
public
int i,j;
begin
//load the tiled map file
    tilemap_load_map(map,"test.tmx");
    tilemap_start(viewport, map, 320, 240);
//spawn the objects in the tiled map file
    for (i=0; i< map.objectgroup_count ; i++)
        for (j=0; j< map.objectgroups[i].objectcount ; j++)
            switch(map.objectgroups[i].objects[j].typename)
                case "player":
                    player_s = player(map.objectgroups[i].objects[j].x+(player_dim_x/2),map.objectgroups[i].objects[j].y+(player_dim_y/2));
                end
            end
        end
    end
end


other than that it uses the libraries from cardiac. ALSO to note, if I put a background into tiled, it doesnt load up in the game.
werg

MisterN

#55
you think you can take a look? when i say(collision_bottom), all that returns is -1.

EDIT:
changed it to tilemap_get_pixel and it returns values, so I will work on the code again tomorrow
werg

JaViS

Cool! I was about to suggest that. Sorry for the late reply, I always can work on it just on fridays
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

Thanks! I got some stuff working, im splitting up my engine into different segments so I can them join them up later. Right now I am working on enemy AI, and I noticed that if I kill the process, I get this error

I was trying to make a way so if I pressed like key F5 in main, it would reload everything. I need your help in this because I need to know how to unload the tilemap and reload it. basically F5 would just bring up the render_level(); process and the process would check if the enemy process existed (and would then remove it) as well as check if tilemap!=0 (but i don't know how to unload it). thanks
werg

JaViS

hehe there is a function to stop the tiled map and unload everything. it's very important because it free the memmory allocated when creating maps. I don't recall the name of the function right now, but it's pretty clear in the example (cardiac runner) please take a look there.


thanks!
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

its tile_map_unload. your program has let_me_alone(); in the main and that didnt work for me, nor do i think it will work later on once my game gets more beefy. for some reason killing that process crashes the game. can you take a look? thanks
werg