Added health potion

This commit is contained in:
Martin
2022-12-20 13:37:56 +01:00
parent 1c009e9fa4
commit dbb55957a0
17 changed files with 409 additions and 195 deletions

View File

@@ -0,0 +1,27 @@
import { Animation, Component, Vec3, _decorator } from "cc";
import { ISignal } from "../../../Services/EventSystem/ISignal";
import { Signal } from "../../../Services/EventSystem/Signal";
const { ccclass, property } = _decorator;
@ccclass("HealthPotion")
export class HealthPotion extends Component {
@property(Animation) private animation: Animation;
private pickUpEvent: Signal<HealthPotion> = new Signal<HealthPotion>();
public setup(position: Vec3): void {
this.node.setWorldPosition(position);
this.node.active = true;
this.animation.play("DropStart");
}
public get PickupEvent(): ISignal<HealthPotion> {
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": "18f8de69-e361-4e73-8b98-a2735d0730a6",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,27 @@
import { Component, Prefab, Vec3, _decorator } from "cc";
import { ObjectPool } from "../../../Services/ObjectPool";
import { HealthPotion } from "./HealthPotion";
const { ccclass, property } = _decorator;
@ccclass("HealthPotionSpawner")
export class HealthPotionSpawner extends Component {
@property(Prefab) public healthPotionPrefab: Prefab;
private healthPotionPool: ObjectPool<HealthPotion>;
public init(): void {
this.healthPotionPool = new ObjectPool<HealthPotion>(this.healthPotionPrefab, this.node, 5, "HealthPotion");
}
public spawn(position: Vec3): void {
const healthPotion: HealthPotion = this.healthPotionPool.borrow();
healthPotion.setup(position);
healthPotion.PickupEvent.on(this.return, this);
}
private return(healthPotion: HealthPotion): void {
healthPotion.PickupEvent.off(this.return);
this.healthPotionPool.return(healthPotion);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "1e3b0c7f-c831-4778-8688-d8e2c0f6dc00",
"files": [],
"subMetas": {},
"userData": {}
}