2022-12-20 10:27:51 +00:00
|
|
|
import { Animation, Node, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator, Details, Sprite, Color } from "cc";
|
|
|
|
import { delay } from "../../../Services/Utils/AsyncUtils";
|
2022-11-28 11:19:04 +00:00
|
|
|
import { IInput } from "../../Input/IInput";
|
|
|
|
import { UnitHealth } from "../UnitHealth";
|
|
|
|
import { UnitLevel } from "../UnitLevel";
|
2022-12-23 10:36:12 +00:00
|
|
|
import { Magnet } from "./Magnet";
|
2022-11-28 13:34:34 +00:00
|
|
|
import { PlayerRegeneration } from "./PlayerRegeneration";
|
2022-11-08 18:45:57 +00:00
|
|
|
import { PlayerUI } from "./PlayerUI/PlayerUI";
|
2022-11-28 11:19:04 +00:00
|
|
|
import { Weapon } from "./Weapon/Weapon";
|
|
|
|
|
2022-11-03 15:55:49 +00:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
@ccclass("Player")
|
|
|
|
export class Player extends Component {
|
2022-11-08 10:42:14 +00:00
|
|
|
@property(BoxCollider2D) private collider: BoxCollider2D;
|
2022-11-08 18:45:57 +00:00
|
|
|
@property(PlayerUI) private playerUI: PlayerUI;
|
2022-11-29 14:55:47 +00:00
|
|
|
@property(Weapon) private weapon: Weapon;
|
2022-12-23 10:36:12 +00:00
|
|
|
@property(Magnet) private magnet: Magnet;
|
2022-12-16 13:18:02 +00:00
|
|
|
@property(Node) private playerGraphics: Node;
|
|
|
|
@property(Animation) private animation: Animation;
|
2022-12-20 10:27:51 +00:00
|
|
|
@property(Sprite) private sprite: Sprite;
|
2022-11-03 15:55:49 +00:00
|
|
|
|
2022-11-14 15:35:47 +00:00
|
|
|
private input: IInput;
|
2022-11-08 18:45:57 +00:00
|
|
|
private health: UnitHealth;
|
2022-11-16 13:04:23 +00:00
|
|
|
private level: UnitLevel;
|
2022-11-28 13:34:34 +00:00
|
|
|
private regeneration: PlayerRegeneration;
|
2022-12-13 14:56:13 +00:00
|
|
|
private speed: number;
|
2022-11-03 15:55:49 +00:00
|
|
|
|
2022-12-16 13:18:02 +00:00
|
|
|
private isMoveAnimationPlaying = false;
|
|
|
|
|
2022-12-13 14:56:13 +00:00
|
|
|
public init(input: IInput, data: PlayerData): void {
|
2022-11-14 15:35:47 +00:00
|
|
|
this.input = input;
|
2022-12-13 14:56:13 +00:00
|
|
|
this.health = new UnitHealth(data.maxHp);
|
|
|
|
this.level = new UnitLevel(data.requiredXP, data.xpMultiplier);
|
|
|
|
this.regeneration = new PlayerRegeneration(this.health, data.regenerationDelay);
|
|
|
|
this.speed = data.speed;
|
2022-11-08 10:42:14 +00:00
|
|
|
|
2022-12-13 14:56:13 +00:00
|
|
|
this.weapon.init(data.strikeDelay, data.damage);
|
2022-12-23 10:36:12 +00:00
|
|
|
this.magnet.init(data.magnetDuration);
|
2022-12-20 12:43:44 +00:00
|
|
|
this.health.HealthPointsChangeEvent.on(this.animateHpChange, this);
|
2022-11-08 18:45:57 +00:00
|
|
|
this.playerUI.init(this.health);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get Health(): UnitHealth {
|
|
|
|
return this.health;
|
2022-11-08 10:42:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 13:04:23 +00:00
|
|
|
public get Level(): UnitLevel {
|
|
|
|
return this.level;
|
2022-11-03 15:55:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 08:01:01 +00:00
|
|
|
public get Weapon(): Weapon {
|
|
|
|
return this.weapon;
|
|
|
|
}
|
|
|
|
|
2022-12-23 10:36:12 +00:00
|
|
|
public get Magnet(): Magnet {
|
|
|
|
return this.magnet;
|
|
|
|
}
|
|
|
|
|
2022-11-28 13:34:34 +00:00
|
|
|
public get Regeneration(): PlayerRegeneration {
|
|
|
|
return this.regeneration;
|
|
|
|
}
|
|
|
|
|
2022-11-16 13:04:23 +00:00
|
|
|
public get Collider(): Collider2D {
|
|
|
|
return this.collider;
|
2022-11-16 11:26:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 15:55:49 +00:00
|
|
|
public gameTick(deltaTime: number): void {
|
2022-12-16 13:54:50 +00:00
|
|
|
this.move(deltaTime);
|
|
|
|
this.weapon.gameTick(deltaTime);
|
2022-12-23 10:36:12 +00:00
|
|
|
this.magnet.gameTick(deltaTime);
|
2022-12-16 13:54:50 +00:00
|
|
|
this.regeneration.gameTick(deltaTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
private move(deltaTime: number): void {
|
2023-01-02 10:14:44 +00:00
|
|
|
if (!this.health.IsAlive) return;
|
|
|
|
|
2022-11-14 15:35:47 +00:00
|
|
|
const movement: Vec2 = this.input.getAxis();
|
2022-12-16 13:18:02 +00:00
|
|
|
if (!movement.equals(Vec2.ZERO)) {
|
|
|
|
movement.x *= deltaTime * this.speed;
|
|
|
|
movement.y *= deltaTime * this.speed;
|
|
|
|
|
|
|
|
const newPosition: Vec3 = this.node.worldPosition;
|
|
|
|
newPosition.x += movement.x;
|
|
|
|
newPosition.y += movement.y;
|
|
|
|
|
|
|
|
this.node.setWorldPosition(newPosition);
|
|
|
|
|
|
|
|
if (!this.isMoveAnimationPlaying) {
|
|
|
|
this.isMoveAnimationPlaying = true;
|
|
|
|
this.animation.play("Move");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (movement.x <= 0) {
|
|
|
|
this.playerGraphics.setScale(new Vec3(1, 1, 1));
|
|
|
|
} else {
|
|
|
|
this.playerGraphics.setScale(new Vec3(-1, 1, 1));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.isMoveAnimationPlaying) {
|
|
|
|
this.isMoveAnimationPlaying = false;
|
|
|
|
this.animation.play("Idle");
|
|
|
|
}
|
|
|
|
}
|
2022-11-03 15:55:49 +00:00
|
|
|
}
|
2022-12-20 10:27:51 +00:00
|
|
|
|
2022-12-20 12:43:44 +00:00
|
|
|
private async animateHpChange(hpChange: number): Promise<void> {
|
|
|
|
if (hpChange < 0) {
|
|
|
|
this.sprite.color = Color.RED;
|
|
|
|
} else {
|
|
|
|
this.sprite.color = Color.GREEN;
|
|
|
|
}
|
|
|
|
|
2022-12-20 10:27:51 +00:00
|
|
|
await delay(100);
|
|
|
|
this.sprite.color = Color.WHITE;
|
2023-01-02 10:14:44 +00:00
|
|
|
|
|
|
|
if (!this.health.IsAlive) {
|
|
|
|
this.animation.play("Die");
|
|
|
|
}
|
2022-12-20 10:27:51 +00:00
|
|
|
}
|
2022-11-03 15:55:49 +00:00
|
|
|
}
|
2022-12-13 10:58:40 +00:00
|
|
|
|
2022-12-13 14:56:13 +00:00
|
|
|
export class PlayerData {
|
|
|
|
public requiredXP: number[] = [];
|
|
|
|
public speed = 0;
|
|
|
|
public maxHp = 0;
|
|
|
|
public regenerationDelay = 0;
|
|
|
|
public xpMultiplier = 0;
|
|
|
|
public goldMultiplier = 0;
|
|
|
|
|
|
|
|
// Weapon
|
|
|
|
public strikeDelay = 0;
|
|
|
|
public damage = 0;
|
2022-12-23 10:36:12 +00:00
|
|
|
|
|
|
|
// Magnet
|
|
|
|
public magnetDuration = 0;
|
2022-12-13 10:58:40 +00:00
|
|
|
}
|