• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[ sorted out]pushing the enemy when attacking

RODO

Member
hello guys, I have a doubt about my Hitbox code.

I made a code for the player to create a square, and if that square hits an enemy it does damage and pushes. In short, the code does this (I'll put it down here for a better understanding):
GML:
//STEP EVENT

var lista_dano = ds_list_create();
// If it was the player who created me, I don't need to find him
if(!player_check)
{
    var col = instance_place_list(x,y,obj_entidade_inimigo,lista_dano,false);
}
else
{
    var col = instance_place_list(x,y,obj_player,lista_dano,false);
}

// perform the damage
if(col)
{   
    
    var tam = ds_list_size(lista_dano);
    for(var i = 0; i < tam; i++)
    {
        
        
        // here the code manages the damage
        var outro = lista_dano[| i];
        
        // sure it doesn't affect my creator
        if(outro != pai)
        {
            outro.hp -= dano;
        }
        
    }
}

// ensuring the list will not be kept in memory
ds_list_destroy(lista_dano);
instance_destroy();

well, I tried to use llengthdir_x () and lengthdir_y () to push it, but it didn't work. On the other hand, he takes damage and loses life which is a good thing but I would like my character's blows to push the enemy away, but apparently he doesn't do it and I don't really understand why. If anyone can explain it to me, I am very grateful
 

RODO

Member
You only appear to be setting the hp variable, not x and/or y. So it's not going to move.
but yes, I had already tried to put it in code like this:

GML:
// perform the damage
if(col)
{   
    
    var tam = ds_list_size(lista_dano);
    for(var i = 0; i < tam; i++)
    {
        
        
        // here the code manages the damage
        var outro = lista_dano[| i];
        
        // sure it doesn't affect my creator
        if(outro != pai)
        {
            outro.hp -= dano;
            outro.h_speed = lengthdir_x(3,ang)
            outro.v_speed = lengthdir_y(3,ang)
                            
        }
        
    }
}
however, even if I put it there it does nothing (NOTE: the hp code there is executed and works normally).
It's like I didn't put lengthdir_x and lengthdir_y
 

RODO

Member
Can we see the code you use for moving your player?
yes
GML:
var _range = point_distance(x,y,mouse_x,mouse_y);

up = keyboard_check(ord("W"))
down = keyboard_check(ord("S"))
right = keyboard_check(ord("D"))
left = keyboard_check(ord("A"))
v_ = (down - up) * vel;
h_ = (right - left) * vel;

var attack = mouse_check_button(mb_left);


if(h_speed != 0)
{
    image_xscale = sign(h_speed);   
}

switch(estado_player)
{
    case "parado":
    sprite_index  = spr_player_parado;
    h_speed = 0;
    v_speed = 0;
    
    if(v_ != 0 or h_ != 0)
    {
        estado_player = "mover";   
    }
    
    if(attack) && (_range < range_attack)
    {
        estado_player = "attack";   
    }
    
    break;
    
    case "mover":
    sprite_index = spr_player_andando;
    
    if(abs(v_) < 0.5 && abs(h_) < 0.5)
    {
        estado_player = "parado";   
    }
    
    
    if(attack) && (_range < range_attack)
    {
        
        estado_player = "attack";   
    }
    
    break;
    
    case "attack":
    var sprite_primeira = spr_player_attack;
    if(h_speed != 0 || v_speed != 0)
    {
        sprite_primeira = spr_player_andando_attack;

    }
    else if(h_speed == 0 || v_speed == 0)
    {
        sprite_primeira = spr_player_attack;
        
    }
     //verifica se o sprite mudou e zera o image_index
    if (ultima_sprite != sprite_index)
    {
         sprite_index = sprite_primeira;
         image_index = 0; //aqui reinicia o numero do frame do sprite
         ultima_sprite=sprite_index; //aqui deixa "ultimosprite" igual o sprite_index.
        
    }
    
    if(image_index == 2)
    {
        var hit = instance_create_layer(mouse_x,mouse_y,"player",obj_hitbox);
        hit.dano = 5;
        hit.pai = self;
        hit.image_xscale = 0.3;
        hit.image_yscale = 0.3;
        var hit_sprite = instance_create_layer(mouse_x,mouse_y,"player",obj_animation);
        hit_sprite.sprite_index = spr_hits_attack;
        hit_sprite.image_xscale = 0.3;
        hit_sprite.image_yscale = 0.3;
    }
    
    if(image_index >= image_number - 1)
    {
        estado_player = "mover";
    }
    break;
    
}
h_speed = lerp(h_speed,h_,acel);
v_speed = lerp(v_speed,v_,acel);
this is my enemy's movement code, which I want to push using the hitbox code

Code:
ar persegue = collision_circle(x,y,area_persegue,obj_entidade_player,false,true);

if(h_speed != 0)
{
    image_xscale = sign(h_speed);   
}



switch(estado)
{
    case "parado":
    h_speed = 0;
    v_speed = 0;
    
    sprite_index = spr_inimigo_pedra_viva_parado;
    
    if(persegue)
    {
        estado = "atacando";   
    }
    break;
    
    case "atacando":
    
    if(instance_exists(obj_entidade_player))
    {
        dir =  point_direction(x,y,obj_entidade_player.x,obj_entidade_player.y);
        if(!collision_line(x,y,obj_entidade_player.x,obj_entidade_player.y,obj_solido_parede,false,false))
        {
            h_speed = lengthdir_x(vel, dir);
            v_speed = lengthdir_y(vel, dir);
            sprite_index = spr_inimigo_pedra_viva_andando;
        }
    }
    else
    {
        estado = "parado";
    }
    
    if(!persegue)
    {
        estado = "parado";   
    }
    
    break;

}

if(hp <= 0)
{
    instance_destroy();   
}


I hope you can understand, because I write all my code in Portuguese (a part of it
)
 

Nidoking

Member
I understand it all. I also understand that you are not changing x and y anywhere in what you posted. You have created h_speed and v_speed variables that you use to determine your sprite index and state, but neither of them has any impact on x or y. If any of the instances in your game are moving, then you still haven't shown the code that makes that happen.
 

RODO

Member
I understand it all. I also understand that you are not changing x and y anywhere in what you posted. You have created h_speed and v_speed variables that you use to determine your sprite index and state, but neither of them has any impact on x or y. If any of the instances in your game are moving, then you still haven't shown the code that makes that happen.
Hum understood, I do this application in the collision, which the collision is checked in an end step. Could that be the reason?
GML:
//colisão com a aparede
if(place_meeting(x+h_speed,y,obj_solido_parede))
{
    while(!place_meeting(x+sign(h_speed),y,obj_solido_parede))
    {
        x = x + sign(h_speed);   
    }
    h_speed = 0;
}
x += h_speed;

if(place_meeting(x,y+v_speed,obj_solido_parede))
{
    while(!place_meeting(x,y+sign(v_speed),obj_solido_parede))
    {
        y = y + sign(v_speed);   
    }
    v_speed = 0;
}
y += v_speed;
 

Nidoking

Member
There we go. The problem is probably that your enemy is resetting its state, and therefore its h_speed and v_speed, every step. Since you're using a state machine, you should have a "knockback" state where it's being knocked back. That's how state machines work.
 

RODO

Member
There we go. The problem is probably that your enemy is resetting its state, and therefore its h_speed and v_speed, every step. Since you're using a state machine, you should have a "knockback" state where it's being knocked back. That's how state machines work.
hum now I understand, I was trying to make this code more compact because I didn't want to do it in a state, since other enemies would have to have this same code, but at least I know how I can make the code now. Thank you very much
 
Top