Added magnet functionality

This commit is contained in:
Martin
2022-12-23 11:36:12 +01:00
parent afc9448826
commit 76c4264838
15 changed files with 193 additions and 13 deletions

View File

@@ -0,0 +1,40 @@
import { Node, Vec3 } from "cc";
import { getDirection } from "../../Services/Utils/VecUtils";
import { Item } from "./Item";
export class ItemAttractor {
private items: Item[] = [];
private speedValues: number[] = [];
public constructor(private playerNode: Node, private speedIncreasePerSecond: number) {}
public gameTick(deltaTime: number): void {
for (let i = 0; i < this.items.length; i++) {
const direction: Vec3 = getDirection(this.playerNode.worldPosition, this.items[i].node.worldPosition);
const position = this.items[i].node.worldPosition.clone();
position.x += direction.x * this.speedValues[i] * deltaTime;
position.y += direction.y * this.speedValues[i] * deltaTime;
this.items[i].node.setWorldPosition(position);
this.speedValues[i] += this.speedIncreasePerSecond * deltaTime;
}
}
public addItem(item: Item): void {
if (this.items.includes(item)) return;
item.PickupEvent.on(this.removeItem, this);
this.items.push(item);
this.speedValues.push(0);
}
private removeItem(item: Item): void {
item.PickupEvent.off(this.removeItem);
const index = this.items.indexOf(item);
this.items.splice(index, 1);
this.speedValues.splice(index, 1);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "9d103e35-89f2-4657-baa4-d89ce8d8b7d9",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -80,7 +80,9 @@ export class ItemManager extends Component {
this.player.Health.heal(this.healthPerPotion);
}
private activateMagnet(): void {}
private activateMagnet(): void {
this.player.Magnet.activate();
}
private giveRandomSkill(): void {}