mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-10 00:56:06 +00:00
Added magnet functionality
This commit is contained in:
40
assets/Scripts/Game/Items/ItemAttractor.ts
Normal file
40
assets/Scripts/Game/Items/ItemAttractor.ts
Normal 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);
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Items/ItemAttractor.ts.meta
Normal file
9
assets/Scripts/Game/Items/ItemAttractor.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "9d103e35-89f2-4657-baa4-d89ce8d8b7d9",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -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 {}
|
||||
|
||||
|
Reference in New Issue
Block a user