A
AnimusRex
Guest
So, I have 8 directional movement on a grid. Here's what I have that governs the actual movement;
So whichever direction you're facing, up moves you in that direction.
For the collision code I have
Which is all well and good. Movement inside the grid works, using this plus my vertical collision code. The problem is as follows.
I want to make it so that if I (@) am against walls like this:
@
XXXXX
and I'm facing South East, for example, and I press forward, the character moves to the East instead.
Similarly for West, basically I want the character to be able to slide along walls rather than just return that there is a tile in the direction you're trying to go and that's it.
As well, with the code as it exists now, if I am like this:
@
XX
And I try to move to the South East, even though it's an open square, it returns a collision because of the square to the South.
I've been toying with this for a couple days but all my solutions are convoluted if statements in my switch for directions, and it turns into a mess way too fast. I'm sure there's a much cleaner way to do it, if anyone has any thoughts I'm all ears.
My collision code is as follows, just in case it matters;
Code:
//Setting up animation frames
facing = image_index;
//Code for turning on the spot
if (ismoving == false){
if (keyboard_check_pressed(vk_right))
{
image_index += 1;
}
if (keyboard_check_pressed(vk_left))
{
image_index -= 1;
}
}
if (keyboard_check(vk_up) and ismoving = false)
{
switch (facing)
{
case 0: //Moving North
{
ismoving = true;
movetimer = 32;
speedy = -movespeed;
speedx = 0;
break;
}
case 1: //Moving North East
{
ismoving = true;
movetimer = 32;
speedy = -movespeed;
speedx = movespeed;
break;
}
case 2: //Moving East
{
ismoving = true;
movetimer = 32;
speedy = 0;
speedx = movespeed;
break;
}
case 3: //Moving South-East
{
ismoving = true;
movetimer = 32;
speedy = movespeed;
speedx = movespeed;
break;
}
case 4: //Moving South
{
ismoving = true;
movetimer = 32;
speedy = movespeed;
speedx = 0;
break;
}
case 5: //Moving South-West
{
ismoving = true;
movetimer = 32;
speedy = movespeed;
speedx = -movespeed;
break;
}
case 6: //Moving West
{
ismoving = true;
movetimer = 32;
speedy = 0;
speedx = -movespeed;
break;
}
case 7: //Moving North-West
{
ismoving = true;
movetimer = 32;
speedy = -movespeed;
speedx = -movespeed;
break;
}
For the collision code I have
Code:
//Horizontal Collisions
if (grid_place_meeting(x+speedx, y))
{
while (!grid_place_meeting(x+sign(speedx), y))
{
x+= sign(speedx);
}
ismoving = false
speedx = 0;
}
I want to make it so that if I (@) am against walls like this:
@
XXXXX
and I'm facing South East, for example, and I press forward, the character moves to the East instead.
Similarly for West, basically I want the character to be able to slide along walls rather than just return that there is a tile in the direction you're trying to go and that's it.
As well, with the code as it exists now, if I am like this:
@
XX
And I try to move to the South East, even though it's an open square, it returns a collision because of the square to the South.
I've been toying with this for a couple days but all my solutions are convoluted if statements in my switch for directions, and it turns into a mess way too fast. I'm sure there's a much cleaner way to do it, if anyone has any thoughts I'm all ears.
My collision code is as follows, just in case it matters;
Code:
///Checking collisions
var xx = argument[0];
var yy = argument[1];
//Remember player position
var xp = x;
var yp = y;
//Update position for the mask calculations
x = xx;
y = yy;
//Check for an x meeting on the grid in either left or right according to the sprites bounding box
var x_meeting = (Level.grid[# bbox_right div CELL_WIDTH, bbox_top div CELL_HEIGHT] != FLOOR) ||
(Level.grid[# bbox_left div CELL_WIDTH, bbox_top div CELL_HEIGHT] != FLOOR);
//Check for a y meeting on the grid in either top or bottom according to the sprites bounding box
var y_meeting = (Level.grid[# bbox_right div CELL_WIDTH, bbox_bottom div CELL_HEIGHT] != FLOOR) ||
(Level.grid[# bbox_left div CELL_WIDTH, bbox_bottom div CELL_HEIGHT] != FLOOR);
//Move back if collided
x = xp;
y = yp;
//Return true or false
return x_meeting or y_meeting;