yashamatt
Member
So I have been fiddling with this for a while now, trying to adapt my spaghetti code to allow oneway platforms that are also slopes.
I am quite new to coding in general but I do have slopes and oneway platforms working properly in my side-scrolling platformer,
however, my oneway platforms that are also slopes still tend to bug with either it not registering properly when being fallen upon
or walked onto by an object (falls through). I am using object-based collision (opposed to tile-based - mainly because I didn't know
about tile-based collision when I started the project, also there won't be too many active objects at any one time in my small game).
Any tips or ideas on how to approach the problem or even just helping my code be better here will be much appreciated! Below is
the current workingmess code that I am using without oneway slopes implemented.
My code is mainly an amalgamation of my own crap with hints from zack bell's 2014 blog and whatever I could shovel up in the forums here.
par_solid and o_oneway_up are separate children of par_collision.
ok here is the actual collision stuff
I am quite new to coding in general but I do have slopes and oneway platforms working properly in my side-scrolling platformer,
however, my oneway platforms that are also slopes still tend to bug with either it not registering properly when being fallen upon
or walked onto by an object (falls through). I am using object-based collision (opposed to tile-based - mainly because I didn't know
about tile-based collision when I started the project, also there won't be too many active objects at any one time in my small game).
Any tips or ideas on how to approach the problem or even just helping my code be better here will be much appreciated! Below is
the current working
My code is mainly an amalgamation of my own crap with hints from zack bell's 2014 blog and whatever I could shovel up in the forums here.
par_solid and o_oneway_up are separate children of par_collision.
GML:
/**************************************************************
* player STEP event
* the state code is actually in a large switch statement with scripts for each
* state but i just copied out what i thought might be relavant
**************************************************************/
if state != player.falling
{
var underblock = instance_place(x,y+1,o_oneway);
if !place_meeting(x,y+1,par_solid)
{
if instance_exists(underblock)
{
if bbox_bottom > underblock.bbox_top
{
state = player.falling;
exit;
}
}
else if !instance_exists(underblock)
{
state = player.falling;
exit;
}
}
}
if state == player.falling
{
var _semi_solid = instance_place(x,bbox_bottom+1,o_oneway_up)
if (place_meeting(x,bbox_bottom+1,par_solid))
or (instance_exists(_semi_solid) and _semi_solid.bbox_top>bbox_bottom)
and ySpeed > 0
{
state = player.idle;
exit;
}
}
ok here is the actual collision stuff
GML:
//Horizontal and up slopes
var y_gradient = 0;
while (place_meeting(x+xSpeed, y-y_gradient, par_solid) and y_gradient <= abs(1*xSpeed))
y_gradient += 1;
if place_meeting(x+xSpeed, y-y_gradient, par_solid)
{
while (!place_meeting(x+sign(xSpeed), y, par_solid)) x+=sign(xSpeed);
xSpeed = 0;
}
else
{
y -= y_gradient;
}
x += xSpeed;
// Downward slopes
if !place_meeting(x,y,par_solid) and ySpeed >= 0 and place_meeting(x, y+2+abs(xSpeed), par_solid)
{
while(!place_meeting(x,y+1,par_collision))
{
y += 1;
}
}
//vertical
repeat (abs(ySpeed))
{
var underblock = instance_place(x,y+1,o_oneway_up);
if ((place_meeting(x, y + sign(ySpeed), o_oneway_up) and ySpeed > 0
and !place_meeting(x, y, o_oneway_up) and instance_exists(underblock)
and underblock.bbox_top>bbox_bottom))
{
ySpeed = 0;
break;
}
else if !place_meeting(x, y + sign(ySpeed), par_solid)
{
y += sign(ySpeed);
}
else
{
ySpeed = 0;
break;
}
}