Grid Movement Code

I’ve been considering using grid based player movement in my next game so today i built a cool prototype game with the goal of perfecting grid based movement and I think I’ve got it.  I’d offer to walk you through it, but it’s rather self explanatory thanks to my borderline excessive use of comments.  Also, this code kinda teleports the player to the target location rather then walks them there, I’ll need to add walking rather then teleporting later as it may require using/creating a new event.

The following is the step event code:

//if left click
if mouse_check_button_released(mb_left){
//and mouse is over self
if x<mouse_x and mouse_x<x+32 and y<mouse_y and mouse_y<y+32{
//set selected to true
selected = true}
//if selected and clicked, move to target
else if selected{
if mouse_check_button_released(mb_left){
if not collision_rectangle(target_x,target_y,target_x+31,target_y+31,solid,false,true){
x = target_x
y = target_y
selected = false}}}}

//if right mouse button then deselect
if mouse_check_button_pressed(mb_right){
selected = false}

//find target_x
if selected and distance_to_point(mouse_x,mouse_y)<move_dist{
target_x = floor(mouse_x/32)*32
target_y = floor(mouse_y/32)*32}

And heres my draw event code:

draw_self()
//draw location to move to
if selected = true{
draw_set_color(c_green)
draw_circle(x+16,y+16,move_dist,true)
draw_set_alpha(0.1)
draw_circle(x+16,y+16,move_dist,false)
draw_set_alpha(1)
draw_set_color(c_black)
draw_rectangle(x,y,x+32,y+32,true)
draw_sprite(sprite_index,0,target_x,target_y)}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s