Hello everyone, so i managed to kind of sort of get my code to do what i wanted it to do, i'm trying to build a top down adventure game a la link to the past but i'm not sure if the way i'm doing this is the right way to go about it, would love for somebody more experience with the engine to give me some tips about my code or how it could be better written or point me towards a tutorial or documentation that could help me out, Thanks in advance :) i really appreciate it.
Sup.ArcadePhysics2D.setGravity(0, 0);
class PlayerBehavior extends Sup.Behavior {
//stores the idle state to transition to when player stops pressing a key
public currentIdle: string;
//trying a different way to go about managing the animation to play
public currentAnimation : string;
//How fast the player can go
public maxSpeed = 0.15;
//The acceleration rate
public acceleration = 0.007;
//How fast he decelerate
public decelaration = 0.0085;
//Current Velocity
private _velocity:Sup.Math.Vector2;
awake() {
this.currentIdle = "Idle_Dwn";
this.currentAnimation = "Idle_Dwn";
this.actor.spriteRenderer.setAnimation(this.currentIdle);
this._velocity = this.actor.arcadeBody2D.getVelocity();
}
update() {
Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.ArcadePhysics2D.getAllBodies());
// Sup.log(`Actor ${this.acceleration}`);
if (Sup.Input.isKeyDown("LEFT"))
{
// Move the current behavior's actor by a small negative offset on the X axis
//Trying to implement inertia
this._velocity.x = Sup.Math.clamp(this._velocity.x-this.acceleration,-this.maxSpeed,0);
//this.actor.spriteRenderer.setAnimation("Walk_Left");
this.currentAnimation = "Walk_Left";
this.currentIdle = "Idle_Left";
}
if (Sup.Input.isKeyDown("RIGHT"))
{
// Same but positive to go to the right
this._velocity.x = this._velocity.x = Sup.Math.clamp(this._velocity.x+this.acceleration,0, this.maxSpeed);
//this.actor.spriteRenderer.setAnimation("Walk_Right");
this.currentAnimation = "Walk_Right";
this.currentIdle = "Idle_Right";
}
if (Sup.Input.isKeyDown("UP"))
{
// Move the current behavior's actor by a small negative offset on the X axis
this._velocity.y = Sup.Math.clamp(this._velocity.y+this.acceleration,0, this.maxSpeed);
//this.actor.spriteRenderer.setAnimation("Walk_Up");
this.currentAnimation = "Walk_Up";
this.currentIdle = "Idle_Up";
}
if (Sup.Input.isKeyDown("DOWN"))
{
// Move the current behavior's actor by a small negative offset on the X axis
this._velocity.y = Sup.Math.clamp(this._velocity.y-this.acceleration,-this.maxSpeed,0);
//this.actor.spriteRenderer.setAnimation("Walk_Dwn");
this.currentAnimation = "Walk_Dwn";
this.currentIdle = "Idle_Dwn";
}
if(!Sup.Input.isKeyDown("UP") && !Sup.Input.isKeyDown("DOWN") && !Sup.Input.isKeyDown("LEFT") && !Sup.Input.isKeyDown("RIGHT"))
{
//De-accelerate depending on the directtion of the speed vector
if(this._velocity.x > 0)
{
this._velocity.x = Sup.Math.clamp(this._velocity.x-this.decelaration,0,this.maxSpeed);
}
else if(this._velocity.x < 0)
{
this._velocity.x = Sup.Math.clamp(this._velocity.x+this.decelaration,-this.maxSpeed,0);
}
if(this._velocity.y > 0)
{
this._velocity.y = Sup.Math.clamp(this._velocity.y-this.decelaration,0,this.maxSpeed);
}
else if(this._velocity.y < 0)
{
this._velocity.y = Sup.Math.clamp(this._velocity.y+this.decelaration,-this.maxSpeed,0);
}
//If we have achieved complete standstill, pay the idel animation
else if(this._velocity.x === 0 && this._velocity.y === 0)
{
this.actor.spriteRenderer.setAnimation(this.currentIdle);
}
//Testing for better de-aceleration code
//if(Sup.Input.wasKeyJustReleased)
//velocity.y = Sup.Math.clamp(Math.abs(velocity.y)-this._decelaration,0,this._maxSpeed);
//velocity.x = Sup.Math.clamp(Math.abs(velocity.x)-this._decelaration,0,this._maxSpeed);
}
this.actor.spriteRenderer.setAnimation(this.currentAnimation);
//this.actor.arcadeBody2D.setVelocityX(velocity.x);
this.actor.arcadeBody2D.setVelocity(this._velocity);
}
}
Sup.registerBehavior(PlayerBehavior);