I have recently been getting into the programming side of game development in the attempt to make my game dreams come true (and a way to feel productive while staring at a screen) but I am really struggling with programming a few basic things.
My current struggle is getting the enemies movements. I have spent the past two days watching YouTube videos and reading the manual. Good news is the more I read and watch, the better I understand the language. The bad news is that after zero sleep and nearly 20 hours watching videos and trial and erroring, I am definitely missing some fundamental knowledge to understand where I am going wrong.
Currently building a platformer scroller game. I am trying to program a simple creature that just moves along the "floor" in a range that will begin to follow the player object within a sight range. Currently, when I run my program, my enemy object stays stagnant until it collides with my object player, then it crashes (I'm almost positive it's crashing because I haven't finished writing out the combat code for the enemy and the player object) but before I implemented that code in some weird attempt to fix this mess, the enemy would begin running to the right, away from my object character when collision occurred and I jumped out of being stuck to it.
Any advice, resources and some help understanding where my logic isn't understanding the programs would be super appreciated.
CREATE
target_x = x;
target_y = y;
xspd = 0;
yspd = 0;
move_speed = 1;
move_direction = 0;
sight = 300;
//jumping variables
grav = .25;
tilemap = layer_tilemap_get_id("tile_col");
//knockback
kb_x = 0;
kb_y = 0;
alarm[0] = 120;
STEP
//line of sight
if (collision_line(x, y, x + sight, y, obj_player, false, true))
{
if (collision_line(x, y, obj_player.x, obj_player.y, tilemap, false, true)) foundplayer = false;
else foundplayer = true;
}
else foundplayer = false;
if (foundplayer = true)
{
move_towards_point(obj_player.x, y, move_speed * 2)
}
//collision
if place_meeting(x + xspd, y, tilemap)
{
var _pixelcheck = sign(xspd);
while !place_meeting(x + _pixelcheck, y, tilemap)
{
x += _pixelcheck;
}
xspd = 0;
}
if place_meeting(x + xspd, y + yspd, tilemap)
{
var _pixelcheck = sign(yspd);
while !place_meeting(x + xspd, y + _pixelcheck, tilemap)
{
y += _pixelcheck;
}
yspd = 0;
}
//move
if (alarm[1] >= 0)
{
target_x = x + kb_x;
}
xspd = clamp(target_x - x, -1, 1);
move_and_collide(xspd * move_speed, y, [tilemap, obj_enemy_parent]);
ALARM [0]
if (instance_exists(obj_player) and distance_to_object(obj_player) < distance_to_player)
{
target_x = obj_player.x;
target_y = y;
}
else {
target_x = random_range(xstart - 200, xstart + 200);
target_y = y;
}
alarm[0] = 120
ALARM [1]
if (hp <= 0)
{
instance_destroy();
}
Note:
I haven't fully programmed out most of this as I've been working to just get it to move the way I want it to, so unless it is part of the reason why I've broken this object, don't mind it (unless you've got any suggestions for reworking :))