new structure, spawner, rebind

This commit is contained in:
Martin
2022-11-08 19:45:57 +01:00
parent 5b098af31d
commit 279218b4c3
45 changed files with 1157 additions and 302 deletions

View File

@@ -0,0 +1,50 @@
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { VirtualJoystic } from "../Input/VirtualJoystic";
import { Weapon } from "../Weapon";
import { UnitHealth } from "./UnitHealth";
import { PlayerUI } from "./PlayerUI/PlayerUI";
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 virtualJoystic: VirtualJoystic;
private weapon: Weapon;
private health: UnitHealth;
public init(virtualJoystic: VirtualJoystic, weapon: Weapon, maxHp: number): void {
this.virtualJoystic = virtualJoystic;
this.weapon = weapon;
this.health = new UnitHealth(maxHp);
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 Collider(): Collider2D {
return this.collider;
}
public gameTick(deltaTime: number): void {
const movement: Vec2 = this.virtualJoystic.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, movement);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "b570abb8-1bcd-4ef0-85e8-4d783531001d",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "82d14029-0ea6-4de6-b796-05339d2d19b1",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View 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;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "84a1c057-2ef8-4ead-8903-5753f36ac735",
"files": [],
"subMetas": {},
"userData": {}
}

View 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);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "634f676f-c335-42a2-85e8-f214e9ce5eb7",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,42 @@
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
export class UnitHealth {
private healthPoints: number;
private maxHealthPoints: number;
private healthPointsChangeEvent: Signal<number> = new Signal<number>();
public constructor(maxHealth: number) {
this.maxHealthPoints = maxHealth;
this.healthPoints = maxHealth;
}
public get IsAlive(): boolean {
return 0 < this.healthPoints;
}
public get HealthPoints(): number {
return this.healthPoints;
}
public get MaxHealthPoints(): number {
return this.maxHealthPoints;
}
public get HealthPointsChangeEvent(): ISignal<number> {
return this.healthPointsChangeEvent;
}
public heal(points: number): void {
this.healthPoints = Math.min(this.maxHealthPoints, this.healthPoints + points);
this.healthPointsChangeEvent.trigger(this.healthPoints);
}
public damage(points: number): void {
this.healthPoints -= points;
this.healthPointsChangeEvent.trigger(this.healthPoints);
}
public setMaxHealth(maxHealth: number): void {
this.maxHealthPoints = maxHealth;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "8118806a-dc7c-4e94-84c2-b0a95be43209",
"files": [],
"subMetas": {},
"userData": {}
}