This commit is contained in:
Martin 2022-11-16 12:26:20 +01:00
parent 3dd10f13ef
commit 0eb9cc907f
23 changed files with 1312 additions and 485 deletions

View File

@ -11,7 +11,7 @@
"speed": 1, "speed": 1,
"wrapMode": 1, "wrapMode": 1,
"enableTrsBlending": false, "enableTrsBlending": false,
"_duration": 0.31666666666666665, "_duration": 1.3,
"_hash": 500763545, "_hash": 500763545,
"_tracks": [ "_tracks": [
{ {
@ -62,7 +62,9 @@
"__type__": "cc.RealCurve", "__type__": "cc.RealCurve",
"_times": [ "_times": [
0, 0,
0.31666666666666665 0.4666666666666667,
0.8666666666666667,
1.3
], ],
"_values": [ "_values": [
{ {
@ -74,10 +76,7 @@
"rightTangentWeight": 1, "rightTangentWeight": 1,
"leftTangent": 0, "leftTangent": 0,
"leftTangentWeight": 1, "leftTangentWeight": 1,
"easingMethod": 0, "easingMethod": 0
"__editorExtras__": {
"tangentMode": 0
}
}, },
{ {
"__type__": "cc.RealKeyframeValue", "__type__": "cc.RealKeyframeValue",
@ -88,10 +87,29 @@
"rightTangentWeight": 1, "rightTangentWeight": 1,
"leftTangent": 0, "leftTangent": 0,
"leftTangentWeight": 1, "leftTangentWeight": 1,
"easingMethod": 0, "easingMethod": 0
"__editorExtras__": { },
"tangentMode": 0 {
} "__type__": "cc.RealKeyframeValue",
"interpolationMode": 0,
"tangentWeightMode": 0,
"value": 0,
"rightTangent": 0,
"rightTangentWeight": 1,
"leftTangent": 0,
"leftTangentWeight": 1,
"easingMethod": 0
},
{
"__type__": "cc.RealKeyframeValue",
"interpolationMode": 0,
"tangentWeightMode": 0,
"value": 0,
"rightTangent": 0,
"rightTangentWeight": 1,
"leftTangent": 0,
"leftTangentWeight": 1,
"easingMethod": 0
} }
], ],
"preExtrapolation": 1, "preExtrapolation": 1,
@ -107,7 +125,9 @@
"__type__": "cc.RealCurve", "__type__": "cc.RealCurve",
"_times": [ "_times": [
0, 0,
0.31666666666666665 0.4666666666666667,
0.8666666666666667,
1.3
], ],
"_values": [ "_values": [
{ {
@ -119,10 +139,7 @@
"rightTangentWeight": 1, "rightTangentWeight": 1,
"leftTangent": 0, "leftTangent": 0,
"leftTangentWeight": 1, "leftTangentWeight": 1,
"easingMethod": 0, "easingMethod": 0
"__editorExtras__": {
"tangentMode": 0
}
}, },
{ {
"__type__": "cc.RealKeyframeValue", "__type__": "cc.RealKeyframeValue",
@ -133,10 +150,29 @@
"rightTangentWeight": 1, "rightTangentWeight": 1,
"leftTangent": 0, "leftTangent": 0,
"leftTangentWeight": 1, "leftTangentWeight": 1,
"easingMethod": 0, "easingMethod": 0
"__editorExtras__": { },
"tangentMode": 0 {
} "__type__": "cc.RealKeyframeValue",
"interpolationMode": 0,
"tangentWeightMode": 0,
"value": 0,
"rightTangent": 0,
"rightTangentWeight": 1,
"leftTangent": 0,
"leftTangentWeight": 1,
"easingMethod": 0
},
{
"__type__": "cc.RealKeyframeValue",
"interpolationMode": 0,
"tangentWeightMode": 0,
"value": 0,
"rightTangent": 0,
"rightTangentWeight": 1,
"leftTangent": 0,
"leftTangentWeight": 1,
"easingMethod": 0
} }
], ],
"preExtrapolation": 1, "preExtrapolation": 1,
@ -152,36 +188,54 @@
"__type__": "cc.RealCurve", "__type__": "cc.RealCurve",
"_times": [ "_times": [
0, 0,
0.31666666666666665 0.4666666666666667,
0.8666666666666667,
1.3
], ],
"_values": [ "_values": [
{ {
"__type__": "cc.RealKeyframeValue", "__type__": "cc.RealKeyframeValue",
"interpolationMode": 0, "interpolationMode": 0,
"tangentWeightMode": 0, "tangentWeightMode": 0,
"value": 0, "value": 170,
"rightTangent": 0, "rightTangent": 0,
"rightTangentWeight": 1, "rightTangentWeight": 1,
"leftTangent": 0, "leftTangent": 0,
"leftTangentWeight": 1, "leftTangentWeight": 1,
"easingMethod": 0, "easingMethod": 0
"__editorExtras__": {
"tangentMode": 0
}
}, },
{ {
"__type__": "cc.RealKeyframeValue", "__type__": "cc.RealKeyframeValue",
"interpolationMode": 0, "interpolationMode": 0,
"tangentWeightMode": 0, "tangentWeightMode": 0,
"value": 90, "value": 170,
"rightTangent": 0, "rightTangent": 0,
"rightTangentWeight": 1, "rightTangentWeight": 1,
"leftTangent": 0, "leftTangent": 0,
"leftTangentWeight": 1, "leftTangentWeight": 1,
"easingMethod": 0, "easingMethod": 0
"__editorExtras__": { },
"tangentMode": 0 {
} "__type__": "cc.RealKeyframeValue",
"interpolationMode": 0,
"tangentWeightMode": 0,
"value": 370,
"rightTangent": 0,
"rightTangentWeight": 1,
"leftTangent": 0,
"leftTangentWeight": 1,
"easingMethod": 0
},
{
"__type__": "cc.RealKeyframeValue",
"interpolationMode": 0,
"tangentWeightMode": 0,
"value": 370,
"rightTangent": 0,
"rightTangentWeight": 1,
"leftTangent": 0,
"leftTangentWeight": 1,
"easingMethod": 0
} }
], ],
"preExtrapolation": 1, "preExtrapolation": 1,

View File

@ -0,0 +1,194 @@
[
{
"__type__": "cc.Prefab",
"_name": "XP",
"_objFlags": 0,
"_native": "",
"data": {
"__id__": 1
},
"optimizationPolicy": 0,
"persistent": false,
"asyncLoadAssets": false
},
{
"__type__": "cc.Node",
"_name": "XP",
"_objFlags": 0,
"__editorExtras__": {},
"_parent": null,
"_children": [],
"_active": true,
"_components": [
{
"__id__": 2
},
{
"__id__": 4
},
{
"__id__": 6
},
{
"__id__": 8
}
],
"_prefab": {
"__id__": 10
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 3
},
"_contentSize": {
"__type__": "cc.Size",
"width": 26,
"height": 26
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "c3Jb8uGT9DFpm574XDqiKL"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 5
},
"_customMaterial": null,
"_srcBlendFactor": 2,
"_dstBlendFactor": 4,
"_color": {
"__type__": "cc.Color",
"r": 88,
"g": 101,
"b": 255,
"a": 255
},
"_spriteFrame": {
"__uuid__": "f12a23c4-b924-4322-a260-3d982428f1e8@f9941",
"__expectedType__": "cc.SpriteFrame"
},
"_type": 0,
"_fillType": 0,
"_sizeMode": 1,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_useGrayscale": false,
"_atlas": null,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "cfXyVJoitMGp/0liEUZfNP"
},
{
"__type__": "cc.CircleCollider2D",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 7
},
"tag": 0,
"_group": 16,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
"_restitution": 0,
"_offset": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_radius": 13,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "75WVRoLrtKLrJihCVsxa4D"
},
{
"__type__": "7a536G2OudFtpTsoF8yLXiW",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 9
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "72AObOIbxCf6LIYsYqoFZF"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "405dEytktEvavOK8J9RVEp"
}
]

View File

@ -0,0 +1,13 @@
{
"ver": "1.1.40",
"importer": "prefab",
"imported": true,
"uuid": "b83dbe32-6b43-4ea9-b0e2-5f96c09f459d",
"files": [
".json"
],
"subMetas": {},
"userData": {
"syncNodeName": "XP"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ import { GroupType } from "../GroupType";
import { Player } from "../Player/Player"; import { Player } from "../Player/Player";
import { GameTimer } from "../../Services/GameTimer"; import { GameTimer } from "../../Services/GameTimer";
import { Enemy } from "../Enemy/Enemy"; import { Enemy } from "../Enemy/Enemy";
import { XP } from "../XP/XP";
export class PlayerCollisionSystem { export class PlayerCollisionSystem {
private playerContacts: Collider2D[] = []; private playerContacts: Collider2D[] = [];
@ -20,6 +21,7 @@ export class PlayerCollisionSystem {
this.collisionTimer = new GameTimer(collisionDelay); this.collisionTimer = new GameTimer(collisionDelay);
this.groupToResolver.set(GroupType.ENEMY, this.resolveEnemyContact.bind(this)); this.groupToResolver.set(GroupType.ENEMY, this.resolveEnemyContact.bind(this));
this.groupToResolver.set(GroupType.XP, this.resolveXpContact.bind(this));
} }
public gameTick(deltaTime: number): void { public gameTick(deltaTime: number): void {
@ -60,4 +62,12 @@ export class PlayerCollisionSystem {
console.log("Collided with enemy: Damage: " + damage); console.log("Collided with enemy: Damage: " + damage);
this.player.Health.damage(damage); this.player.Health.damage(damage);
} }
private resolveXpContact(xpCollider: Collider2D): void {
const xp: XP = xpCollider.node.getComponent(XP);
this.player.addXp(xp.Value);
xp.pickup();
console.log("Collided with xp: " + xp);
}
} }

View File

@ -1,4 +1,4 @@
import { BoxCollider2D, Component, Vec2, Vec3, _decorator } from "cc"; import { BoxCollider2D, Component, randomRange, Vec3, _decorator } from "cc";
import { ISignal } from "../../Services/EventSystem/ISignal"; import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal"; import { Signal } from "../../Services/EventSystem/Signal";
import { UnitHealth } from "../Player/UnitHealth"; import { UnitHealth } from "../Player/UnitHealth";
@ -10,10 +10,13 @@ export class Enemy extends Component implements IDamageDealing {
private health: UnitHealth = new UnitHealth(1); private health: UnitHealth = new UnitHealth(1);
private deathEvent: Signal<Enemy> = new Signal<Enemy>(); private deathEvent: Signal<Enemy> = new Signal<Enemy>();
private speed: number;
public setup(): void { public setup(position: Vec3): void {
this.node.active = true;
this.health = new UnitHealth(1); this.health = new UnitHealth(1);
this.speed = randomRange(0.5, 1);
this.node.setWorldPosition(position);
this.node.active = true;
} }
public get Collider(): BoxCollider2D { public get Collider(): BoxCollider2D {
@ -41,8 +44,8 @@ export class Enemy extends Component implements IDamageDealing {
public moveBy(move: Vec3): void { public moveBy(move: Vec3): void {
const newPosition: Vec3 = this.node.worldPosition; const newPosition: Vec3 = this.node.worldPosition;
newPosition.x += move.x; newPosition.x += move.x * this.speed;
newPosition.y += move.y; newPosition.y += move.y * this.speed;
this.node.setWorldPosition(newPosition); this.node.setWorldPosition(newPosition);
} }

View File

@ -0,0 +1,41 @@
import { Component, Node, _decorator } from "cc";
import { XPSpawner } from "../XP/XPSpawner";
import { Enemy } from "./Enemy";
import { EnemyMover } from "./EnemyMover";
import { EnemySpawner } from "./EnemySpawner";
const { ccclass, property } = _decorator;
@ccclass("EnemyManager")
export class EnemyManager extends Component {
@property(EnemySpawner) private enemySpawner: EnemySpawner;
@property(XPSpawner) private xpSpawner: XPSpawner;
private enemyMover: EnemyMover;
public init(targetNode: Node): void {
this.enemyMover = new EnemyMover(targetNode);
this.enemySpawner.init();
this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this);
this.xpSpawner.init();
}
public gameTick(deltaTime: number): void {
this.enemySpawner.gameTick(deltaTime);
this.enemyMover.gameTick(deltaTime);
}
private onEnemyAdded(enemy: Enemy): void {
enemy.DeathEvent.on(this.onEnemyDied, this);
this.enemyMover.addEnemy(enemy);
}
private onEnemyDied(enemy: Enemy): void {
enemy.DeathEvent.off(this.onEnemyDied);
this.xpSpawner.spawnXp(enemy.node.worldPosition, 1);
this.enemyMover.removeEnemy(enemy);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "c5416aed-64b1-426e-959b-bc7d5c745e76",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -21,7 +21,7 @@ export class EnemyMover {
public gameTick(deltaTime: number): void { public gameTick(deltaTime: number): void {
this.enemies.forEach((enemy) => { this.enemies.forEach((enemy) => {
let direction: Vec3 = new Vec3(); let direction: Vec3 = new Vec3();
direction = Vec3.subtract(direction, this.targetNode.position, enemy.node.position); direction = Vec3.subtract(direction, this.targetNode.worldPosition, enemy.node.worldPosition);
enemy.moveBy(direction.multiplyScalar(deltaTime).normalize()); enemy.moveBy(direction.multiplyScalar(deltaTime).normalize());
}); });
} }

View File

@ -1,22 +1,23 @@
import { Component, Prefab, randomRange, Vec3, _decorator } from "cc"; import { Component, Prefab, randomRange, Vec3, _decorator } from "cc";
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
import { GameTimer } from "../../Services/GameTimer"; import { GameTimer } from "../../Services/GameTimer";
import { ObjectPool } from "../../Services/ObjectPool"; import { ObjectPool } from "../../Services/ObjectPool";
import { Enemy } from "./Enemy"; import { Enemy } from "./Enemy";
import { EnemyMover } from "./EnemyMover";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("EnemySpawner") @ccclass("EnemySpawner")
export class EnemySpawner extends Component { export class EnemySpawner extends Component {
@property(Prefab) private enemies: Prefab[] = []; @property(Prefab) private enemies: Prefab[] = [];
public enemyAddedEvent: Signal<Enemy> = new Signal<Enemy>();
private enemyPool: ObjectPool<Enemy>; private enemyPool: ObjectPool<Enemy>;
private spawnTimer: GameTimer; private spawnTimer: GameTimer;
private enemyMover: EnemyMover;
public init(enemyMover: EnemyMover): void { public init(): void {
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, Enemy); this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, "Enemy");
this.spawnTimer = new GameTimer(1); this.spawnTimer = new GameTimer(1);
this.enemyMover = enemyMover;
} }
public gameTick(deltaTime: number): void { public gameTick(deltaTime: number): void {
@ -24,26 +25,23 @@ export class EnemySpawner extends Component {
if (this.spawnTimer.tryFinishPeriod()) { if (this.spawnTimer.tryFinishPeriod()) {
this.spawnNewEnemy(); this.spawnNewEnemy();
} }
}
this.enemyMover.gameTick(deltaTime); public get EnemyAddedEvent(): ISignal<Enemy> {
return this.enemyAddedEvent;
} }
private spawnNewEnemy(): void { private spawnNewEnemy(): void {
const enemy = this.enemyPool.borrow(); const enemy = this.enemyPool.borrow();
enemy.node.active = true; enemy.setup(new Vec3(randomRange(0, 300), randomRange(0, 800)));
enemy.node.setPosition(new Vec3(randomRange(-300, 300), randomRange(-300, 300)));
enemy.setup();
enemy.DeathEvent.on(this.returnEnemyToPool, this); enemy.DeathEvent.on(this.returnEnemyToPool, this);
this.enemyMover.addEnemy(enemy); this.enemyAddedEvent.trigger(enemy);
} }
private returnEnemyToPool(enemy: Enemy): void { private returnEnemyToPool(enemy: Enemy): void {
enemy.DeathEvent.off(this.returnEnemyToPool); enemy.DeathEvent.off(this.returnEnemyToPool);
this.enemyPool.return(enemy); this.enemyPool.return(enemy);
this.enemyMover.removeEnemy(enemy);
} }
} }

View File

@ -1,13 +1,12 @@
import { Component, KeyCode, _decorator } from "cc"; import { Camera, CCFloat, Component, KeyCode, _decorator } from "cc";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem"; import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem"; import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
import { EnemySpawner } from "./Enemy/EnemySpawner"; import { EnemyManager } from "./Enemy/EnemyManager";
import { KeyboardInput } from "./Input/KeyboardInput";
import { MultiInput } from "./Input/MultiInput"; import { MultiInput } from "./Input/MultiInput";
import { VirtualJoystic } from "./Input/VirtualJoystic"; import { VirtualJoystic } from "./Input/VirtualJoystic";
import { KeyboardInput } from "./Input/KeyboardInput";
import { Player } from "./Player/Player"; import { Player } from "./Player/Player";
import { Weapon } from "./Weapon"; import { Weapon } from "./Weapon";
import { EnemyMover } from "./Enemy/EnemyMover";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("GameBootstrapper") @ccclass("GameBootstrapper")
@ -15,9 +14,10 @@ export class GameBootstrapper extends Component {
@property(VirtualJoystic) private virtualJoystic: VirtualJoystic; @property(VirtualJoystic) private virtualJoystic: VirtualJoystic;
@property(Player) private player: Player; @property(Player) private player: Player;
@property(Weapon) private weapon: Weapon; @property(Weapon) private weapon: Weapon;
@property(EnemySpawner) private enemySpawner: EnemySpawner; @property(EnemyManager) private enemyManager: EnemyManager;
@property(Number) private strikeDelay = 0; @property(CCFloat) private strikeDelay = 0;
@property(Number) private collisionDelay = 0; @property(CCFloat) private collisionDelay = 0;
@property(Camera) private camera: Camera;
private playerCollisionSystem: PlayerCollisionSystem; private playerCollisionSystem: PlayerCollisionSystem;
public start(): void { public start(): void {
@ -28,15 +28,17 @@ export class GameBootstrapper extends Component {
const dualInput: MultiInput = new MultiInput([this.virtualJoystic, wasd, arrowKeys]); const dualInput: MultiInput = new MultiInput([this.virtualJoystic, wasd, arrowKeys]);
this.player.init(dualInput, this.weapon, 50); this.player.init(dualInput, this.weapon, 50);
const enemyMover = new EnemyMover(this.player.node);
this.enemySpawner.init(enemyMover);
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, this.collisionDelay); this.playerCollisionSystem = new PlayerCollisionSystem(this.player, this.collisionDelay);
new WeaponCollisionSystem(this.weapon); new WeaponCollisionSystem(this.weapon);
this.enemyManager.init(this.player.node);
} }
public update(deltaTime: number): void { public update(deltaTime: number): void {
this.player.gameTick(deltaTime); this.player.gameTick(deltaTime);
this.playerCollisionSystem.gameTick(deltaTime); this.playerCollisionSystem.gameTick(deltaTime);
this.enemySpawner.gameTick(deltaTime); this.enemyManager.gameTick(deltaTime);
this.camera.node.worldPosition = this.player.node.worldPosition;
} }
} }

View File

@ -4,5 +4,6 @@ export enum GroupType {
DEFAULT = 1 << 0, DEFAULT = 1 << 0,
PLAYER = 1 << 1, PLAYER = 1 << 1,
ENEMY = 1 << 2, ENEMY = 1 << 2,
WEAPON = 1 << 3 WEAPON = 1 << 3,
XP = 1 << 4
} }

View File

@ -1,10 +1,10 @@
import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch } from "cc"; import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch, CCFloat } from "cc";
import { IInput } from "./IInput"; import { IInput } from "./IInput";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("VirtualJoystic") @ccclass("VirtualJoystic")
export class VirtualJoystic extends Component implements IInput { export class VirtualJoystic extends Component implements IInput {
@property(Number) private maxDistance = 10; @property(CCFloat) private maxDistance = 10;
@property(Node) private knob: Node; @property(Node) private knob: Node;
#isUsingJoystic = false; #isUsingJoystic = false;
@ -35,6 +35,7 @@ export class VirtualJoystic extends Component implements IInput {
} }
private activateMouseJoystic(e: EventMouse): void { private activateMouseJoystic(e: EventMouse): void {
console.log(e.getUILocation());
this.activateJoystic(e.getUILocation()); this.activateJoystic(e.getUILocation());
} }
@ -43,7 +44,7 @@ export class VirtualJoystic extends Component implements IInput {
this.node.active = true; this.node.active = true;
this.#defaultPosition = location; this.#defaultPosition = location;
this.node.setWorldPosition(new Vec3(this.#defaultPosition.x, this.#defaultPosition.y, 0)); this.node.setPosition(new Vec3(this.#defaultPosition.x, this.#defaultPosition.y, 0));
this.knob.position = new Vec3(); this.knob.position = new Vec3();
} }

View File

@ -1,4 +1,4 @@
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc"; import { BoxCollider2D, Collider2D, Component, PointToPointConstraint, Vec2, Vec3, _decorator } from "cc";
import { IInput } from "../Input/IInput"; import { IInput } from "../Input/IInput";
import { Weapon } from "../Weapon"; import { Weapon } from "../Weapon";
import { PlayerUI } from "./PlayerUI/PlayerUI"; import { PlayerUI } from "./PlayerUI/PlayerUI";
@ -15,6 +15,8 @@ export class Player extends Component {
private weapon: Weapon; private weapon: Weapon;
private health: UnitHealth; private health: UnitHealth;
private xp: number;
public init(input: IInput, weapon: Weapon, maxHp: number): void { public init(input: IInput, weapon: Weapon, maxHp: number): void {
this.input = input; this.input = input;
this.weapon = weapon; this.weapon = weapon;
@ -34,6 +36,10 @@ export class Player extends Component {
return this.collider; return this.collider;
} }
public addXp(points: number): void {
this.xp += points;
}
public gameTick(deltaTime: number): void { public gameTick(deltaTime: number): void {
const movement: Vec2 = this.input.getAxis(); const movement: Vec2 = this.input.getAxis();
movement.x *= deltaTime * this.speed; movement.x *= deltaTime * this.speed;
@ -45,6 +51,6 @@ export class Player extends Component {
this.node.setWorldPosition(newPosition); this.node.setWorldPosition(newPosition);
this.weapon.gameTick(deltaTime, movement); this.weapon.gameTick(deltaTime);
} }
} }

View File

@ -1,6 +1,5 @@
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc"; import { Animation, AnimationState, BoxCollider2D, Collider2D, Component, _decorator } from "cc";
import { GameTimer } from "../Services/GameTimer"; import { GameTimer } from "../Services/GameTimer";
import { delay } from "../Services/Utils/AsyncUtils";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("Weapon") @ccclass("Weapon")
@ -9,24 +8,21 @@ export class Weapon extends Component {
@property(BoxCollider2D) private collider: BoxCollider2D; @property(BoxCollider2D) private collider: BoxCollider2D;
private strikeTimer: GameTimer; private strikeTimer: GameTimer;
private lastDirection = new Vec2(); private strikeState: AnimationState;
public init(strikeDelay: number): void { public init(strikeDelay: number): void {
this.strikeTimer = new GameTimer(strikeDelay); this.strikeTimer = new GameTimer(strikeDelay);
this.node.active = false; 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;
} }
public gameTick(deltaTime: number, movement: Vec2): void { public gameTick(deltaTime: number): void {
let direction: Vec2 = movement.normalize();
if (direction.x == 0 && direction.y == 0) {
direction = this.lastDirection;
} else {
this.lastDirection = direction;
}
this.strikeTimer.gameTick(deltaTime); this.strikeTimer.gameTick(deltaTime);
if (this.strikeTimer.tryFinishPeriod()) { if (this.strikeTimer.tryFinishPeriod()) {
this.strike(direction); this.strike();
} }
} }
@ -38,16 +34,12 @@ export class Weapon extends Component {
return 5; return 5;
} }
private async strike(direction: Vec2): Promise<void> { private strike(): void {
this.node.active = true; this.node.active = true;
this.weaponAnimation.play(this.strikeState.name);
}
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI - 45; private endStrike(): void {
this.node.eulerAngles = new Vec3(0, 0, angle);
this.weaponAnimation.getState("WeaponSwing").speed = 4;
this.weaponAnimation.play("WeaponSwing");
await delay(1000);
this.node.active = false; this.node.active = false;
} }
} }

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "1e85dd0e-3cbc-4870-9a6a-50e0c29cfeb7",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,29 @@
import { Component, Vec3, _decorator } from "cc";
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
const { ccclass, property } = _decorator;
@ccclass("XP")
export class XP extends Component {
private pickUpEvent: Signal<XP> = new Signal<XP>();
private value = 2;
public setup(position: Vec3, value: number): void {
this.node.setWorldPosition(position);
this.value = value;
this.node.active = true;
}
public get Value(): number {
return this.value;
}
public get PickupEvent(): ISignal<XP> {
return this.pickUpEvent;
}
public pickup(): void {
this.pickUpEvent.trigger(this);
this.node.active = false;
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "7a5361b6-3ae7-45b6-94ec-a05f322d7896",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,25 @@
import { Component, Prefab, Vec3, _decorator } from "cc";
import { ObjectPool } from "../../Services/ObjectPool";
import { XP } from "./XP";
const { ccclass, property } = _decorator;
@ccclass("XPSpawner")
export class XPSpawner extends Component {
@property(Prefab) public xpPrefab: Prefab;
private xpPool: ObjectPool<XP>;
public init(): void {
this.xpPool = new ObjectPool<XP>(this.xpPrefab, this.node, 5, "XP");
}
public spawnXp(position: Vec3, value: number): void {
const xp: XP = this.xpPool.borrow();
xp.setup(position, value);
xp.PickupEvent.on(this.returnXp, this);
}
private returnXp(xp: XP): void {
xp.PickupEvent.off(this.returnXp);
this.xpPool.return(xp);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ab9e206b-f11a-417c-814b-07d29c59ed6e",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -17,8 +17,12 @@ export class Signal<T> implements ISignal<T> {
} }
public trigger(data: T): void { public trigger(data: T): void {
for (let i = 0; i < this.handlers.length; i++) { // protect from trigger >> off
this.handlers[i].call(this.thisArgs[i], data); const handlers: ((data: T) => void)[] = [...this.handlers];
const thisArgs: any[] = [...this.thisArgs];
for (let i = 0; i < handlers.length; i++) {
handlers[i].call(thisArgs[i], data);
} }
} }
} }

View File

@ -4,12 +4,12 @@ export class ObjectPool<T extends Component> {
private prefab: Prefab; private prefab: Prefab;
private parent: Node; private parent: Node;
private pooledObjects: PooledObject<T>[] = []; private pooledObjects: PooledObject<T>[] = [];
private componentType: { new (): T }; private componentName: string;
public constructor(prefab: Prefab, parent: Node, defaultPoolCount: number, componentType: { new (): T }) { public constructor(prefab: Prefab, parent: Node, defaultPoolCount: number, componentName: string) {
this.prefab = prefab; this.prefab = prefab;
this.parent = parent; this.parent = parent;
this.componentType = componentType; this.componentName = componentName;
for (let i = 0; i < defaultPoolCount; i++) { for (let i = 0; i < defaultPoolCount; i++) {
this.pooledObjects.push(this.createNew()); this.pooledObjects.push(this.createNew());
@ -35,7 +35,7 @@ export class ObjectPool<T extends Component> {
} }
private createNew(): PooledObject<T> { private createNew(): PooledObject<T> {
const newPooledObject: PooledObject<T> = new PooledObject(this.prefab, this.parent, this.componentType); const newPooledObject: PooledObject<T> = new PooledObject(this.prefab, this.parent, this.componentName);
this.pooledObjects.push(newPooledObject); this.pooledObjects.push(newPooledObject);
return newPooledObject; return newPooledObject;
@ -48,13 +48,13 @@ class PooledObject<T extends Component> {
private instancedNode: Node; private instancedNode: Node;
private instancedComponent: T; private instancedComponent: T;
public constructor(prefab: Prefab, defaultParent: Node, componentType: { new (): T }) { public constructor(prefab: Prefab, defaultParent: Node, componentName: string) {
this.defaultParent = defaultParent; this.defaultParent = defaultParent;
this.instancedNode = instantiate(prefab); this.instancedNode = instantiate(prefab);
this.instancedComponent = <T>this.instancedNode.getComponent(componentType.name); this.instancedComponent = <T>this.instancedNode.getComponent(componentName);
if (this.instancedComponent == null) { if (this.instancedComponent == null) {
throw new Error("Object " + prefab.name + " does not have component " + componentType.name); console.log("Object " + prefab.name + " does not have component " + componentName);
} }
this.clear(); this.clear();

View File

@ -12,13 +12,25 @@
{ {
"index": 3, "index": 3,
"name": "WEAPON" "name": "WEAPON"
},
{
"index": 4,
"name": "XP"
} }
], ],
"collisionMatrix": { "collisionMatrix": {
"0": 0, "0": 0,
"1": 4, "1": 20,
"2": 10, "2": 10,
"3": 4 "3": 4,
"4": 2
}
},
"general": {
"designResolution": {
"width": 640,
"height": 960,
"fitHeight": true
} }
} }
} }