mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 08:36:14 +00:00
Folder structure
This commit is contained in:
62
assets/Scripts/Game/Unit/Player/Player.ts
Normal file
62
assets/Scripts/Game/Unit/Player/Player.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
|
||||
import { IInput } from "../../Input/IInput";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
import { UnitLevel } from "../UnitLevel";
|
||||
import { PlayerUI } from "./PlayerUI/PlayerUI";
|
||||
import { Weapon } from "./Weapon/Weapon";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Player")
|
||||
export class Player extends Component {
|
||||
@property private speed = 0;
|
||||
@property(BoxCollider2D) private collider: BoxCollider2D;
|
||||
@property(PlayerUI) private playerUI: PlayerUI;
|
||||
|
||||
private input: IInput;
|
||||
private weapon: Weapon;
|
||||
private health: UnitHealth;
|
||||
private level: UnitLevel;
|
||||
|
||||
public init(input: IInput, weapon: Weapon, maxHp: number, requiredLevelXps: number[]): void {
|
||||
this.input = input;
|
||||
this.weapon = weapon;
|
||||
this.health = new UnitHealth(maxHp);
|
||||
this.level = new UnitLevel(requiredLevelXps);
|
||||
|
||||
this.weapon.node.parent = this.node;
|
||||
this.weapon.node.setPosition(new Vec3());
|
||||
|
||||
this.playerUI.init(this.health);
|
||||
}
|
||||
|
||||
public get Health(): UnitHealth {
|
||||
return this.health;
|
||||
}
|
||||
|
||||
public get Level(): UnitLevel {
|
||||
return this.level;
|
||||
}
|
||||
|
||||
public get Weapon(): Weapon {
|
||||
return this.weapon;
|
||||
}
|
||||
|
||||
public get Collider(): Collider2D {
|
||||
return this.collider;
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
const movement: Vec2 = this.input.getAxis();
|
||||
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);
|
||||
|
||||
this.weapon.gameTick(deltaTime);
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Player/Player.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Player/Player.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "b570abb8-1bcd-4ef0-85e8-4d783531001d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Game/Unit/Player/PlayerUI.meta
Normal file
12
assets/Scripts/Game/Unit/Player/PlayerUI.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "7eadb488-192c-4370-9e59-20f27929fba1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
19
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerHealthUI.ts
Normal file
19
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerHealthUI.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Component, ProgressBar, _decorator } from "cc";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("PlayerHealthUI")
|
||||
export class PlayerHealthUI extends Component {
|
||||
@property(ProgressBar) public healthBar: ProgressBar;
|
||||
private health: UnitHealth;
|
||||
|
||||
public init(health: UnitHealth): void {
|
||||
this.healthBar.progress = 1;
|
||||
this.health = health;
|
||||
this.health.HealthPointsChangeEvent.on(this.updateHealthBar, this);
|
||||
}
|
||||
|
||||
private updateHealthBar(): void {
|
||||
this.healthBar.progress = this.health.HealthPoints / this.health.MaxHealthPoints;
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "def45aaf-cc88-4ec6-97ff-4d5b0beb015b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
13
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerUI.ts
Normal file
13
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerUI.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Component, _decorator } from "cc";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
import { PlayerHealthUI } from "./PlayerHealthUI";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("PlayerUI")
|
||||
export class PlayerUI extends Component {
|
||||
@property(PlayerHealthUI) private healthUI: PlayerHealthUI;
|
||||
|
||||
public init(playerHealth: UnitHealth): void {
|
||||
this.healthUI.init(playerHealth);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "6247f0de-45c1-4e74-89aa-388c217ce3a3",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Game/Unit/Player/Weapon.meta
Normal file
12
assets/Scripts/Game/Unit/Player/Weapon.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "b232195c-46e2-4508-9f71-5efb42ab490f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
43
assets/Scripts/Game/Unit/Player/Weapon/UpgradableCollider.ts
Normal file
43
assets/Scripts/Game/Unit/Player/Weapon/UpgradableCollider.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { BoxCollider2D, Collider2D, Component, Contact2DType, _decorator } from "cc";
|
||||
import { ISignal } from "../../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../../Services/EventSystem/Signal";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("UpgradableCollider")
|
||||
export class UpgradableCollider extends Component {
|
||||
@property(BoxCollider2D) private colliders: BoxCollider2D[] = [];
|
||||
private contactBeginEvent: Signal<Collider2D> = new Signal<Collider2D>();
|
||||
private currentUpgradeLevel = 0;
|
||||
|
||||
public init(): void {
|
||||
this.setUpgradeLevel();
|
||||
|
||||
for (const collider of this.colliders) {
|
||||
collider.on(Contact2DType.BEGIN_CONTACT, this.onColliderContactBegin, this);
|
||||
}
|
||||
}
|
||||
|
||||
public get ContactBeginEvent(): ISignal<Collider2D> {
|
||||
return this.contactBeginEvent;
|
||||
}
|
||||
|
||||
public upgrade(): void {
|
||||
if (this.currentUpgradeLevel == this.colliders.length - 1) throw new Error("Already at max upgrade! " + this.currentUpgradeLevel);
|
||||
|
||||
this.currentUpgradeLevel++;
|
||||
this.setUpgradeLevel();
|
||||
}
|
||||
|
||||
private setUpgradeLevel(): void {
|
||||
for (const collider of this.colliders) {
|
||||
collider.node.active = false;
|
||||
}
|
||||
|
||||
this.colliders[this.currentUpgradeLevel].node.active = true;
|
||||
}
|
||||
|
||||
private onColliderContactBegin(thisCollider: Collider2D, otherCollider: Collider2D): void {
|
||||
this.contactBeginEvent.trigger(otherCollider);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bba49b00-3fe5-4042-85f3-7d1f017d75d3",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
59
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts
Normal file
59
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Animation, AnimationState, Component, _decorator } from "cc";
|
||||
import { GameTimer } from "../../../../Services/GameTimer";
|
||||
import { WeaponSettings } from "../../../Data/GameSettings";
|
||||
|
||||
import { UpgradableCollider } from "./UpgradableCollider";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Weapon")
|
||||
export class Weapon extends Component {
|
||||
@property(Animation) private weaponAnimation: Animation;
|
||||
@property(UpgradableCollider) private upgradableCollider: UpgradableCollider;
|
||||
|
||||
private strikeTimer: GameTimer;
|
||||
private strikeState: AnimationState;
|
||||
private damage: number;
|
||||
|
||||
public init(settings: WeaponSettings): void {
|
||||
this.strikeTimer = new GameTimer(settings.strikeDelay);
|
||||
this.damage = settings.damage;
|
||||
this.node.active = false;
|
||||
|
||||
this.weaponAnimation.on(Animation.EventType.FINISHED, this.endStrike, this);
|
||||
this.strikeState = this.weaponAnimation.getState(this.weaponAnimation.clips[0].name);
|
||||
this.strikeState.speed = 1;
|
||||
|
||||
this.upgradableCollider.init();
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.strikeTimer.gameTick(deltaTime);
|
||||
if (this.strikeTimer.tryFinishPeriod()) {
|
||||
this.strike();
|
||||
}
|
||||
}
|
||||
|
||||
public get Collider(): UpgradableCollider {
|
||||
return this.upgradableCollider;
|
||||
}
|
||||
|
||||
public get Damage(): number {
|
||||
return this.damage;
|
||||
}
|
||||
|
||||
public upgradeWeaponDamage(): void {
|
||||
this.damage++;
|
||||
}
|
||||
public upgradeWeaponLength(): void {
|
||||
this.upgradableCollider.upgrade();
|
||||
}
|
||||
|
||||
private strike(): void {
|
||||
this.node.active = true;
|
||||
this.weaponAnimation.play(this.strikeState.name);
|
||||
}
|
||||
|
||||
private endStrike(): void {
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2391b8b7-f9fa-42a8-b046-55ff57b07d02",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user