I finally got crouching working for my player entity. However I'm trying to figure out how to make it so that the player entity stays in its crouched state if the user lets go of the crouch key when the entity is in a space that's too low for its standing state.
For example: Player presses S to crawl in tunnel -> Player is inside tunnel and unpresses S.
Currently: this causes the player to stand up and appear above the roof of the tunnel.
What it needs to do: remain crouched until the player enters a space that is high enough, then stand up.
I'm guessing I'll have to use .handleMovementTrace for this, but I can't figure out how exactly to detect if a tile is too low to fit the player.
Here's the current collision code:
crouch: 35,
crouched:false,
In update function:
if (ig.input.state('crouch') && this.standing) {
this.size.y = this.crouch;
this.pos.y = this.pos.y + this.crouch * -0.0001;
this.crouched = true;
this.maxVel.x = 200;
}
else if (this.crouched && !ig.input.state('crouch') && this.standing) {
this.size.y=60;
this.pos.y = this.pos.y - this.crouch;
this.maxVel.x = 300;
this.crouched = false;
}
if ( this.crouched && this.vel.y >= 0 && this.vel.x==0) {
this.currentAnim = this.anims.crouchidle;
}
else if ( this.crouched && this.vel.y >=0 && this.vel.x !=0 ) {
this.currentAnim = this.anims.crouchrun;
}
Thanks in advance!
Edit: Eh..sorry about the weird code indentation, things seem to get misaligned when I try to post code in here.
1 decade ago
by Arantor
Easiest way I can think of it to have an entity sat at both ends of the tunnel that trigger the state change for your player's entity. Yes, it makes level generation slightly more time consuming but it would be the easiest way without having to go through an examine the collisionMap manually for each movement (over and above what's normally done anyway)
So if I'm understanding what you mean:
Have a trigger entity just inside the tunnel (so that it triggers after the user has to first manually crouch to enter) which checks against the player and sets crouched to true
Have this trigger entity set crouch to false if the player touches it a second time (somehow?) and place another instance of it at the end of the tunnel.
Is this correct, or am I way off here?
Crap. This level has a lot of tunnels >.<
1 decade ago
by Arantor
Pretty much, yes. It depends more on exactly how fussy you want to be about the player crouching or not, really.
I was personally thinking of something less fussy, whereby I'd just have the entity check collision for entity-vs-player and player going the direction out of the tunnel, and if so, uncrouch the player.
The issue here might be that there is no set direction of the tunnel. I used tunnels as example as it's the best label I could give it, but they're more like low overhangs of different lengths where the player goes in and out of small 'chambers' to pick things up and to climb higher up the level. So the player will likely be moving through these in both directions several times.
or you could make a custom entity update() for your crouching dude. in line 74 of entity.js the update() method calls ig.game.collisionMap.trace(...) to check for collisions. What you could do is have an additional trace() called if the person is crouching to check above him.
var res = ig.game.collisionMap.trace(
this.pos.x, this.pos.y, mx, my, this.size.x, this.size.y //regular collision check
);
this.handleMovementTrace(res);
//crouching check below:
if (this.crouching){
var res2 = ig.game.collisionMap.trace(
this.pos.x, this.pos.y + (this.StandingSize.y - this.CrouchingSize.y), mx, my, this.size.x, this.size.y
);
if (res2.collision.y){
//insert code so the dude doesn't stand up
}
}
I think. or something.
SlouchCouch, I'm trying very hard to get my head around this and got to the stage of adding your code into entity.js. Right now it triggers a test string in the console each time the character crouches (regardless of whether it's under a low tile or not), but I have no idea where to go from there.
if (this.crouched){
var res2 = ig.game.collisionMap.trace(
this.pos.x, this.pos.y + (60 - 35), mx, my, this.size.x, this.size.y
);
if (res2.collision.y){
console.log( 'res2 test' );
}
}
oh my bad, i got my y axis flipped. you probably want:
this.pos.y - (60 - 35)
instead of plus. also note that I haven't tested this code yet.
I am working on pretty much the exact same thing today and I'll let you know if i got it to work or not.
you could also do a getTile() call on the collision map:
if (this.crouching && ig.game.collisionMap.getTile(player.pos.x,
player.pos.y - (player.StandingSize.y - player.CrouchingSize.y) { //evaluates non-zero if a collision tile is there. 0 if no tile is present.
stay_crouched_or_whatever();
}
1 decade ago
by Xander
I would use getTile. Consider the spike.js example entity that flips directions if it gets too close to an edge:
update: function() {
// near an edge? return!
if( !ig.game.collisionMap.getTile(
this.pos.x + (this.flip ? +4 : this.size.x -4),
this.pos.y + this.size.y+1
)
) {
this.flip = !this.flip;
}
var xdir = this.flip ? -1 : 1;
this.vel.x = this.speed * xdir;
this.parent();
},
In this case it checks the direction that spike is heading and tests a distance in advance of where it is and then increases the y coordinate by 1 (to see if it is contacting the ground once moved over in the direction it is traveling). If this proposed shift leaves spike in a state of non-collision, then we know their is an edge, so flip the direction.
So as SlouchCrouch recommended, check to see if there is a collision using the coordinate that would represent the top of the characters head while standing and see if that collides with a tile. If it does, prevent the standing up action.
Also, since you are using a "hold-down button" crouch system, you will need this same check repeated each update once "s" has been released since once you walk out of the tunnel, you will want to character to stand up immediately after he exits (since s is no longer being pressed).
Page 1 of 1
« first
« previous
next ›
last »