DelayNoMore/frontend/assets/scripts/BasePlayer.js

97 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-09-20 15:50:01 +00:00
module.export = cc.Class({
extends: cc.Component,
properties: {
animComp: {
type: cc.Animation,
default: null,
},
lastMovedAt: {
type: cc.Float,
default: 0 // In "GMT milliseconds"
}
2022-09-20 15:50:01 +00:00
},
// LIFE-CYCLE CALLBACKS:
start() {
const self = this;
self.activeDirection = {
dx: 0,
dy: 0
};
},
onLoad() {
const self = this;
self.clips = {
'02': 'Top',
'0-2': 'Bottom',
2022-09-20 15:50:01 +00:00
'-20': 'Left',
'20': 'Right',
'-11': 'TopLeft',
'11': 'TopRight',
'-1-1': 'BottomLeft',
'1-1': 'BottomRight'
2022-09-20 15:50:01 +00:00
};
const canvasNode = self.mapNode.parent;
self.mapIns = self.mapNode.getComponent("Map");
2022-09-20 15:50:01 +00:00
const joystickInputControllerScriptIns = canvasNode.getComponent("TouchEventsManager");
self.ctrl = joystickInputControllerScriptIns;
self.animComp = self.node.getComponent(cc.Animation);
self.animComp.play();
},
scheduleNewDirection(newScheduledDirection, forceAnimSwitch) {
if (!newScheduledDirection) {
return;
}
if (forceAnimSwitch || null == this.activeDirection || (newScheduledDirection.dx != this.activeDirection.dx || newScheduledDirection.dy != this.activeDirection.dy)) {
this.activeDirection = newScheduledDirection;
this.activeDirection = newScheduledDirection;
2022-09-20 15:50:01 +00:00
const clipKey = newScheduledDirection.dx.toString() + newScheduledDirection.dy.toString();
const clips = (this.attacked ? this.attackedClips : this.clips);
2022-09-20 15:50:01 +00:00
let clip = clips[clipKey];
if (!clip) {
// Keep playing the current anim.
if (0 !== newScheduledDirection.dx || 0 !== newScheduledDirection.dy) {
cc.warn('Clip for clipKey === ' + clipKey + ' is invalid: ' + clip + '.');
}
} else {
this.animComp.play(clip);
if (this.attacked) {
cc.log(`Attacked, switching to play clipKey = ${clipKey}, clip == ${clip}, this.activeDirection == ${JSON.stringify(this.activeDirection)}, this.activeDirection == ${JSON.stringify(this.activeDirection)}.`);
2022-09-20 15:50:01 +00:00
}
}
}
},
update(dt) {},
2022-09-20 15:50:01 +00:00
lateUpdate(dt) {},
2022-09-20 15:50:01 +00:00
_generateRandomDirection() {
return ALL_DISCRETE_DIRECTIONS_CLOCKWISE[Math.floor(Math.random() * ALL_DISCRETE_DIRECTIONS_CLOCKWISE.length)];
},
updateSpeed(proposedSpeed) {
if (0 == proposedSpeed && 0 < this.speed) {
this.startFrozenDisplay();
}
2022-09-20 15:50:01 +00:00
if (0 < proposedSpeed && 0 == this.speed) {
this.stopFrozenDisplay();
}
this.speed = proposedSpeed;
2022-09-20 15:50:01 +00:00
},
startFrozenDisplay() {
const self = this;
2022-09-20 15:50:01 +00:00
self.attacked = true;
},
stopFrozenDisplay() {
const self = this;
self.attacked = false;
},
});