From 4400311a113821cff18bba1c094b63cfddc5c811 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 19 Dec 2022 16:24:52 +0100 Subject: [PATCH] Blinking end of lifetime --- assets/Media/Prefabs/Game/Bat.prefab | 130 ++++++++++++++++++++---- assets/Media/Prefabs/Game/Goblin.prefab | 130 ++++++++++++++++++++---- assets/Media/Prefabs/Game/Shark.prefab | 130 ++++++++++++++++++++---- assets/Scripts/Game/Unit/Enemy/Enemy.ts | 27 ++++- 4 files changed, 350 insertions(+), 67 deletions(-) diff --git a/assets/Media/Prefabs/Game/Bat.prefab b/assets/Media/Prefabs/Game/Bat.prefab index 9e3852f..c87ec9e 100644 --- a/assets/Media/Prefabs/Game/Bat.prefab +++ b/assets/Media/Prefabs/Game/Bat.prefab @@ -17,24 +17,25 @@ "_objFlags": 0, "__editorExtras__": {}, "_parent": null, - "_children": [], + "_children": [ + { + "__id__": 2 + } + ], "_active": true, "_components": [ - { - "__id__": 2 - }, - { - "__id__": 4 - }, - { - "__id__": 6 - }, { "__id__": 8 + }, + { + "__id__": 10 + }, + { + "__id__": 12 } ], "_prefab": { - "__id__": 10 + "__id__": 14 }, "_lpos": { "__type__": "cc.Vec3", @@ -64,16 +65,64 @@ }, "_id": "" }, + { + "__type__": "cc.Node", + "_name": "Graphics", + "_objFlags": 0, + "_parent": { + "__id__": 1 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 3 + }, + { + "__id__": 5 + } + ], + "_prefab": { + "__id__": 7 + }, + "_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 + "__id__": 2 }, - "_enabled": false, + "_enabled": true, "__prefab": { - "__id__": 3 + "__id__": 4 }, "_contentSize": { "__type__": "cc.Size", @@ -89,18 +138,18 @@ }, { "__type__": "cc.CompPrefabInfo", - "fileId": "bbsWYc2MNCLLweNFP7pjHF" + "fileId": "c4cbC3G4lJcovI9zoCe/21" }, { "__type__": "cc.Sprite", "_name": "", "_objFlags": 0, "node": { - "__id__": 1 + "__id__": 2 }, "_enabled": true, "__prefab": { - "__id__": 5 + "__id__": 6 }, "_customMaterial": null, "_srcBlendFactor": 2, @@ -133,7 +182,44 @@ }, { "__type__": "cc.CompPrefabInfo", - "fileId": "bfkBAEWOZDbILym05Q5wrx" + "fileId": "78W7mwOx9Kc4cDvfMFcZjX" + }, + { + "__type__": "cc.PrefabInfo", + "root": { + "__id__": 1 + }, + "asset": { + "__id__": 0 + }, + "fileId": "f0Ff0jfgNKTqjoStNmJGP/" + }, + { + "__type__": "cc.UITransform", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 1 + }, + "_enabled": false, + "__prefab": { + "__id__": 9 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 25, + "height": 26 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_id": "" + }, + { + "__type__": "cc.CompPrefabInfo", + "fileId": "bbsWYc2MNCLLweNFP7pjHF" }, { "__type__": "cc.BoxCollider2D", @@ -144,7 +230,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 7 + "__id__": 11 }, "tag": 0, "_group": 4, @@ -177,13 +263,13 @@ }, "_enabled": true, "__prefab": { - "__id__": 9 + "__id__": 13 }, "collider": { - "__id__": 6 + "__id__": 10 }, "sprite": { - "__id__": 4 + "__id__": 5 }, "whiteMaterial": { "__uuid__": "9cb17675-370b-48ed-bfa2-bb613a88af44", diff --git a/assets/Media/Prefabs/Game/Goblin.prefab b/assets/Media/Prefabs/Game/Goblin.prefab index 0fa0be3..8d432cc 100644 --- a/assets/Media/Prefabs/Game/Goblin.prefab +++ b/assets/Media/Prefabs/Game/Goblin.prefab @@ -17,24 +17,25 @@ "_objFlags": 0, "__editorExtras__": {}, "_parent": null, - "_children": [], + "_children": [ + { + "__id__": 2 + } + ], "_active": true, "_components": [ - { - "__id__": 2 - }, - { - "__id__": 4 - }, - { - "__id__": 6 - }, { "__id__": 8 + }, + { + "__id__": 10 + }, + { + "__id__": 12 } ], "_prefab": { - "__id__": 10 + "__id__": 14 }, "_lpos": { "__type__": "cc.Vec3", @@ -64,16 +65,64 @@ }, "_id": "" }, + { + "__type__": "cc.Node", + "_name": "Graphics", + "_objFlags": 0, + "_parent": { + "__id__": 1 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 3 + }, + { + "__id__": 5 + } + ], + "_prefab": { + "__id__": 7 + }, + "_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 + "__id__": 2 }, - "_enabled": false, + "_enabled": true, "__prefab": { - "__id__": 3 + "__id__": 4 }, "_contentSize": { "__type__": "cc.Size", @@ -89,18 +138,18 @@ }, { "__type__": "cc.CompPrefabInfo", - "fileId": "bbsWYc2MNCLLweNFP7pjHF" + "fileId": "464/MPdiZA5qZYQRCq97Ha" }, { "__type__": "cc.Sprite", "_name": "", "_objFlags": 0, "node": { - "__id__": 1 + "__id__": 2 }, "_enabled": true, "__prefab": { - "__id__": 5 + "__id__": 6 }, "_customMaterial": null, "_srcBlendFactor": 2, @@ -133,7 +182,44 @@ }, { "__type__": "cc.CompPrefabInfo", - "fileId": "bfkBAEWOZDbILym05Q5wrx" + "fileId": "d0xqMqB4RM45zTqWTxtmE+" + }, + { + "__type__": "cc.PrefabInfo", + "root": { + "__id__": 1 + }, + "asset": { + "__id__": 0 + }, + "fileId": "eeHhbzhLlHZ6xMMi0bNFTG" + }, + { + "__type__": "cc.UITransform", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 1 + }, + "_enabled": false, + "__prefab": { + "__id__": 9 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 25, + "height": 28 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_id": "" + }, + { + "__type__": "cc.CompPrefabInfo", + "fileId": "bbsWYc2MNCLLweNFP7pjHF" }, { "__type__": "cc.BoxCollider2D", @@ -144,7 +230,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 7 + "__id__": 11 }, "tag": 0, "_group": 4, @@ -177,13 +263,13 @@ }, "_enabled": true, "__prefab": { - "__id__": 9 + "__id__": 13 }, "collider": { - "__id__": 6 + "__id__": 10 }, "sprite": { - "__id__": 4 + "__id__": 5 }, "whiteMaterial": { "__uuid__": "9cb17675-370b-48ed-bfa2-bb613a88af44", diff --git a/assets/Media/Prefabs/Game/Shark.prefab b/assets/Media/Prefabs/Game/Shark.prefab index f5efccf..210a032 100644 --- a/assets/Media/Prefabs/Game/Shark.prefab +++ b/assets/Media/Prefabs/Game/Shark.prefab @@ -17,24 +17,25 @@ "_objFlags": 0, "__editorExtras__": {}, "_parent": null, - "_children": [], + "_children": [ + { + "__id__": 2 + } + ], "_active": true, "_components": [ - { - "__id__": 2 - }, - { - "__id__": 4 - }, - { - "__id__": 6 - }, { "__id__": 8 + }, + { + "__id__": 10 + }, + { + "__id__": 12 } ], "_prefab": { - "__id__": 10 + "__id__": 14 }, "_lpos": { "__type__": "cc.Vec3", @@ -64,16 +65,64 @@ }, "_id": "" }, + { + "__type__": "cc.Node", + "_name": "Graphics", + "_objFlags": 0, + "_parent": { + "__id__": 1 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 3 + }, + { + "__id__": 5 + } + ], + "_prefab": { + "__id__": 7 + }, + "_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 + "__id__": 2 }, - "_enabled": false, + "_enabled": true, "__prefab": { - "__id__": 3 + "__id__": 4 }, "_contentSize": { "__type__": "cc.Size", @@ -89,18 +138,18 @@ }, { "__type__": "cc.CompPrefabInfo", - "fileId": "bbsWYc2MNCLLweNFP7pjHF" + "fileId": "a3+ZZgs3ROZYsgwk/9mKPx" }, { "__type__": "cc.Sprite", "_name": "", "_objFlags": 0, "node": { - "__id__": 1 + "__id__": 2 }, "_enabled": true, "__prefab": { - "__id__": 5 + "__id__": 6 }, "_customMaterial": null, "_srcBlendFactor": 2, @@ -133,7 +182,44 @@ }, { "__type__": "cc.CompPrefabInfo", - "fileId": "bfkBAEWOZDbILym05Q5wrx" + "fileId": "2a9qQUmpNPhLo3/pi7NiW+" + }, + { + "__type__": "cc.PrefabInfo", + "root": { + "__id__": 1 + }, + "asset": { + "__id__": 0 + }, + "fileId": "1amYNnXbNLeKZa6imz7kGg" + }, + { + "__type__": "cc.UITransform", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 1 + }, + "_enabled": false, + "__prefab": { + "__id__": 9 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 34, + "height": 33 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_id": "" + }, + { + "__type__": "cc.CompPrefabInfo", + "fileId": "bbsWYc2MNCLLweNFP7pjHF" }, { "__type__": "cc.BoxCollider2D", @@ -144,7 +230,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 7 + "__id__": 11 }, "tag": 0, "_group": 4, @@ -177,13 +263,13 @@ }, "_enabled": true, "__prefab": { - "__id__": 9 + "__id__": 13 }, "collider": { - "__id__": 6 + "__id__": 10 }, "sprite": { - "__id__": 4 + "__id__": 5 }, "whiteMaterial": { "__uuid__": "9cb17675-370b-48ed-bfa2-bb613a88af44", diff --git a/assets/Scripts/Game/Unit/Enemy/Enemy.ts b/assets/Scripts/Game/Unit/Enemy/Enemy.ts index d4967dc..cf8d660 100644 --- a/assets/Scripts/Game/Unit/Enemy/Enemy.ts +++ b/assets/Scripts/Game/Unit/Enemy/Enemy.ts @@ -1,4 +1,4 @@ -import { BoxCollider2D, Component, Material, randomRange, Sprite, Vec3, _decorator } from "cc"; +import { BoxCollider2D, Color, Component, Material, randomRange, Sprite, Vec3, _decorator } from "cc"; import { ISignal } from "../../../Services/EventSystem/ISignal"; import { Signal } from "../../../Services/EventSystem/Signal"; import { delay } from "../../../Services/Utils/AsyncUtils"; @@ -29,6 +29,8 @@ export class Enemy extends Component { private xpReward: number; private goldReward: number; + private endOfLifetimeTriggered = false; + public setup(position: Vec3, settings: EnemySettings): void { this.defaultMaterial = this.sprite.material; @@ -46,6 +48,7 @@ export class Enemy extends Component { this.node.active = true; this.health.HealthPointsChangeEvent.on(this.animateHurt, this); + this.endOfLifetimeTriggered = false; } public get MovementType(): EnemyMovementType { @@ -92,16 +95,38 @@ export class Enemy extends Component { newPosition.x += move.x * this.speedX * deltaTime; newPosition.y += move.y * this.speedY * deltaTime; + if (move.x < 0) { + this.sprite.node.setScale(-1, 1, 1); + } else { + this.sprite.node.setScale(1, 1, 1); + } + this.node.setWorldPosition(newPosition); if (0 < this.lifetimeLeft) { this.lifetimeLeft -= deltaTime; if (this.lifetimeLeft <= 0) { this.lifetimeEndedEvent.trigger(this); + } else if (this.lifetimeLeft <= 2) { + this.animateEndOfLifetime(); } } } + private async animateEndOfLifetime(): Promise { + if (this.endOfLifetimeTriggered) return; + + this.endOfLifetimeTriggered = true; + + while (this.node.active) { + this.sprite.node.active = false; + await delay(200); + + this.sprite.node.active = true; + await delay(200); + } + } + private async animateHurt(): Promise { this.sprite.material = this.whiteMaterial; await delay(100);