From 7e20e4148269972e698f88be1d615a1ae3a4577a Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 30 Nov 2022 08:21:22 +0100 Subject: [PATCH] Projectiles --- assets/Media/Prefabs/PlayerProjectile.prefab | 5 ++- .../PlayerProjectileCollisionSystem.ts | 13 +++++++ .../PlayerProjectileCollisionSystem.ts.meta | 9 +++++ assets/Scripts/Game/GameBootstrapper.ts | 6 ++- assets/Scripts/Game/GroupType.ts | 4 +- assets/Scripts/Game/Projectile.meta | 12 ++++++ assets/Scripts/Game/Projectile/Projectile.ts | 27 ++++++++++++++ .../Projectile.ts.meta} | 0 .../Game/Projectile/ProjectileCollision.ts | 7 ++++ .../Projectile/ProjectileCollision.ts.meta | 9 +++++ .../Game/Unit/Player/Halo/PlayerProjectile.ts | 14 ------- .../Game/Unit/Player/ProjectileLauncher.meta | 12 ++++++ .../Player/{ => ProjectileLauncher}/Halo.meta | 0 .../Halo/HaloProjectileLauncher.ts | 37 ++++++++++++++----- .../Halo/HaloProjectileLauncher.ts.meta | 0 settings/v2/packages/project.json | 16 ++++++-- 16 files changed, 140 insertions(+), 31 deletions(-) create mode 100644 assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts create mode 100644 assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts.meta create mode 100644 assets/Scripts/Game/Projectile.meta create mode 100644 assets/Scripts/Game/Projectile/Projectile.ts rename assets/Scripts/Game/{Unit/Player/Halo/PlayerProjectile.ts.meta => Projectile/Projectile.ts.meta} (100%) create mode 100644 assets/Scripts/Game/Projectile/ProjectileCollision.ts create mode 100644 assets/Scripts/Game/Projectile/ProjectileCollision.ts.meta delete mode 100644 assets/Scripts/Game/Unit/Player/Halo/PlayerProjectile.ts create mode 100644 assets/Scripts/Game/Unit/Player/ProjectileLauncher.meta rename assets/Scripts/Game/Unit/Player/{ => ProjectileLauncher}/Halo.meta (100%) rename assets/Scripts/Game/Unit/Player/{ => ProjectileLauncher}/Halo/HaloProjectileLauncher.ts (66%) rename assets/Scripts/Game/Unit/Player/{ => ProjectileLauncher}/Halo/HaloProjectileLauncher.ts.meta (100%) diff --git a/assets/Media/Prefabs/PlayerProjectile.prefab b/assets/Media/Prefabs/PlayerProjectile.prefab index 6b1a3a9..75c942e 100644 --- a/assets/Media/Prefabs/PlayerProjectile.prefab +++ b/assets/Media/Prefabs/PlayerProjectile.prefab @@ -147,7 +147,7 @@ "__id__": 7 }, "tag": 0, - "_group": 8, + "_group": 32, "_density": 1, "_sensor": false, "_friction": 0.2, @@ -175,6 +175,9 @@ "__prefab": { "__id__": 9 }, + "collider": { + "__id__": 6 + }, "_id": "" }, { diff --git a/assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts b/assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts new file mode 100644 index 0000000..a34547f --- /dev/null +++ b/assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts @@ -0,0 +1,13 @@ +import { ProjectileCollision } from "../Projectile/ProjectileCollision"; +import { Enemy } from "../Unit/Enemy/Enemy"; +import { HaloProjectileLauncher } from "../Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher"; + +export class PlayerProjectileCollisionSystem { + public constructor(haloLauncher: HaloProjectileLauncher) { + haloLauncher.ProjectileCollisionEvent.on(this.onProjectileCollision, this); + } + + private onProjectileCollision(projectileCollision: ProjectileCollision): void { + projectileCollision.otherCollider.getComponent(Enemy).dealDamage(1); + } +} diff --git a/assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts.meta b/assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts.meta new file mode 100644 index 0000000..381e8bd --- /dev/null +++ b/assets/Scripts/Game/Collision/PlayerProjectileCollisionSystem.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "9d016ddb-6e34-4f37-b197-e89aaa82c572", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/Game/GameBootstrapper.ts b/assets/Scripts/Game/GameBootstrapper.ts index 5ab6ee4..f299633 100644 --- a/assets/Scripts/Game/GameBootstrapper.ts +++ b/assets/Scripts/Game/GameBootstrapper.ts @@ -1,6 +1,7 @@ import { Camera, Component, JsonAsset, KeyCode, _decorator } from "cc"; import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager"; import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem"; +import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem"; import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem"; import { GameSettings } from "./Data/GameSettings"; import { KeyboardInput } from "./Input/KeyboardInput"; @@ -10,9 +11,8 @@ import { GameModalLauncher } from "./ModalWIndows/GameModalLauncher"; import { Pauser } from "./Pauser"; import { GameUI } from "./UI/GameUI"; import { EnemyManager } from "./Unit/Enemy/EnemyManager"; -import { HaloProjectileLauncher } from "./Unit/Player/Halo/HaloProjectileLauncher"; import { Player } from "./Unit/Player/Player"; - +import { HaloProjectileLauncher } from "./Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher"; import { Upgrader } from "./Upgrades/Upgrader"; const { ccclass, property } = _decorator; @@ -53,6 +53,8 @@ export class GameBootstrapper extends Component { this.haloProjectiles.init(this.player.node, settings.player.haloLauncher); this.haloProjectiles.upgrade(); + new PlayerProjectileCollisionSystem(this.haloProjectiles); + this.gameUI.init(this.player); } diff --git a/assets/Scripts/Game/GroupType.ts b/assets/Scripts/Game/GroupType.ts index a0e31c4..3b04337 100644 --- a/assets/Scripts/Game/GroupType.ts +++ b/assets/Scripts/Game/GroupType.ts @@ -5,5 +5,7 @@ export enum GroupType { PLAYER = 1 << 1, ENEMY = 1 << 2, WEAPON = 1 << 3, - XP = 1 << 4 + XP = 1 << 4, + PLAYER_PROJECTILE = 1 << 5, + ENEMY_PROJECTILE = 1 << 6 } diff --git a/assets/Scripts/Game/Projectile.meta b/assets/Scripts/Game/Projectile.meta new file mode 100644 index 0000000..a7b5639 --- /dev/null +++ b/assets/Scripts/Game/Projectile.meta @@ -0,0 +1,12 @@ +{ + "ver": "1.1.0", + "importer": "directory", + "imported": true, + "uuid": "d53f6106-9e6a-4e3c-9348-557d9898b7ca", + "files": [], + "subMetas": {}, + "userData": { + "compressionType": {}, + "isRemoteBundle": {} + } +} diff --git a/assets/Scripts/Game/Projectile/Projectile.ts b/assets/Scripts/Game/Projectile/Projectile.ts new file mode 100644 index 0000000..cf6712d --- /dev/null +++ b/assets/Scripts/Game/Projectile/Projectile.ts @@ -0,0 +1,27 @@ +import { CircleCollider2D, Collider2D, Component, Contact2DType, _decorator } from "cc"; +import { ISignal } from "../../Services/EventSystem/ISignal"; +import { Signal } from "../../Services/EventSystem/Signal"; +import { ProjectileCollision } from "./ProjectileCollision"; +const { ccclass, property } = _decorator; + +@ccclass("Projectile") +export class Projectile extends Component { + @property(CircleCollider2D) private collider: CircleCollider2D; + private contactBeginEvent: Signal = new Signal(); + + private isInit = false; + public tryInit(): void { + if (this.isInit) return; + this.isInit = true; + + this.collider.on(Contact2DType.BEGIN_CONTACT, this.onColliderContactBegin, this); + } + + public get ContactBeginEvent(): ISignal { + return this.contactBeginEvent; + } + + private onColliderContactBegin(thisCollider: Collider2D, otherCollider: Collider2D): void { + this.contactBeginEvent.trigger({ otherCollider, projectile: this }); + } +} diff --git a/assets/Scripts/Game/Unit/Player/Halo/PlayerProjectile.ts.meta b/assets/Scripts/Game/Projectile/Projectile.ts.meta similarity index 100% rename from assets/Scripts/Game/Unit/Player/Halo/PlayerProjectile.ts.meta rename to assets/Scripts/Game/Projectile/Projectile.ts.meta diff --git a/assets/Scripts/Game/Projectile/ProjectileCollision.ts b/assets/Scripts/Game/Projectile/ProjectileCollision.ts new file mode 100644 index 0000000..a705f2b --- /dev/null +++ b/assets/Scripts/Game/Projectile/ProjectileCollision.ts @@ -0,0 +1,7 @@ +import { Collider2D } from "cc"; +import { Projectile } from "./Projectile"; + +export class ProjectileCollision { + public otherCollider: Collider2D; + public projectile: Projectile; +} diff --git a/assets/Scripts/Game/Projectile/ProjectileCollision.ts.meta b/assets/Scripts/Game/Projectile/ProjectileCollision.ts.meta new file mode 100644 index 0000000..8a2a906 --- /dev/null +++ b/assets/Scripts/Game/Projectile/ProjectileCollision.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "0f4db0b8-c13a-41cd-8d53-b87b6b615ac6", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/Game/Unit/Player/Halo/PlayerProjectile.ts b/assets/Scripts/Game/Unit/Player/Halo/PlayerProjectile.ts deleted file mode 100644 index 43fb2d6..0000000 --- a/assets/Scripts/Game/Unit/Player/Halo/PlayerProjectile.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { _decorator, Component, Node } from 'cc'; -const { ccclass, property } = _decorator; - -@ccclass('PlayerProjectile') -export class PlayerProjectile extends Component { - start() { - - } - - update(deltaTime: number) { - - } -} - diff --git a/assets/Scripts/Game/Unit/Player/ProjectileLauncher.meta b/assets/Scripts/Game/Unit/Player/ProjectileLauncher.meta new file mode 100644 index 0000000..bc4e0e0 --- /dev/null +++ b/assets/Scripts/Game/Unit/Player/ProjectileLauncher.meta @@ -0,0 +1,12 @@ +{ + "ver": "1.1.0", + "importer": "directory", + "imported": true, + "uuid": "f996de47-37ab-489b-820f-fe7a38c09c64", + "files": [], + "subMetas": {}, + "userData": { + "compressionType": {}, + "isRemoteBundle": {} + } +} diff --git a/assets/Scripts/Game/Unit/Player/Halo.meta b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/Halo.meta similarity index 100% rename from assets/Scripts/Game/Unit/Player/Halo.meta rename to assets/Scripts/Game/Unit/Player/ProjectileLauncher/Halo.meta diff --git a/assets/Scripts/Game/Unit/Player/Halo/HaloProjectileLauncher.ts b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher.ts similarity index 66% rename from assets/Scripts/Game/Unit/Player/Halo/HaloProjectileLauncher.ts rename to assets/Scripts/Game/Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher.ts index 983f73b..d389731 100644 --- a/assets/Scripts/Game/Unit/Player/Halo/HaloProjectileLauncher.ts +++ b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher.ts @@ -1,9 +1,13 @@ -import { Component, Prefab, Vec2, Vec3, _decorator, Node } from "cc"; -import { GameTimer } from "../../../../Services/GameTimer"; -import { ObjectPool } from "../../../../Services/ObjectPool"; -import { roundToOneDecimal } from "../../../../Services/Utils/MathUtils"; -import { HaloLauncherSettings } from "../../../Data/GameSettings"; -import { PlayerProjectile } from "./PlayerProjectile"; +import { Component, Node, Prefab, Vec2, Vec3, _decorator } from "cc"; +import { ISignal } from "../../../../../Services/EventSystem/ISignal"; +import { Signal } from "../../../../../Services/EventSystem/Signal"; +import { GameTimer } from "../../../../../Services/GameTimer"; +import { ObjectPool } from "../../../../../Services/ObjectPool"; +import { roundToOneDecimal } from "../../../../../Services/Utils/MathUtils"; +import { HaloLauncherSettings } from "../../../../Data/GameSettings"; +import { Projectile } from "../../../../Projectile/Projectile"; +import { ProjectileCollision } from "../../../../Projectile/ProjectileCollision"; + const { ccclass, property } = _decorator; @ccclass("HaloProjectileLauncher") @@ -18,16 +22,18 @@ export class HaloProjectileLauncher extends Component { private isFiring = false; - private projectilePool: ObjectPool; - private projectiles: PlayerProjectile[] = []; + private projectilePool: ObjectPool; + private projectiles: Projectile[] = []; private directions: Vec2[] = []; private playerNode: Node; + private projectileCollisionEvent: Signal = new Signal(); + public init(playerNode: Node, settings: HaloLauncherSettings): void { this.playerNode = playerNode; this.projectilesToSpawn = settings.projectilesToSpawn; - this.projectilePool = new ObjectPool(this.projectilePrefab, this.node, this.projectilesToSpawn, "PlayerProjectile"); + this.projectilePool = new ObjectPool(this.projectilePrefab, this.node, this.projectilesToSpawn, "PlayerProjectile"); this.speed = settings.projectileSpeed; this.defaultCooldown = settings.cooldown; @@ -43,6 +49,10 @@ export class HaloProjectileLauncher extends Component { } } + public get ProjectileCollisionEvent(): ISignal { + return this.projectileCollisionEvent; + } + public upgrade(): void { this.currentLevel++; this.fireTimer = new GameTimer(this.defaultCooldown - this.currentLevel); @@ -64,9 +74,11 @@ export class HaloProjectileLauncher extends Component { private fireProjectiles(): void { for (let index = 0; index < this.projectilesToSpawn; index++) { - const projectile: PlayerProjectile = this.projectilePool.borrow(); + const projectile: Projectile = this.projectilePool.borrow(); + projectile.tryInit(); projectile.node.setWorldPosition(this.playerNode.worldPosition); projectile.node.active = true; + projectile.ContactBeginEvent.on(this.onProjectileCollision, this); this.projectiles.push(projectile); } @@ -87,6 +99,7 @@ export class HaloProjectileLauncher extends Component { this.lifetimeTimer.gameTick(deltaTime); if (this.lifetimeTimer.tryFinishPeriod()) { for (const projectile of this.projectiles) { + projectile.ContactBeginEvent.off(this.onProjectileCollision); this.projectilePool.return(projectile); } @@ -94,4 +107,8 @@ export class HaloProjectileLauncher extends Component { this.isFiring = false; } } + + private onProjectileCollision(projectileCollision: ProjectileCollision): void { + this.projectileCollisionEvent.trigger(projectileCollision); + } } diff --git a/assets/Scripts/Game/Unit/Player/Halo/HaloProjectileLauncher.ts.meta b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher.ts.meta similarity index 100% rename from assets/Scripts/Game/Unit/Player/Halo/HaloProjectileLauncher.ts.meta rename to assets/Scripts/Game/Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher.ts.meta diff --git a/settings/v2/packages/project.json b/settings/v2/packages/project.json index 1e14b92..09a058c 100644 --- a/settings/v2/packages/project.json +++ b/settings/v2/packages/project.json @@ -16,14 +16,24 @@ { "index": 4, "name": "XP" + }, + { + "index": 5, + "name": "PLAYER_PROJECTILE" + }, + { + "index": 6, + "name": "ENEMY_PROJECTILE" } ], "collisionMatrix": { "0": 0, - "1": 20, - "2": 10, + "1": 84, + "2": 42, "3": 4, - "4": 2 + "4": 2, + "5": 4, + "6": 2 } }, "general": {