render optimization

This commit is contained in:
sli97
2022-12-06 23:10:03 +08:00
parent 1b215bfbb4
commit b7c95c5ca9
6 changed files with 32 additions and 20 deletions

View File

@@ -55,6 +55,9 @@ export class ActorManager extends EntityManager implements IActor {
weapon.setParent(this.node)
this.weapon = weapon.addComponent(WeaponManager)
this.weapon.init(data)
this.targetPos = undefined
this.node.active = false
}
tick(dt: number) {
@@ -88,33 +91,32 @@ export class ActorManager extends EntityManager implements IActor {
}
renderPosition(data: IActor) {
// this.node.setPosition(data.position.x, data.position.y)
const newPos = new Vec3(data.position.x, data.position.y)
if (!this.targetPos) {
this.node.active = true
this.node.setPosition(newPos)
this.targetPos = new Vec3()
this.targetPos.set(newPos)
this.targetPos = new Vec3(newPos)
} else if (!this.targetPos.equals(newPos)) {
this.tw?.stop()
this.node.setPosition(this.targetPos)
this.targetPos.set(newPos)
this.state = EntityStateEnum.Run
this.tw = tween(this.node).to(0.1, {
position: this.targetPos
}).call(() => {
this.state = EntityStateEnum.Idle
}).start()
}
// this.node.setPosition(data.position.x, data.position.y)
}
renderDirection(data: IActor) {
if (data.direction.x === 0 && data.direction.y === 0) {
this.state = EntityStateEnum.Idle
return
}
this.state = EntityStateEnum.Run
const { x, y } = data.direction
if (x !== 0) {
this.node.setScale(x > 0 ? 1 : -1, 1)
this.label.node.setScale(x > 0 ? 1 : -1, 1)
this.hpBar.node.setScale(x > 0 ? 1 : -1, 1)
}
const side = Math.sqrt(x * x + y * y)
const angle = rad2Angle(Math.asin(y / side))