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
},
"tag": 0,
"_group": 8,
"_group": 32,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
@ -175,6 +175,9 @@
"__prefab": {
"__id__": 9
},
"collider": {
"__id__": 6
},
"_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 { 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);
}

View File

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

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 { 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<PlayerProjectile>;
private projectiles: PlayerProjectile[] = [];
private projectilePool: ObjectPool<Projectile>;
private projectiles: Projectile[] = [];
private directions: Vec2[] = [];
private playerNode: Node;
private projectileCollisionEvent: Signal<ProjectileCollision> = new Signal<ProjectileCollision>();
public init(playerNode: Node, settings: HaloLauncherSettings): void {
this.playerNode = playerNode;
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.defaultCooldown = settings.cooldown;
@ -43,6 +49,10 @@ export class HaloProjectileLauncher extends Component {
}
}
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
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);
}
}

View File

@ -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": {