Projectiles

This commit is contained in:
Martin 2022-11-30 08:21:22 +01:00
parent da70723f2d
commit 7e20e41482
16 changed files with 140 additions and 31 deletions

View File

@ -147,7 +147,7 @@
"__id__": 7 "__id__": 7
}, },
"tag": 0, "tag": 0,
"_group": 8, "_group": 32,
"_density": 1, "_density": 1,
"_sensor": false, "_sensor": false,
"_friction": 0.2, "_friction": 0.2,
@ -175,6 +175,9 @@
"__prefab": { "__prefab": {
"__id__": 9 "__id__": 9
}, },
"collider": {
"__id__": 6
},
"_id": "" "_id": ""
}, },
{ {

View File

@ -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);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "9d016ddb-6e34-4f37-b197-e89aaa82c572",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,6 +1,7 @@
import { Camera, Component, JsonAsset, KeyCode, _decorator } from "cc"; import { Camera, Component, JsonAsset, KeyCode, _decorator } from "cc";
import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager"; import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem"; import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem";
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem"; import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
import { GameSettings } from "./Data/GameSettings"; import { GameSettings } from "./Data/GameSettings";
import { KeyboardInput } from "./Input/KeyboardInput"; import { KeyboardInput } from "./Input/KeyboardInput";
@ -10,9 +11,8 @@ import { GameModalLauncher } from "./ModalWIndows/GameModalLauncher";
import { Pauser } from "./Pauser"; import { Pauser } from "./Pauser";
import { GameUI } from "./UI/GameUI"; import { GameUI } from "./UI/GameUI";
import { EnemyManager } from "./Unit/Enemy/EnemyManager"; import { EnemyManager } from "./Unit/Enemy/EnemyManager";
import { HaloProjectileLauncher } from "./Unit/Player/Halo/HaloProjectileLauncher";
import { Player } from "./Unit/Player/Player"; import { Player } from "./Unit/Player/Player";
import { HaloProjectileLauncher } from "./Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher";
import { Upgrader } from "./Upgrades/Upgrader"; import { Upgrader } from "./Upgrades/Upgrader";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ -53,6 +53,8 @@ export class GameBootstrapper extends Component {
this.haloProjectiles.init(this.player.node, settings.player.haloLauncher); this.haloProjectiles.init(this.player.node, settings.player.haloLauncher);
this.haloProjectiles.upgrade(); this.haloProjectiles.upgrade();
new PlayerProjectileCollisionSystem(this.haloProjectiles);
this.gameUI.init(this.player); this.gameUI.init(this.player);
} }

View File

@ -5,5 +5,7 @@ export enum GroupType {
PLAYER = 1 << 1, PLAYER = 1 << 1,
ENEMY = 1 << 2, ENEMY = 1 << 2,
WEAPON = 1 << 3, WEAPON = 1 << 3,
XP = 1 << 4 XP = 1 << 4,
PLAYER_PROJECTILE = 1 << 5,
ENEMY_PROJECTILE = 1 << 6
} }

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "d53f6106-9e6a-4e3c-9348-557d9898b7ca",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -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<ProjectileCollision> = new Signal<ProjectileCollision>();
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<ProjectileCollision> {
return this.contactBeginEvent;
}
private onColliderContactBegin(thisCollider: Collider2D, otherCollider: Collider2D): void {
this.contactBeginEvent.trigger({ otherCollider, projectile: this });
}
}

View File

@ -0,0 +1,7 @@
import { Collider2D } from "cc";
import { Projectile } from "./Projectile";
export class ProjectileCollision {
public otherCollider: Collider2D;
public projectile: Projectile;
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "0f4db0b8-c13a-41cd-8d53-b87b6b615ac6",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -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) {
}
}

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "f996de47-37ab-489b-820f-fe7a38c09c64",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -1,9 +1,13 @@
import { Component, Prefab, Vec2, Vec3, _decorator, Node } from "cc"; import { Component, Node, Prefab, Vec2, Vec3, _decorator } from "cc";
import { GameTimer } from "../../../../Services/GameTimer"; import { ISignal } from "../../../../../Services/EventSystem/ISignal";
import { ObjectPool } from "../../../../Services/ObjectPool"; import { Signal } from "../../../../../Services/EventSystem/Signal";
import { roundToOneDecimal } from "../../../../Services/Utils/MathUtils"; import { GameTimer } from "../../../../../Services/GameTimer";
import { HaloLauncherSettings } from "../../../Data/GameSettings"; import { ObjectPool } from "../../../../../Services/ObjectPool";
import { PlayerProjectile } from "./PlayerProjectile"; 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; const { ccclass, property } = _decorator;
@ccclass("HaloProjectileLauncher") @ccclass("HaloProjectileLauncher")
@ -18,16 +22,18 @@ export class HaloProjectileLauncher extends Component {
private isFiring = false; private isFiring = false;
private projectilePool: ObjectPool<PlayerProjectile>; private projectilePool: ObjectPool<Projectile>;
private projectiles: PlayerProjectile[] = []; private projectiles: Projectile[] = [];
private directions: Vec2[] = []; private directions: Vec2[] = [];
private playerNode: Node; private playerNode: Node;
private projectileCollisionEvent: Signal<ProjectileCollision> = new Signal<ProjectileCollision>();
public init(playerNode: Node, settings: HaloLauncherSettings): void { public init(playerNode: Node, settings: HaloLauncherSettings): void {
this.playerNode = playerNode; this.playerNode = playerNode;
this.projectilesToSpawn = settings.projectilesToSpawn; this.projectilesToSpawn = settings.projectilesToSpawn;
this.projectilePool = new ObjectPool<PlayerProjectile>(this.projectilePrefab, this.node, this.projectilesToSpawn, "PlayerProjectile"); this.projectilePool = new ObjectPool<Projectile>(this.projectilePrefab, this.node, this.projectilesToSpawn, "PlayerProjectile");
this.speed = settings.projectileSpeed; this.speed = settings.projectileSpeed;
this.defaultCooldown = settings.cooldown; this.defaultCooldown = settings.cooldown;
@ -43,6 +49,10 @@ export class HaloProjectileLauncher extends Component {
} }
} }
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
return this.projectileCollisionEvent;
}
public upgrade(): void { public upgrade(): void {
this.currentLevel++; this.currentLevel++;
this.fireTimer = new GameTimer(this.defaultCooldown - this.currentLevel); this.fireTimer = new GameTimer(this.defaultCooldown - this.currentLevel);
@ -64,9 +74,11 @@ export class HaloProjectileLauncher extends Component {
private fireProjectiles(): void { private fireProjectiles(): void {
for (let index = 0; index < this.projectilesToSpawn; index++) { 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.setWorldPosition(this.playerNode.worldPosition);
projectile.node.active = true; projectile.node.active = true;
projectile.ContactBeginEvent.on(this.onProjectileCollision, this);
this.projectiles.push(projectile); this.projectiles.push(projectile);
} }
@ -87,6 +99,7 @@ export class HaloProjectileLauncher extends Component {
this.lifetimeTimer.gameTick(deltaTime); this.lifetimeTimer.gameTick(deltaTime);
if (this.lifetimeTimer.tryFinishPeriod()) { if (this.lifetimeTimer.tryFinishPeriod()) {
for (const projectile of this.projectiles) { for (const projectile of this.projectiles) {
projectile.ContactBeginEvent.off(this.onProjectileCollision);
this.projectilePool.return(projectile); this.projectilePool.return(projectile);
} }
@ -94,4 +107,8 @@ export class HaloProjectileLauncher extends Component {
this.isFiring = false; this.isFiring = false;
} }
} }
private onProjectileCollision(projectileCollision: ProjectileCollision): void {
this.projectileCollisionEvent.trigger(projectileCollision);
}
} }

View File

@ -16,14 +16,24 @@
{ {
"index": 4, "index": 4,
"name": "XP" "name": "XP"
},
{
"index": 5,
"name": "PLAYER_PROJECTILE"
},
{
"index": 6,
"name": "ENEMY_PROJECTILE"
} }
], ],
"collisionMatrix": { "collisionMatrix": {
"0": 0, "0": 0,
"1": 20, "1": 84,
"2": 10, "2": 42,
"3": 4, "3": 4,
"4": 2 "4": 2,
"5": 4,
"6": 2
} }
}, },
"general": { "general": {