Added object pool, event system collision system

This commit is contained in:
Martin 2022-11-08 11:42:14 +01:00
parent f250c24f90
commit 5b098af31d
21 changed files with 757 additions and 243 deletions

12
assets/Media/Prefabs.meta Normal file
View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "84d44585-c4ad-4c89-bfd7-8631d9f9c6ab",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,198 @@
[
{
"__type__": "cc.Prefab",
"_name": "Enemy",
"_objFlags": 0,
"_native": "",
"data": {
"__id__": 1
},
"optimizationPolicy": 0,
"persistent": false,
"asyncLoadAssets": false
},
{
"__type__": "cc.Node",
"_name": "Enemy",
"_objFlags": 0,
"__editorExtras__": {},
"_parent": null,
"_children": [],
"_active": true,
"_components": [
{
"__id__": 2
},
{
"__id__": 4
},
{
"__id__": 6
},
{
"__id__": 8
}
],
"_prefab": {
"__id__": 10
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 3
},
"_contentSize": {
"__type__": "cc.Size",
"width": 40,
"height": 40
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "bbsWYc2MNCLLweNFP7pjHF"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 5
},
"_customMaterial": null,
"_srcBlendFactor": 2,
"_dstBlendFactor": 4,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_spriteFrame": {
"__uuid__": "20835ba4-6145-4fbc-a58a-051ce700aa3e@f9941",
"__expectedType__": "cc.SpriteFrame"
},
"_type": 0,
"_fillType": 0,
"_sizeMode": 1,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_useGrayscale": false,
"_atlas": null,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "bfkBAEWOZDbILym05Q5wrx"
},
{
"__type__": "cc.BoxCollider2D",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 7
},
"tag": 0,
"_group": 1,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
"_restitution": 0,
"_offset": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_size": {
"__type__": "cc.Size",
"width": 40,
"height": 40
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "5dR8969SVAfLXx5AY1X9B5"
},
{
"__type__": "e6f96nAJ4NOs7RnNWxo20US",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 9
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "94XkV2AslIlKb4oyiOnsKv"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "b3GFrfkl5AR4Q9jndybzln"
}
]

View File

@ -0,0 +1,13 @@
{
"ver": "1.1.40",
"importer": "prefab",
"imported": true,
"uuid": "fc9a455d-1b5f-460e-96eb-7c0ebf07d3d0",
"files": [
".json"
],
"subMetas": {},
"userData": {
"syncNodeName": "Enemy"
}
}

View File

@ -20,10 +20,12 @@
],
"_active": true,
"_components": [],
"_prefab": null,
"_prefab": {
"__id__": 44
},
"autoReleaseAssets": false,
"_globals": {
"__id__": 39
"__id__": 45
},
"_id": "cffa5b19-2043-4cef-8221-1a10ab7a2c1f"
},
@ -42,25 +44,28 @@
"__id__": 5
},
{
"__id__": 9
"__id__": 13
},
{
"__id__": 22
},
{
"__id__": 27
}
],
"_active": true,
"_components": [
{
"__id__": 35
"__id__": 40
},
{
"__id__": 36
"__id__": 41
},
{
"__id__": 37
"__id__": 42
},
{
"__id__": 38
"__id__": 43
}
],
"_prefab": null,
@ -180,159 +185,126 @@
},
{
"__type__": "cc.Node",
"_name": "Enemy",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"_prefab": {
"__id__": 6
},
{
"__id__": 7
"__editorExtras__": {}
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 5
},
"asset": {
"__uuid__": "fc9a455d-1b5f-460e-96eb-7c0ebf07d3d0",
"__expectedType__": "cc.Prefab"
},
"fileId": "b3GFrfkl5AR4Q9jndybzln",
"instance": {
"__id__": 7
}
},
{
"__type__": "cc.PrefabInstance",
"fileId": "6eLx/bqYlD4q9xuucUXxZQ",
"mountedChildren": [],
"mountedComponents": [],
"propertyOverrides": [
{
"__id__": 8
},
{
"__id__": 10
},
{
"__id__": 11
},
{
"__id__": 12
}
],
"_prefab": null,
"_lpos": {
"removedComponents": []
},
{
"__type__": "CCPropertyOverrideInfo",
"targetInfo": {
"__id__": 9
},
"propertyPath": [
"_name"
],
"value": "Enemy"
},
{
"__type__": "cc.TargetInfo",
"localID": [
"b3GFrfkl5AR4Q9jndybzln"
]
},
{
"__type__": "CCPropertyOverrideInfo",
"targetInfo": {
"__id__": 9
},
"propertyPath": [
"_lpos"
],
"value": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
}
},
"_lrot": {
{
"__type__": "CCPropertyOverrideInfo",
"targetInfo": {
"__id__": 9
},
"propertyPath": [
"_lrot"
],
"value": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
}
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
{
"__type__": "CCPropertyOverrideInfo",
"targetInfo": {
"__id__": 9
},
"_layer": 33554432,
"_euler": {
"propertyPath": [
"_euler"
],
"value": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": "b3GFrfkl5AR4Q9jndybzln"
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 5
},
"_enabled": true,
"__prefab": null,
"_contentSize": {
"__type__": "cc.Size",
"width": 40,
"height": 40
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_id": "a1j/F0wxNK1auaa7mfDSMc"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 5
},
"_enabled": true,
"__prefab": null,
"_customMaterial": null,
"_srcBlendFactor": 2,
"_dstBlendFactor": 4,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_spriteFrame": {
"__uuid__": "20835ba4-6145-4fbc-a58a-051ce700aa3e@f9941",
"__expectedType__": "cc.SpriteFrame"
},
"_type": 0,
"_fillType": 0,
"_sizeMode": 1,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_useGrayscale": false,
"_atlas": null,
"_id": "833OTmgwBFcZXshtGw0Nj0"
},
{
"__type__": "cc.BoxCollider2D",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 5
},
"_enabled": true,
"__prefab": null,
"tag": 0,
"_group": 1,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
"_restitution": 0,
"_offset": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_size": {
"__type__": "cc.Size",
"width": 40,
"height": 40
},
"_id": "81JtKAqqlEkYmvk8K2eLPy"
}
},
{
"__type__": "cc.Node",
"_name": "Player",
"_name": "Weapon",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [
{
"__id__": 10
"__id__": 14
}
],
"_active": true,
"_components": [
{
"__id__": 19
},
{
"__id__": 20
},
{
"__id__": 21
}
@ -364,24 +336,27 @@
"y": 0,
"z": 0
},
"_id": "fbcM/H31RNxK0HvQrwpElL"
"_id": "36M1oczB9EsY/fO80ilIoc"
},
{
"__type__": "cc.Node",
"_name": "Weapon",
"_name": "AnimatedWeapon",
"_objFlags": 0,
"_parent": {
"__id__": 9
"__id__": 13
},
"_children": [
{
"__id__": 11
"__id__": 15
}
],
"_active": true,
"_components": [
{
"__id__": 18
"__id__": 19
},
{
"__id__": 20
}
],
"_prefab": null,
@ -411,20 +386,16 @@
"y": 0,
"z": 0
},
"_id": "36M1oczB9EsY/fO80ilIoc"
"_id": "9dFcDHQYpKPLaHRfcVfzbw"
},
{
"__type__": "cc.Node",
"_name": "AnimatedWeapon",
"_name": "WeaponCollider",
"_objFlags": 0,
"_parent": {
"__id__": 10
"__id__": 14
},
"_children": [
{
"__id__": 12
}
],
"_children": [],
"_active": true,
"_components": [
{
@ -432,55 +403,9 @@
},
{
"__id__": 17
}
],
"_prefab": null,
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": "9dFcDHQYpKPLaHRfcVfzbw"
},
{
"__type__": "cc.Node",
"_name": "Sprite",
"_objFlags": 0,
"_parent": {
"__id__": 11
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 13
},
{
"__id__": 14
},
{
"__id__": 15
"__id__": 18
}
],
"_prefab": null,
@ -517,7 +442,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 12
"__id__": 15
},
"_enabled": true,
"__prefab": null,
@ -538,7 +463,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 12
"__id__": 15
},
"_enabled": true,
"__prefab": null,
@ -576,7 +501,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 12
"__id__": 15
},
"_enabled": true,
"__prefab": null,
@ -603,7 +528,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 11
"__id__": 14
},
"_enabled": true,
"__prefab": null,
@ -624,7 +549,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 11
"__id__": 14
},
"_enabled": true,
"__prefab": null,
@ -646,24 +571,76 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 10
"__id__": 13
},
"_enabled": true,
"__prefab": null,
"weaponAnimation": {
"__id__": 17
"__id__": 20
},
"weaponCollider": {
"__id__": 15
"collider": {
"__id__": 18
},
"_id": "femtdMmA5Ax5K78hjELCcD"
},
{
"__type__": "cc.Node",
"_name": "Player",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 23
},
{
"__id__": 24
},
{
"__id__": 25
},
{
"__id__": 26
}
],
"_prefab": null,
"_lpos": {
"__type__": "cc.Vec3",
"x": -78,
"y": -42,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": "fbcM/H31RNxK0HvQrwpElL"
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 9
"__id__": 22
},
"_enabled": true,
"__prefab": null,
@ -684,7 +661,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 9
"__id__": 22
},
"_enabled": true,
"__prefab": null,
@ -722,16 +699,43 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 9
"__id__": 22
},
"_enabled": true,
"__prefab": null,
"speed": 100,
"weapon": {
"__id__": 18
"collider": {
"__id__": 26
},
"_id": "5aDEGDNWpIaL+U2iIxK4vA"
},
{
"__type__": "cc.BoxCollider2D",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 22
},
"_enabled": true,
"__prefab": null,
"tag": 0,
"_group": 1,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
"_restitution": 0,
"_offset": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_size": {
"__type__": "cc.Size",
"width": 40,
"height": 40
},
"_id": "15Y+yVn0FL9ahaUAsSVwM8"
},
{
"__type__": "cc.Node",
"_name": "UI",
@ -741,19 +745,19 @@
},
"_children": [
{
"__id__": 23
"__id__": 28
},
{
"__id__": 30
"__id__": 35
}
],
"_active": true,
"_components": [
{
"__id__": 33
"__id__": 38
},
{
"__id__": 34
"__id__": 39
}
],
"_prefab": null,
@ -790,23 +794,23 @@
"_name": "VirtualJoystic",
"_objFlags": 0,
"_parent": {
"__id__": 22
"__id__": 27
},
"_children": [
{
"__id__": 24
"__id__": 29
}
],
"_active": true,
"_components": [
{
"__id__": 27
"__id__": 32
},
{
"__id__": 28
"__id__": 33
},
{
"__id__": 29
"__id__": 34
}
],
"_prefab": null,
@ -843,16 +847,16 @@
"_name": "Knob",
"_objFlags": 0,
"_parent": {
"__id__": 23
"__id__": 28
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 25
"__id__": 30
},
{
"__id__": 26
"__id__": 31
}
],
"_prefab": null,
@ -889,7 +893,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 24
"__id__": 29
},
"_enabled": true,
"__prefab": null,
@ -910,7 +914,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 24
"__id__": 29
},
"_enabled": true,
"__prefab": null,
@ -948,7 +952,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 23
"__id__": 28
},
"_enabled": true,
"__prefab": null,
@ -969,7 +973,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 23
"__id__": 28
},
"_enabled": true,
"__prefab": null,
@ -1007,13 +1011,13 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 23
"__id__": 28
},
"_enabled": true,
"__prefab": null,
"maxDistance": 50,
"knob": {
"__id__": 24
"__id__": 29
},
"_id": "50ceZG62tGlYXvZWEvSDKl"
},
@ -1022,16 +1026,16 @@
"_name": "BottomRight",
"_objFlags": 0,
"_parent": {
"__id__": 22
"__id__": 27
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 31
"__id__": 36
},
{
"__id__": 32
"__id__": 37
}
],
"_prefab": null,
@ -1068,7 +1072,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 30
"__id__": 35
},
"_enabled": true,
"__prefab": null,
@ -1089,7 +1093,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 30
"__id__": 35
},
"_enabled": true,
"__prefab": null,
@ -1118,7 +1122,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 22
"__id__": 27
},
"_enabled": true,
"__prefab": null,
@ -1139,7 +1143,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 22
"__id__": 27
},
"_enabled": true,
"__prefab": null,
@ -1238,30 +1242,46 @@
"_enabled": true,
"__prefab": null,
"virtualJoystic": {
"__id__": 29
"__id__": 34
},
"player": {
"__id__": 25
},
"weapon": {
"__id__": 21
},
"enemy": {
"__uuid__": "fc9a455d-1b5f-460e-96eb-7c0ebf07d3d0",
"__expectedType__": "cc.Prefab"
},
"strikeDelay": 2,
"_id": "81HHJkTWZDO60EyXTSS38c"
},
{
"__type__": "cc.PrefabInfo",
"fileId": "cffa5b19-2043-4cef-8221-1a10ab7a2c1f",
"nestedPrefabInstanceRoots": [
{
"__id__": 5
}
]
},
{
"__type__": "cc.SceneGlobals",
"ambient": {
"__id__": 40
"__id__": 46
},
"shadows": {
"__id__": 41
"__id__": 47
},
"_skybox": {
"__id__": 42
"__id__": 48
},
"fog": {
"__id__": 43
"__id__": 49
},
"octree": {
"__id__": 44
"__id__": 50
}
},
{

View File

@ -0,0 +1,22 @@
import { Collider2D, Contact2DType } from "cc";
import { Enemy, IDamageDealing } from "./Enemy";
import { Player } from "./Player";
import { Weapon } from "./Weapon";
export class CollisionSystem {
public constructor(player: Player, weapon: Weapon) {
player.Collider.on(Contact2DType.BEGIN_CONTACT, this.onPlayerContactBegin, this);
weapon.Collider.on(Contact2DType.BEGIN_CONTACT, this.onWeaponContactBegin, this);
}
private onWeaponContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
console.log("Weapon contact! " + otherCollider.node.name);
}
private onPlayerContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
console.log("Player contact! " + otherCollider.node.name);
const damageDealing: IDamageDealing = otherCollider.node.getComponent(Enemy);
console.log("DAMAGE: " + damageDealing.Damage);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ea4efcef-c9f5-4116-b43d-7cae85bf298f",
"files": [],
"subMetas": {},
"userData": {}
}

19
assets/Scripts/Enemy.ts Normal file
View File

@ -0,0 +1,19 @@
import { BoxCollider2D, Component, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("Enemy")
export class Enemy extends Component implements IDamageDealing {
@property(BoxCollider2D) public collider: BoxCollider2D;
public get Collider(): BoxCollider2D {
return this.collider;
}
public get Damage(): number {
return 3;
}
}
export interface IDamageDealing {
Damage: number;
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "e6f969c0-2783-4eb3-b467-356c68db4512",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,20 +1,51 @@
import { Component, _decorator } from "cc";
import { Component, instantiate, Prefab, random, randomRange, Vec3, _decorator } from "cc";
import { CollisionSystem } from "./CollisionSystem";
import { Enemy } from "./Enemy";
import { Player } from "./Player";
import { ObjectPool } from "./Services/ObjectPool";
import { VirtualJoystic } from "./VirtualJoystic";
import { Weapon } from "./Weapon";
const { ccclass, property } = _decorator;
@ccclass("GameBootstrapper")
export class GameBootstrapper extends Component {
@property(VirtualJoystic) private virtualJoystic: VirtualJoystic;
@property(Player) private player: Player;
@property(Weapon) private weapon: Weapon;
@property(Prefab) private enemy: Prefab;
@property(Number) private strikeDelay = 0;
public start(): void {
this.virtualJoystic.init();
this.player.init(this.virtualJoystic, this.strikeDelay);
this.weapon.init(this.strikeDelay);
this.player.init(this.virtualJoystic, this.weapon);
new CollisionSystem(this.player, this.weapon);
const op: ObjectPool<Player> = new ObjectPool(this.enemy, this.node, 10, Player);
const borrowed: Player[] = [];
for (let index = 0; index < 7; index++) {
const enemy: Player = op.borrow();
enemy.node.parent = this.node;
enemy.node.active = true;
enemy.node.setPosition(new Vec3(randomRange(-200, 200)));
if (index < 5) borrowed.push(enemy);
}
borrowed.forEach((borrowedEnemy) => {
op.return(borrowedEnemy);
});
}
public update(deltaTime: number): void {
this.player.gameTick(deltaTime);
}
public getEnemy<T extends Component>(): T {
const i = instantiate(this.enemy);
i.parent = this.node;
return <T>i.getComponent(Enemy.name);
}
}

View File

@ -1,18 +1,26 @@
import { Component, Vec2, Vec3, _decorator } from "cc";
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { VirtualJoystic } from "./VirtualJoystic";
import { Weapon } from "./Weapon";
const { ccclass, property } = _decorator;
@ccclass("Player")
export class Player extends Component {
private virtualJoystic: VirtualJoystic;
@property private speed = 0;
@property(BoxCollider2D) private collider: BoxCollider2D;
@property(Weapon) private weapon: Weapon;
private virtualJoystic: VirtualJoystic;
private weapon: Weapon;
public init(virtualJoystic: VirtualJoystic, strikeDelay: number): void {
public init(virtualJoystic: VirtualJoystic, weapon: Weapon): void {
this.virtualJoystic = virtualJoystic;
this.weapon.init(strikeDelay);
this.weapon = weapon;
this.weapon.node.parent = this.node;
this.weapon.node.setPosition(new Vec3());
}
public get Collider(): Collider2D {
return this.collider;
}
public gameTick(deltaTime: number): void {

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "cebf49b9-55bd-4829-bb97-cb8bd1378a21",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,7 @@
import { Collider2D, IPhysics2DContact } from "cc";
export type ContactParams = {
selfCollider: Collider2D;
otherCollider: Collider2D;
contact: IPhysics2DContact | null;
};

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ee9d41d4-91f4-4d8f-a4ef-7e0df147fb35",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "53ace3f5-848d-4d10-a12a-9f5a5ae681a2",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,4 @@
export interface ISignal<T> {
on(handler: (data: T) => void): void;
off(handler: (data: T) => void): void;
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "626b0371-bbf5-4b70-988e-48e9e3ebb171",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,16 @@
import { ISignal } from "./ISignal";
export class Signal<T> implements ISignal<T> {
private handlers: ((data: T) => void)[] = [];
public on(handler: (data: T) => void): void {
this.handlers.push(handler);
}
public off(handler: (data: T) => void): void {
this.handlers = this.handlers.filter((h) => h !== handler);
}
public trigger(data: T): void {
[...this.handlers].forEach((handler) => handler(data));
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "421ff10c-7e13-4fa7-844a-003fd7e738ca",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,85 @@
import { Component, instantiate, Node, Prefab } from "cc";
export class ObjectPool<T extends Component> {
private prefab: Prefab;
private parent: Node;
private pooledObjects: PooledObject<T>[] = [];
private componentType: { new (): T };
public constructor(prefab: Prefab, parent: Node, defaultPoolCount: number, componentType: { new (): T }) {
this.prefab = prefab;
this.parent = parent;
this.componentType = componentType;
for (let i = 0; i < defaultPoolCount; i++) {
this.pooledObjects.push(this.createNew());
}
}
public borrow(): T {
const objectToBorrow: PooledObject<T> | null = this.pooledObjects.find((o) => !o.IsBorrowed);
if (objectToBorrow != null) {
return objectToBorrow.borrow();
}
return this.createNew().borrow();
}
public return(object: T): void {
const objectToReturn: PooledObject<T> | null = this.pooledObjects.find((o) => o.Equals(object));
if (objectToReturn == null) {
throw new Error("Object " + this.prefab.name + " is not a member of the pool");
}
objectToReturn.return();
}
private createNew(): PooledObject<T> {
const newPooledObject: PooledObject<T> = new PooledObject(this.prefab, this.parent, this.componentType);
this.pooledObjects.push(newPooledObject);
return newPooledObject;
}
}
class PooledObject<T extends Component> {
private isBorrowed = false;
private defaultParent: Node;
private instancedNode: Node;
private instancedComponent: T;
public constructor(prefab: Prefab, defaultParent: Node, componentType: { new (): T }) {
this.defaultParent = defaultParent;
this.instancedNode = instantiate(prefab);
this.instancedComponent = <T>this.instancedNode.getComponent(componentType.name);
if (this.instancedComponent == null) {
throw new Error("Object " + prefab.name + " does not have component " + componentType.name);
}
this.clear();
}
public get IsBorrowed(): boolean {
return this.isBorrowed;
}
public Equals(component: T): boolean {
return this.instancedComponent == component;
}
public borrow(): T {
this.isBorrowed = true;
return this.instancedComponent;
}
public return(): void {
this.clear();
}
private clear(): void {
this.instancedNode.active = false;
this.instancedNode.parent = this.defaultParent;
this.isBorrowed = false;
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "a4ed3b4c-8989-4f08-9303-4d7a3ae90703",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,35 +1,36 @@
import { Animation, BoxCollider2D, Component, Contact2DType, Vec2, Vec3, _decorator } from "cc";
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("Weapon")
export class Weapon extends Component {
@property(Animation) private weaponAnimation: Animation;
@property(BoxCollider2D) private weaponCollider: BoxCollider2D;
@property(BoxCollider2D) private collider: BoxCollider2D;
private strikeDelay: number;
private currentDelay = 0;
public init(strikeDelay: number): void {
this.strikeDelay = strikeDelay;
this.weaponCollider.on(Contact2DType.BEGIN_CONTACT, () => {
console.log("Begin Contact!");
});
}
public gameTick(deltaTime: number, movement: Vec2): void {
this.currentDelay += deltaTime;
if (this.strikeDelay <= this.currentDelay) {
if (this.strikeDelay / 4 <= this.currentDelay) {
this.currentDelay = 0;
this.strike(movement);
}
}
public get Collider(): Collider2D {
return this.collider;
}
private strike(movement: Vec2): void {
const direction: Vec2 = movement.normalize();
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI;
this.node.eulerAngles = new Vec3(0, 0, angle);
this.weaponAnimation.getState("WeaponSwing").speed = 4;
this.weaponAnimation.play("WeaponSwing");
}
}