Skip to content

Commit 5005e30

Browse files
author
obiot
committed
Added the possibility to limit the entity velocity to a specified value (through the setMaxVelocity function).
It's however only possible for now to define a cap value for the y velocity, for the x velocity some code refractor is needed first !
1 parent 63fadaf commit 5005e30

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/entity/entity.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,9 @@
829829

830830
// default speed
831831
this.accel = new me.Vector2d();
832+
833+
// max velocity to be applied on entity movement
834+
this.maxVel = new me.Vector2d(1000,1000);
832835

833836
// some default contants
834837
this.gravity = 0.98;
@@ -928,6 +931,19 @@
928931
this.accel.x = (x != 0) ? x : this.accel.x;
929932
this.accel.y = (x != 0) ? y : this.accel.y;
930933
},
934+
935+
936+
/**
937+
* cap the entity velocity to the specified value<br>
938+
* (!) this will only cap the y velocity for now(!)
939+
* @param {Int} x max velocity on x axis
940+
* @param {Int} y max velocity on y axis
941+
* @protected
942+
*/
943+
setMaxVelocity : function(x, y) {
944+
this.maxVel.x = x;
945+
this.maxVel.y = y;
946+
},
931947

932948
/**
933949
* make the player move left of right
@@ -1088,6 +1104,12 @@
10881104
this.falling = true;
10891105
this.vel.y += (this.gravity * me.timer.tick);
10901106
}
1107+
1108+
// cap the y velocity
1109+
if (this.vel.y !=0)
1110+
{
1111+
this.vel.y = this.vel.y.clamp(-this.maxVel.y,this.maxVel.y);
1112+
}
10911113

10921114
// check for collision
10931115
collision = this.collisionMap.checkCollision(

0 commit comments

Comments
 (0)