mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 16:46:00 +00:00
Added magnet functionality
This commit is contained in:
14
assets/Scripts/Game/Collision/MagnetCollisionSystem.ts
Normal file
14
assets/Scripts/Game/Collision/MagnetCollisionSystem.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Collider2D, Contact2DType } from "cc";
|
||||
import { Item } from "../Items/Item";
|
||||
import { ItemAttractor } from "../Items/ItemAttractor";
|
||||
import { Magnet } from "../Unit/Player/Magnet";
|
||||
|
||||
export class MagnetCollisionSystem {
|
||||
public constructor(magnet: Magnet, private itemAttractor: ItemAttractor) {
|
||||
magnet.Collider.on(Contact2DType.BEGIN_CONTACT, this.onMagnetContactBegin, this);
|
||||
}
|
||||
|
||||
private onMagnetContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
|
||||
this.itemAttractor.addItem(otherCollider.getComponent(Item));
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "632b80ed-c193-4c84-b571-7f7663bba767",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -14,6 +14,7 @@ export class PlayerSettings {
|
||||
public speed = 0;
|
||||
public regenerationDelay = 0;
|
||||
public collisionDelay = 0;
|
||||
public magnetDuration = 0;
|
||||
public weapon: WeaponSettings = new WeaponSettings();
|
||||
public haloLauncher: HaloLauncherSettings = new HaloLauncherSettings();
|
||||
public horizontalLauncher: WaveLauncherSettings = new WaveLauncherSettings();
|
||||
|
@@ -3,6 +3,7 @@ import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowMan
|
||||
import { delay } from "../Services/Utils/AsyncUtils";
|
||||
import { GameAudioAdapter } from "./Audio/GameAudioAdapter";
|
||||
import { Background } from "./Background/Background";
|
||||
import { MagnetCollisionSystem } from "./Collision/MagnetCollisionSystem";
|
||||
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
|
||||
import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem";
|
||||
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
|
||||
@@ -12,6 +13,7 @@ import { UserData } from "./Data/UserData";
|
||||
import { KeyboardInput } from "./Input/KeyboardInput";
|
||||
import { MultiInput } from "./Input/MultiInput";
|
||||
import { VirtualJoystic } from "./Input/VirtualJoystic";
|
||||
import { ItemAttractor } from "./Items/ItemAttractor";
|
||||
import { ItemManager } from "./Items/ItemManager";
|
||||
import { GameModalLauncher } from "./ModalWIndows/GameModalLauncher";
|
||||
import { Pauser } from "./Pauser";
|
||||
@@ -55,6 +57,8 @@ export class Game extends Component {
|
||||
|
||||
private enemyProjectileLauncher: EnemyProjectileLauncher;
|
||||
|
||||
private itemAttractor: ItemAttractor;
|
||||
|
||||
private gamePauser: Pauser = new Pauser();
|
||||
private gameResult: GameResult;
|
||||
|
||||
@@ -127,6 +131,9 @@ export class Game extends Component {
|
||||
|
||||
new PlayerProjectileCollisionSystem([this.haloProjectileLauncher, this.horizontalProjectileLauncher, this.diagonalProjectileLauncher]);
|
||||
|
||||
this.itemAttractor = new ItemAttractor(this.player.node, 100);
|
||||
new MagnetCollisionSystem(this.player.Magnet, this.itemAttractor);
|
||||
|
||||
const upgrader = new Upgrader(
|
||||
this.player,
|
||||
this.horizontalProjectileLauncher,
|
||||
@@ -178,6 +185,7 @@ export class Game extends Component {
|
||||
this.horizontalProjectileLauncher.gameTick(deltaTime);
|
||||
this.diagonalProjectileLauncher.gameTick(deltaTime);
|
||||
this.enemyProjectileLauncher.gameTick(deltaTime);
|
||||
this.itemAttractor.gameTick(deltaTime);
|
||||
this.background.gameTick();
|
||||
|
||||
this.timeAlive += deltaTime;
|
||||
@@ -199,6 +207,8 @@ export class Game extends Component {
|
||||
playerData.damage = metaUpgrades.getUpgradeValue(MetaUpgradeType.OverallDamage) + settings.weapon.damage;
|
||||
playerData.strikeDelay = settings.weapon.strikeDelay;
|
||||
|
||||
playerData.magnetDuration = settings.magnetDuration;
|
||||
|
||||
return playerData;
|
||||
}
|
||||
}
|
||||
|
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 {}
|
||||
|
||||
|
33
assets/Scripts/Game/Unit/Player/Magnet.ts
Normal file
33
assets/Scripts/Game/Unit/Player/Magnet.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { _decorator, Component, Node, Collider2D, CircleCollider2D } from "cc";
|
||||
import { GameTimer } from "../../../Services/GameTimer";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Magnet")
|
||||
export class Magnet extends Component {
|
||||
@property(CircleCollider2D) private collider: CircleCollider2D;
|
||||
|
||||
private timer: GameTimer;
|
||||
private duration: number;
|
||||
|
||||
public get Collider(): Collider2D {
|
||||
return this.collider;
|
||||
}
|
||||
public init(duration: number): void {
|
||||
this.duration = duration;
|
||||
this.node.active = false;
|
||||
}
|
||||
|
||||
public activate(): void {
|
||||
this.timer = new GameTimer(this.duration);
|
||||
this.node.active = true;
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
if (!this.node.active) return;
|
||||
|
||||
this.timer.gameTick(deltaTime);
|
||||
if (this.timer.tryFinishPeriod()) {
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Player/Magnet.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Player/Magnet.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "05a1d2ca-9083-4d11-ad38-1892bc0d5082",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -3,6 +3,7 @@ import { delay } from "../../../Services/Utils/AsyncUtils";
|
||||
import { IInput } from "../../Input/IInput";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
import { UnitLevel } from "../UnitLevel";
|
||||
import { Magnet } from "./Magnet";
|
||||
import { PlayerRegeneration } from "./PlayerRegeneration";
|
||||
import { PlayerUI } from "./PlayerUI/PlayerUI";
|
||||
import { Weapon } from "./Weapon/Weapon";
|
||||
@@ -14,6 +15,7 @@ export class Player extends Component {
|
||||
@property(BoxCollider2D) private collider: BoxCollider2D;
|
||||
@property(PlayerUI) private playerUI: PlayerUI;
|
||||
@property(Weapon) private weapon: Weapon;
|
||||
@property(Magnet) private magnet: Magnet;
|
||||
@property(Node) private playerGraphics: Node;
|
||||
@property(Animation) private animation: Animation;
|
||||
@property(Sprite) private sprite: Sprite;
|
||||
@@ -34,6 +36,7 @@ export class Player extends Component {
|
||||
this.speed = data.speed;
|
||||
|
||||
this.weapon.init(data.strikeDelay, data.damage);
|
||||
this.magnet.init(data.magnetDuration);
|
||||
this.health.HealthPointsChangeEvent.on(this.animateHpChange, this);
|
||||
this.playerUI.init(this.health);
|
||||
}
|
||||
@@ -50,6 +53,10 @@ export class Player extends Component {
|
||||
return this.weapon;
|
||||
}
|
||||
|
||||
public get Magnet(): Magnet {
|
||||
return this.magnet;
|
||||
}
|
||||
|
||||
public get Regeneration(): PlayerRegeneration {
|
||||
return this.regeneration;
|
||||
}
|
||||
@@ -61,6 +68,7 @@ export class Player extends Component {
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.move(deltaTime);
|
||||
this.weapon.gameTick(deltaTime);
|
||||
this.magnet.gameTick(deltaTime);
|
||||
this.regeneration.gameTick(deltaTime);
|
||||
}
|
||||
|
||||
@@ -117,4 +125,7 @@ export class PlayerData {
|
||||
// Weapon
|
||||
public strikeDelay = 0;
|
||||
public damage = 0;
|
||||
|
||||
// Magnet
|
||||
public magnetDuration = 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user