mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 08:36:14 +00:00
new structure, spawner, rebind
This commit is contained in:
63
assets/Scripts/Game/Collision/PlayerCollisionSystem.ts
Normal file
63
assets/Scripts/Game/Collision/PlayerCollisionSystem.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Collider2D, Contact2DType } from "cc";
|
||||
import { GroupType } from "../GroupType";
|
||||
import { Player } from "../Player/Player";
|
||||
import { GameTimer } from "../../Services/GameTimer";
|
||||
import { Enemy } from "../Enemy/Enemy";
|
||||
|
||||
export class PlayerCollisionSystem {
|
||||
private playerContacts: Collider2D[] = [];
|
||||
private collisionTimer: GameTimer;
|
||||
private player: Player;
|
||||
|
||||
private groupToResolver: Map<number, (collider: Collider2D) => void> = new Map<number, (collider: Collider2D) => void>();
|
||||
|
||||
public constructor(player: Player, collisionDelay: number) {
|
||||
this.player = player;
|
||||
|
||||
player.Collider.on(Contact2DType.BEGIN_CONTACT, this.onPlayerContactBegin, this);
|
||||
player.Collider.on(Contact2DType.END_CONTACT, this.onPlayerContactEnd, this);
|
||||
|
||||
this.collisionTimer = new GameTimer(collisionDelay);
|
||||
|
||||
this.groupToResolver.set(GroupType.ENEMY, this.resolveEnemyContact.bind(this));
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.collisionTimer.gameTick(deltaTime);
|
||||
if (this.collisionTimer.tryFinishPeriod()) {
|
||||
this.resolveAllContacts();
|
||||
}
|
||||
}
|
||||
|
||||
private onPlayerContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
|
||||
this.playerContacts.push(otherCollider);
|
||||
this.resolveContact(otherCollider);
|
||||
}
|
||||
|
||||
private onPlayerContactEnd(_selfCollider: Collider2D, otherCollider: Collider2D): void {
|
||||
const index: number = this.playerContacts.indexOf(otherCollider);
|
||||
if (index != -1) {
|
||||
this.playerContacts.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private resolveAllContacts(): void {
|
||||
for (let i = 0; i < this.playerContacts.length; i++) {
|
||||
this.resolveContact(this.playerContacts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private resolveContact(otherCollider: Collider2D): void {
|
||||
if (this.groupToResolver.has(otherCollider.group)) {
|
||||
this.groupToResolver.get(otherCollider.group)(otherCollider);
|
||||
} else {
|
||||
console.log("Collided with undefined group: " + otherCollider.group);
|
||||
}
|
||||
}
|
||||
|
||||
private resolveEnemyContact(enemyCollider: Collider2D): void {
|
||||
const damage: number = enemyCollider.node.getComponent(Enemy).Damage;
|
||||
console.log("Collided with enemy: Damage: " + damage);
|
||||
this.player.Health.damage(damage);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ea4efcef-c9f5-4116-b43d-7cae85bf298f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
15
assets/Scripts/Game/Collision/WeaponCollisionSystem.ts
Normal file
15
assets/Scripts/Game/Collision/WeaponCollisionSystem.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Collider2D, Contact2DType } from "cc";
|
||||
import { Enemy } from "../Enemy/Enemy";
|
||||
import { Weapon } from "../Weapon";
|
||||
|
||||
export class WeaponCollisionSystem {
|
||||
private weapon: Weapon;
|
||||
public constructor(weapon: Weapon) {
|
||||
this.weapon = weapon;
|
||||
weapon.Collider.on(Contact2DType.BEGIN_CONTACT, this.onWeaponContactBegin, this);
|
||||
}
|
||||
|
||||
private onWeaponContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
|
||||
otherCollider.getComponent(Enemy).dealDamage(this.weapon.Damage);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "eea89f2a-424b-4169-b2b7-7bf5261e9750",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user