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

#75


I got some answers to your questions:

2. If I make the z in the enemy process 1, then sometimes when I start the game up it will disappear but still register its active, so that means it's hiding behind the level somehow.




Making the process z = 1 will send it to the back. You should set it to -1 so it won't be hidden behind the scroll render process.




3. I noticed if I put a background image into tiled, it will not show up when the game is being played. Any way that I can set a background behind the tiles?


It's not supported yet, but seems like a good idea, I'll implement it soon and set a project on bitbucket so it's easier to keep updated.

I got internet again, so tomorrow I'll by able to take a look at the first issue by the afternoon. Thank you for waiting.
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

werg

JaViS


Hey, I know what's the problem


Quote from: DoctorN on April 21, 2013, 03:31:34 PM


1. I have an enemy process and a player process together. The scroller follows the player, but when you jump onto that little platform above where the enemy moves back and forth under and then jump, the enemy freaks out.




There is one thing you have to know. The map is being rendered every time you move the position of your player, but the part of the map being rendered is the part visible on the screen (with one or two more tiles but it's pretty much only what you can see). This is done this way for memory optimization and performance.


What is happening actually is that when the enemy is outside the visible part of the map, and he checks for where is the floor for knowing where to move, the floor isn't there anymore, because it's outside the viewport and is not being rendered, it's black, so surely what is happening is that the enemy is falling because it doesn't have floor to stand on.


What you can do is to use the <a href="http://wiki.bennugd.org/index.php?title=Region_out">region_out</a>  function. you can set a region of the size of the screen, or just to use the region number 0, and check in this way to know when the enemy is outside the viewport.


Once you know that, you can stop checking for the floor and wait until you are visible to start moving again.


Let me know if it makes sense
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

so do it like its done in the older games, where if its not on screen, itll go to its starting position? thanks
werg

JaViS

Yeah that's a possible solution.


What I would do is just to freeze the movement until the process is visible again:



loop
    if (!region_out(id,0))
        // do my stuff
    end
    frame;
end



But there is always the possibility  to check the hardness against an image of the full stage. I think you can use Tiled to export the hardness layer of your map as an image. Once you load it to your game you can make your enemy to use it and to move independently if it's visible or not.


Of course it's not optimized in what it comes to memory usage. A common practice is to re-size the hardness map image to the 50% of its original size, so it'll require less memory to be loaded.When you do that, you need to divide your x and y positions by 2 in order to get the right color from the hardness map.

Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

You are going to have to show me how to do it, because I tried this
//on-screen variables
        write(0,128,32,0,"EnemyHP: "); write_var(0,192,32,0,eneHP);
       
        //location on screen
        x+=inc_x;
        y+=inc_y;
       
        IF(!region_out(id,0))
            //gravity
            if(grav_time>0)
                grav_time--;
            else
                inc_y += gravity;
                grav_time = grav_timer;
            end
            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;
           
            //moving
            if(dir==1)
                inc_x = 2;
            else
                inc_x = -2;
            end

            for(i = 0; i <= (enemy_dim_x/2); i++)
                for(j = -(enemy_dim_y/4); j <= (enemy_dim_y/4); j++)
                    //right
                    collision_right=tilemap_get_pixel(map,0,x+i,y+j);
                    if(collision_right != RGB(255,0,0))
                        if(dir==1) inc_x = 2; end
                    else
                        if(dir==1) inc_x = 0; dir = -1; end
                    end
                    //left
                    collision_left=tilemap_get_pixel(map,0,x-(i+1),y+j);
                    if(collision_left != RGB(255,0,0))
                        if(dir==-1) inc_x = -2; end
                    else
                        if(dir==-1) inc_x = 0; dir = 1; end
                    end
                end
            end
           
            //floor
            for(i = 0; i <= (enemy_dim_x/2); i++)
                for(j = -(enemy_dim_y/4); j <= (enemy_dim_y/4); j++)
                    collision_bottom=tilemap_get_pixel(map,0,x+j,y+i);
                    if(collision_bottom == RGB(255,0,0))
                        // Went through the floor, let's fix the position
                        y--;
                        inc_y = 0;
                        if(status == "in air")
                            status = "on ground";
                        end
                    end
                end
            end
           
            //ceiling
            for(i = 0; i <= (enemy_dim_y/2); i++)
                for(j = -(enemy_dim_x/4); j <= (enemy_dim_x/4); j += (enemy_dim_x/4))
                    collision_top=tilemap_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
           
            //getting hit
            if(collision(type player_melee))
                if(!hit)
                    hit = true;
                    hit_time = 15;
                    say("Enemy has been hit");
                    eneHP--;
                end
            end
           
            if(hit_time>0) //after getting hit, the time between each time it can get hurt
                hit_time--;
            else
                hit = false; //can be hit again
            end
           
            //health management
            if(eneHP <= 0)
                signal(id,s_kill);
            end
        END
       
        //sprite section
            switch(dir)
           
            case -1:
                graph = enemy_l;
            end
            case  1:
                graph = enemy_r;
            end
           
            end

and the enemy process disappeared forever once it left the region, never to come back.
werg

JaViS

haha yeah that's because you should put


//location on screen[/size]        x+=inc_x;        y+=inc_y;


[/size]inside the if too[/size][/font]
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

#82
I think you need to fix that lol, I put
IF(!region_out(id,0))
            //location on screen
            x+=inc_x;
            y+=inc_y;

and it doesnt appear at all, it also makes the fps like 10 out of 60
werg

JaViS

Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

#84
the frame is outside the if, look at the code
process enemy(x,y);
private
    //status
    string status = "on ground";
    int dir = 1;
    byte eneHP = 10;
    bool hit = false; //hit means it has been attacked
    byte hit_time = 0; //the time before it can get hit again
   
    //location
    int inc_x = 0; // horizontal speed
    int inc_y = 0; // vertical speed
   
    //movement
    int gravity = 1;
        byte grav_time = 6;
        byte grav_timer = 6;
    int jump_height = -5;
    int max_vertical_speed = 6;
    int char_speed = 0;
    int char_max_speed = 2;
   
    //sprites
    int enemy_r, enemy_l;
   
    //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
z = -1;
ctype = c_scroll;
//upon startup it will either move left or right
dir = rand(0,1);
//load the sprites
enemy_r = load_png("enemy_r.png");
enemy_l = load_png("enemy_l.png");
    loop
        //on-screen variables
        write(0,128,32,0,"EnemyHP: "); write_var(0,192,32,0,eneHP);
       
        IF(!region_out(id,0))
            //location on screen
            x+=inc_x;
            y+=inc_y;
           
            //gravity
            if(grav_time>0)
                grav_time--;
            else
                inc_y += gravity;
                grav_time = grav_timer;
            end
            if(inc_y > max_vertical_speed)
                // Change the max_vertical_speed value if you want to jump higher
                inc_y = max_vertical_speed;
            end
           
            //moving
            if(dir==1)
                inc_x = 2;
            else
                inc_x = -2;
            end

            for(i = 0; i <= (enemy_dim_x/2); i++)
                for(j = -(enemy_dim_y/4); j <= (enemy_dim_y/4); j++)
                    //right
                    collision_right=tilemap_get_pixel(map,0,x+i,y+j);
                    if(collision_right != RGB(255,0,0))
                        if(dir==1) inc_x = 2; end
                    else
                        if(dir==1) inc_x = 0; dir = -1; end
                    end
                    //left
                    collision_left=tilemap_get_pixel(map,0,x-(i+1),y+j);
                    if(collision_left != RGB(255,0,0))
                        if(dir==-1) inc_x = -2; end
                    else
                        if(dir==-1) inc_x = 0; dir = 1; end
                    end
                end
            end
           
            //floor
            for(i = 0; i <= (enemy_dim_x/2); i++)
                for(j = -(enemy_dim_y/4); j <= (enemy_dim_y/4); j++)
                    collision_bottom=tilemap_get_pixel(map,0,x+j,y+i);
                    if(collision_bottom == RGB(255,0,0))
                        // Went through the floor, let's fix the position
                        y--;
                        inc_y = 0;
                        if(status == "in air")
                            status = "on ground";
                        end
                    end
                end
            end
           
            //ceiling
            for(i = 0; i <= (enemy_dim_y/2); i++)
                for(j = -(enemy_dim_x/4); j <= (enemy_dim_x/4); j += (enemy_dim_x/4))
                    collision_top=tilemap_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
           
            //getting hit
            if(collision(type player_melee))
                if(!hit)
                    hit = true;
                    hit_time = 15;
                    say("Enemy has been hit");
                    eneHP--;
                end
            end
           
            if(hit_time>0) //after getting hit, the time between each time it can get hurt
                hit_time--;
            else
                hit = false; //can be hit again
            end
           
            //health management
            if(eneHP <= 0)
                signal(id,s_kill);
            end
        END
       
        //sprite section
            switch(dir)
           
            case -1:
                graph = enemy_l;
            end
            case  1:
                graph = enemy_r;
            end
           
            end

    frame;end
    OnExit
    say("enemy process has been killed");
    unload_map(0,enemy_r);
    unload_map(0,enemy_l);
end

the IF and END capitalized are the ones that handle the region_out. Sometimes it works, sometimes it doesnt. Both times, the game will either just leave a black window, or doing this will require so much it makes the framerate about 20 out of 60. So i am confused here S:l
werg

JaViS

first thing I noted is that you put a write inside a loop


YOU SHOULDN'T!!


that's why your framerate fell to the ground!
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

i tried that and thats not the case, the framerate only goes down when the code
IF(!region_out(id,0))
is being used, if
//location on screen
            x+=inc_x;
            y+=inc_y;

is outside it, then it will not show up at all. when it is inside it, it will appear every once in awhile. both times, it will throttle the framerate. Now, when it does appear, say you climb to the top and then fall down, you will see the enemy process randomly falling if it is within view as well, which makes no sense because it was on the ground.

do you compile the code or just look at it? i am really stuck here, and i need your help. thanks
werg

JaViS

Quote from: DoctorN on May 13, 2013, 08:07:07 PM
i tried that and thats not the case, the framerate only goes down when the code
IF(!region_out(id,0))
is being used, if
//location on screen
            x+=inc_x;
            y+=inc_y;

is outside it, then it will not show up at all. when it is inside it, it will appear every once in awhile. both times, it will throttle the framerate. Now, when it does appear, say you climb to the top and then fall down, you will see the enemy process randomly falling if it is within view as well, which makes no sense because it was on the ground.

do you compile the code or just look at it? i am really stuck here, and i need your help. thanks


let me just take a look as soon as I get home today. region_out is not a slow function. Shouldn't be the problem
Working on Anarkade. A couch multiplayer 2D shooter.

MisterN

werg