83 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-11-28 12:19:04 +01:00
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { IInput } from "../../Input/IInput";
import { UnitHealth } from "../UnitHealth";
import { UnitLevel } from "../UnitLevel";
2022-11-28 14:34:34 +01:00
import { PlayerRegeneration } from "./PlayerRegeneration";
2022-11-08 19:45:57 +01:00
import { PlayerUI } from "./PlayerUI/PlayerUI";
2022-11-28 12:19:04 +01:00
import { Weapon } from "./Weapon/Weapon";
2022-11-03 16:55:49 +01:00
const { ccclass, property } = _decorator;
@ccclass("Player")
export class Player extends Component {
@property(BoxCollider2D) private collider: BoxCollider2D;
2022-11-08 19:45:57 +01:00
@property(PlayerUI) private playerUI: PlayerUI;
2022-11-29 15:55:47 +01:00
@property(Weapon) private weapon: Weapon;
2022-11-03 16:55:49 +01:00
2022-11-14 16:35:47 +01:00
private input: IInput;
2022-11-08 19:45:57 +01:00
private health: UnitHealth;
2022-11-16 14:04:23 +01:00
private level: UnitLevel;
2022-11-28 14:34:34 +01:00
private regeneration: PlayerRegeneration;
2022-12-13 15:56:13 +01:00
private speed: number;
2022-11-03 16:55:49 +01:00
2022-12-13 15:56:13 +01:00
public init(input: IInput, data: PlayerData): void {
2022-11-14 16:35:47 +01:00
this.input = input;
2022-12-13 15:56:13 +01: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-12-13 15:56:13 +01:00
this.weapon.init(data.strikeDelay, data.damage);
2022-11-08 19:45:57 +01:00
this.playerUI.init(this.health);
}
public get Health(): UnitHealth {
return this.health;
}
2022-11-16 14:04:23 +01:00
public get Level(): UnitLevel {
return this.level;
2022-11-03 16:55:49 +01:00
}
2022-11-23 09:01:01 +01:00
public get Weapon(): Weapon {
return this.weapon;
}
2022-11-28 14:34:34 +01:00
public get Regeneration(): PlayerRegeneration {
return this.regeneration;
}
2022-11-16 14:04:23 +01:00
public get Collider(): Collider2D {
return this.collider;
2022-11-16 12:26:20 +01:00
}
2022-11-03 16:55:49 +01:00
public gameTick(deltaTime: number): void {
2022-11-14 16:35:47 +01:00
const movement: Vec2 = this.input.getAxis();
2022-11-03 16:55:49 +01:00
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);
2022-11-16 12:26:20 +01:00
this.weapon.gameTick(deltaTime);
2022-11-28 14:34:34 +01:00
this.regeneration.gameTick(deltaTime);
2022-11-03 16:55:49 +01:00
}
}
2022-12-13 11:58:40 +01:00
2022-12-13 15:56:13 +01: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-13 11:58:40 +01:00
}