Add rotation and cardinal sprite usage
This commit is contained in:
parent
5a47ac00e8
commit
bef457b399
|
@ -19,6 +19,7 @@ class Entity {
|
|||
acceleration: number = 0.5
|
||||
maxSpeed: number = 4
|
||||
direction: number = 0
|
||||
turnRate: number = 10
|
||||
|
||||
constructor(ctor: string) {
|
||||
this.sprite = new SpriteInstance(ctor)
|
||||
|
@ -96,29 +97,69 @@ class PlayerEntity extends Entity {
|
|||
update(delta: number) {
|
||||
if (this.action) {
|
||||
// FIXME: Use physics.
|
||||
let shouldMove = false
|
||||
switch(this.action.type) {
|
||||
case 'west':
|
||||
if (this.velocity[0] > -this.maxSpeed) {
|
||||
this.velocity[0] -= this.acceleration
|
||||
if (this.direction !== 0) {
|
||||
if (this.direction < 180) {
|
||||
this.direction -= this.turnRate
|
||||
} else {
|
||||
this.direction += this.turnRate
|
||||
}
|
||||
}
|
||||
shouldMove = true
|
||||
break
|
||||
case 'east':
|
||||
if (this.velocity[0] < this.maxSpeed) {
|
||||
this.velocity[0] += this.acceleration
|
||||
if (this.direction !== 180) {
|
||||
if (this.direction < 180) {
|
||||
this.direction += this.turnRate
|
||||
} else {
|
||||
this.direction -= this.turnRate
|
||||
}
|
||||
}
|
||||
shouldMove = true
|
||||
break
|
||||
case 'north':
|
||||
if (this.velocity[1] > -this.maxSpeed) {
|
||||
this.velocity[1] -= this.acceleration
|
||||
if (this.direction !== 90) {
|
||||
if (this.direction < 90 || this.direction > 270) {
|
||||
this.direction += this.turnRate
|
||||
} else {
|
||||
this.direction -= this.turnRate
|
||||
}
|
||||
}
|
||||
shouldMove = true
|
||||
break
|
||||
case 'south':
|
||||
if (this.velocity[1] < this.maxSpeed) {
|
||||
this.velocity[1] += this.acceleration
|
||||
if (this.direction !== 270) {
|
||||
if (this.direction > 90 && this.direction < 270) {
|
||||
this.direction += this.turnRate
|
||||
} else {
|
||||
this.direction -= this.turnRate
|
||||
}
|
||||
}
|
||||
shouldMove = true
|
||||
break
|
||||
}
|
||||
if (this.direction > 360) {
|
||||
this.direction = 0
|
||||
} else if (this.direction < 0) {
|
||||
this.direction = 360
|
||||
}
|
||||
if (shouldMove) {
|
||||
let r = this.direction * (Math.PI/180)
|
||||
if (Math.abs(this.velocity[0]) < this.maxSpeed) {
|
||||
this.velocity[0] -= Math.cos(r) * this.acceleration
|
||||
}
|
||||
if (Math.abs(this.velocity[1]) < this.maxSpeed) {
|
||||
this.velocity[1] -= Math.sin(r) * this.acceleration
|
||||
}
|
||||
let cardinal = this.getCardinal()
|
||||
if (this.sprite.subsetKey !== cardinal) {
|
||||
this.sprite.setCtor(`${this.sprite.spriteKey}.${this.sprite.animationKey}.${this.sprite.setKey}.${cardinal}.${this.sprite.frameIndex}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
this.velocity[0] *= 0.5
|
||||
this.velocity[1] *= 0.5
|
||||
|
@ -126,6 +167,27 @@ class PlayerEntity extends Entity {
|
|||
// Eh... let's manually handle velocity
|
||||
this.body?.setLinearVelocity(planck.Vec2(this.velocity[0], this.velocity[1]))
|
||||
}
|
||||
getCardinal(): string {
|
||||
const degreesPerDirection = 360 / 8
|
||||
const angle = this.direction + degreesPerDirection / 2
|
||||
|
||||
if (angle >= 0 * degreesPerDirection && angle < 1 * degreesPerDirection) {
|
||||
return 'w'
|
||||
} else if (angle >= 1 * degreesPerDirection && angle < 2 * degreesPerDirection) {
|
||||
return 'nw'
|
||||
} else if (angle >= 2 * degreesPerDirection && angle < 3 * degreesPerDirection) {
|
||||
return 'n'
|
||||
} else if (angle >= 3 * degreesPerDirection && angle < 4 * degreesPerDirection) {
|
||||
return 'ne'
|
||||
} else if (angle >= 4 * degreesPerDirection && angle < 5 * degreesPerDirection) {
|
||||
return 'e'
|
||||
} else if (angle >= 5 * degreesPerDirection && angle < 6 * degreesPerDirection) {
|
||||
return 'se'
|
||||
} else if (angle >= 6 * degreesPerDirection && angle < 7 * degreesPerDirection) {
|
||||
return 's'
|
||||
}
|
||||
return 'sw'
|
||||
}
|
||||
}
|
||||
function isPlayerEntity(o: any): o is PlayerEntity {
|
||||
return o.act
|
||||
|
@ -187,7 +249,7 @@ export function GameState(ctx: ContextI): StateI {
|
|||
|
||||
// Add bogus entity
|
||||
//addEntity(new PlayerEntity('animals.deer.animal.west.0'), 0, 0)
|
||||
addEntity(new PlayerEntity('bogus-arrows.player.normal.e.0'), 0, 0)
|
||||
addEntity(new PlayerEntity('bogus-arrows.player.normal.w.0'), 0, 0)
|
||||
|
||||
ctx.app.stage.addChild(rootContainer)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user