Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MingSnake_241120
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
SparkProjects
MingSnake_241120
Commits
5454f9ee
Commit
5454f9ee
authored
Nov 22, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
61249a20
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
155 additions
and
227 deletions
+155
-227
MainGame.scene
assets/Bundles/MainGame/MainGame.scene
+4
-4
AISnake.prefab
assets/Bundles/MainGame/prefab/AISnake.prefab
+1
-4
food.prefab
assets/Bundles/MainGame/prefab/food.prefab
+22
-1
AISnake.ts
assets/Scripts/Scenes/MainGame/AISnake.ts
+3
-1
MainGame.ts
assets/Scripts/Scenes/MainGame/MainGame.ts
+8
-7
CommonPool.ts
assets/Scripts/Scenes/MainGame/Manager/CommonPool.ts
+12
-0
CommonPool.ts.meta
assets/Scripts/Scenes/MainGame/Manager/CommonPool.ts.meta
+1
-1
FoodManger.ts
assets/Scripts/Scenes/MainGame/Manager/FoodManger.ts
+36
-22
FoodManger.ts.meta
assets/Scripts/Scenes/MainGame/Manager/FoodManger.ts.meta
+0
-0
PoolManager.ts
assets/Scripts/Scenes/MainGame/Manager/PoolManager.ts
+0
-152
Food.ts
assets/Scripts/Scenes/MainGame/Props/Food.ts
+35
-0
Food.ts.meta
assets/Scripts/Scenes/MainGame/Props/Food.ts.meta
+9
-0
Snake.ts
assets/Scripts/Scenes/MainGame/Snake.ts
+24
-35
No files found.
assets/Bundles/MainGame/MainGame.scene
View file @
5454f9ee
...
@@ -190,7 +190,7 @@
...
@@ -190,7 +190,7 @@
"_priority": 40000,
"_priority": 40000,
"_fov": 45,
"_fov": 45,
"_fovAxis": 0,
"_fovAxis": 0,
"_orthoHeight":
697.1836734693877
,
"_orthoHeight":
810.6237288135594
,
"_near": 0,
"_near": 0,
"_far": 2000,
"_far": 2000,
"_color": {
"_color": {
...
@@ -1737,7 +1737,7 @@
...
@@ -1737,7 +1737,7 @@
"_priority": 50000,
"_priority": 50000,
"_fov": 45,
"_fov": 45,
"_fovAxis": 0,
"_fovAxis": 0,
"_orthoHeight":
697.1836734693877
,
"_orthoHeight":
810.6237288135594
,
"_near": 1,
"_near": 1,
"_far": 2000,
"_far": 2000,
"_color": {
"_color": {
...
@@ -2171,7 +2171,7 @@
...
@@ -2171,7 +2171,7 @@
"__prefab": null,
"__prefab": null,
"_contentSize": {
"_contentSize": {
"__type__": "cc.Size",
"__type__": "cc.Size",
"width": 1624,
"width": 1624
.0000000000002
,
"height": 750
"height": 750
},
},
"_anchorPoint": {
"_anchorPoint": {
...
@@ -2439,7 +2439,7 @@
...
@@ -2439,7 +2439,7 @@
"__prefab": null,
"__prefab": null,
"_contentSize": {
"_contentSize": {
"__type__": "cc.Size",
"__type__": "cc.Size",
"width": 1624,
"width": 1624
.0000000000002
,
"height": 750
"height": 750
},
},
"_anchorPoint": {
"_anchorPoint": {
...
...
assets/Bundles/MainGame/prefab/AISnake.prefab
View file @
5454f9ee
...
@@ -331,10 +331,7 @@
...
@@ -331,10 +331,7 @@
"b": 255,
"b": 255,
"a": 255
"a": 255
},
},
"_spriteFrame": {
"_spriteFrame": null,
"__uuid__": "5a57c4ce-88f6-4582-af09-4eff049952e0@9296f",
"__expectedType__": "cc.SpriteFrame"
},
"_type": 0,
"_type": 0,
"_fillType": 0,
"_fillType": 0,
"_sizeMode": 1,
"_sizeMode": 1,
...
...
assets/Bundles/MainGame/prefab/food.prefab
View file @
5454f9ee
...
@@ -31,10 +31,13 @@
...
@@ -31,10 +31,13 @@
},
},
{
{
"__id__": 8
"__id__": 8
},
{
"__id__": 10
}
}
],
],
"_prefab": {
"_prefab": {
"__id__": 1
0
"__id__": 1
2
},
},
"_lpos": {
"_lpos": {
"__type__": "cc.Vec3",
"__type__": "cc.Vec3",
...
@@ -202,6 +205,24 @@
...
@@ -202,6 +205,24 @@
"__type__": "cc.CompPrefabInfo",
"__type__": "cc.CompPrefabInfo",
"fileId": "bbd/hzdzVJ1rG0lXtNgF7s"
"fileId": "bbd/hzdzVJ1rG0lXtNgF7s"
},
},
{
"__type__": "55174qkRKhBO78kTSc2IvFc",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 11
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "d6rQ0kR1dF0Z5Wb7aK/Nmz"
},
{
{
"__type__": "cc.PrefabInfo",
"__type__": "cc.PrefabInfo",
"root": {
"root": {
...
...
assets/Scripts/Scenes/MainGame/AISnake.ts
View file @
5454f9ee
...
@@ -2,6 +2,7 @@ import { _decorator } from "cc";
...
@@ -2,6 +2,7 @@ import { _decorator } from "cc";
import
{
Snake
}
from
"./Snake"
;
import
{
Snake
}
from
"./Snake"
;
import
{
MainGame
}
from
"./MainGame"
;
import
{
MainGame
}
from
"./MainGame"
;
import
{
AIController
}
from
"./AI/AIController"
;
import
{
AIController
}
from
"./AI/AIController"
;
import
{
aiPool
}
from
"./Manager/CommonPool"
;
const
{
ccclass
}
=
_decorator
;
const
{
ccclass
}
=
_decorator
;
...
@@ -40,7 +41,8 @@ export class AISnake extends Snake {
...
@@ -40,7 +41,8 @@ export class AISnake extends Snake {
super
.
death
();
super
.
death
();
this
.
node
.
removeFromParent
();
this
.
node
.
removeFromParent
();
this
.
destroy
();
aiPool
.
put
(
this
.
node
);
console
.
log
(
MainGame
.
ins
.
animalNode
.
children
.
length
)
console
.
log
(
MainGame
.
ins
.
animalNode
.
children
.
length
)
...
...
assets/Scripts/Scenes/MainGame/MainGame.ts
View file @
5454f9ee
import
{
import
{
_decorator
,
_decorator
,
Camera
,
Camera
,
director
,
director
,
instantiate
,
Label
,
Label
,
math
,
math
,
Node
,
Node
,
...
@@ -11,10 +11,9 @@ import {
...
@@ -11,10 +11,9 @@ import {
}
from
"cc"
;
}
from
"cc"
;
import
{
Joystick
}
from
"./Components/Joystick"
;
import
{
Joystick
}
from
"./Components/Joystick"
;
import
{
FastBtn
}
from
"./Components/FastBtn"
;
import
{
FastBtn
}
from
"./Components/FastBtn"
;
import
{
Fo
ndManger
}
from
"./Manager/Fon
dManger"
;
import
{
Fo
odManger
}
from
"./Manager/Foo
dManger"
;
import
{
Global
}
from
"./Global"
;
import
{
Global
}
from
"./Global"
;
import
{
Events
,
GameState
}
from
"./Common/Enums"
;
import
{
Events
,
GameState
}
from
"./Common/Enums"
;
import
{
PoolManager
}
from
"./Manager/PoolManager"
;
import
{
showToast
}
from
"../../../Module/UIFast"
;
import
{
showToast
}
from
"../../../Module/UIFast"
;
import
Scene
from
"../../../Module/Scene"
;
import
Scene
from
"../../../Module/Scene"
;
import
{
executePreFrame
,
getItemGenerator
}
from
"../../Utils/ExecutePreFrame"
;
import
{
executePreFrame
,
getItemGenerator
}
from
"../../Utils/ExecutePreFrame"
;
...
@@ -22,6 +21,7 @@ import { Player } from "./Player";
...
@@ -22,6 +21,7 @@ import { Player } from "./Player";
import
{
AISnake
}
from
"./AISnake"
;
import
{
AISnake
}
from
"./AISnake"
;
import
{
Quadtree
}
from
"./QuadTree/QuadTree"
;
import
{
Quadtree
}
from
"./QuadTree/QuadTree"
;
import
{
QuadTreeNode
}
from
"./QuadTree/QuadTreeNode"
;
import
{
QuadTreeNode
}
from
"./QuadTree/QuadTreeNode"
;
import
{
aiPool
,
clearAllPool
,
foodPool
}
from
"./Manager/CommonPool"
;
const
{
ccclass
,
property
}
=
_decorator
;
const
{
ccclass
,
property
}
=
_decorator
;
...
@@ -61,8 +61,8 @@ export class MainGame extends Scene {
...
@@ -61,8 +61,8 @@ export class MainGame extends Scene {
@
property
(
Player
)
@
property
(
Player
)
player
:
Player
=
null
;
player
:
Player
=
null
;
@
property
(
Fo
n
dManger
)
@
property
(
Fo
o
dManger
)
fondManger
:
Fo
n
dManger
=
null
;
fondManger
:
Fo
o
dManger
=
null
;
@
property
(
Node
)
@
property
(
Node
)
animalNode
:
Node
=
null
;
animalNode
:
Node
=
null
;
...
@@ -139,6 +139,7 @@ export class MainGame extends Scene {
...
@@ -139,6 +139,7 @@ export class MainGame extends Scene {
onDestroy
()
{
onDestroy
()
{
MainGame
.
_ins
=
null
;
MainGame
.
_ins
=
null
;
clearAllPool
();
}
}
update
(
dt
:
number
)
{
update
(
dt
:
number
)
{
...
@@ -240,7 +241,7 @@ export class MainGame extends Scene {
...
@@ -240,7 +241,7 @@ export class MainGame extends Scene {
}
}
initItem
=
(
index
:
number
)
=>
{
initItem
=
(
index
:
number
)
=>
{
const
node
=
PoolManager
.
instance
.
getNod
e
(
this
.
animalPrefab
);
const
node
=
aiPool
.
get
()
||
instantiat
e
(
this
.
animalPrefab
);
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_HEIGHT
/
2
-
50
),
Global
.
MAP_HEIGHT
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_HEIGHT
/
2
-
50
),
Global
.
MAP_HEIGHT
/
2
-
50
);
...
@@ -248,7 +249,7 @@ export class MainGame extends Scene {
...
@@ -248,7 +249,7 @@ export class MainGame extends Scene {
x
,
y
,
x
,
y
,
// x: 50,
// x: 50,
// y: 50,
// y: 50,
bodyCount
:
100
,
//
bodyCount: 100,
angle
:
math
.
randomRangeInt
(
0
,
360
),
angle
:
math
.
randomRangeInt
(
0
,
360
),
skinName
:
"default"
,
skinName
:
"default"
,
});
});
...
...
assets/Scripts/Scenes/MainGame/Manager/CommonPool.ts
0 → 100644
View file @
5454f9ee
import
{
NodePool
}
from
"cc"
;
export
const
foodPool
=
new
NodePool
();
export
const
bodyPool
=
new
NodePool
();
export
const
aiPool
=
new
NodePool
();
export
function
clearAllPool
()
{
foodPool
.
clear
();
bodyPool
.
clear
();
aiPool
.
clear
();
}
\ No newline at end of file
assets/Scripts/Scenes/MainGame/Manager/
PoolManager
.ts.meta
→
assets/Scripts/Scenes/MainGame/Manager/
CommonPool
.ts.meta
View file @
5454f9ee
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
"ver": "4.0.24",
"ver": "4.0.24",
"importer": "typescript",
"importer": "typescript",
"imported": true,
"imported": true,
"uuid": "
bdf18a2f-1ee1-4fcd-bcf5-e17972dee33d
",
"uuid": "
f68fa372-122a-4b96-89ea-35313b0129a6
",
"files": [],
"files": [],
"subMetas": {},
"subMetas": {},
"userData": {}
"userData": {}
...
...
assets/Scripts/Scenes/MainGame/Manager/Fo
n
dManger.ts
→
assets/Scripts/Scenes/MainGame/Manager/Fo
o
dManger.ts
View file @
5454f9ee
import
{
_decorator
,
Enum
,
SpriteFrame
,
math
,
v3
,
Sprite
,
Collider2D
,
Component
,
Prefab
,
ccenum
}
from
"cc"
;
import
{
import
{
PoolManager
}
from
"db://assets/Scripts/Scenes/MainGame/Manager/PoolManager"
;
_decorator
,
SpriteFrame
,
math
,
Sprite
,
Collider2D
,
Component
,
Prefab
,
instantiate
,
Vec2
}
from
"cc"
;
import
{
FoodType
}
from
"db://assets/Scripts/Scenes/MainGame/Common/Enums"
;
import
{
FoodType
}
from
"db://assets/Scripts/Scenes/MainGame/Common/Enums"
;
import
{
Global
}
from
"db://assets/Scripts/Scenes/MainGame/Global"
;
import
{
Global
}
from
"db://assets/Scripts/Scenes/MainGame/Global"
;
import
{
executePreFrame
,
getItemGenerator
}
from
"db://assets/Scripts/Utils/ExecutePreFrame"
;
import
{
executePreFrame
,
getItemGenerator
}
from
"db://assets/Scripts/Utils/ExecutePreFrame"
;
import
{
Food
}
from
"../Props/Food"
;
import
{
foodPool
}
from
"./CommonPool"
;
const
{
ccclass
,
property
}
=
_decorator
;
const
{
ccclass
,
property
}
=
_decorator
;
/**
/**
* 食物管理器
* 食物管理器
*/
*/
@
ccclass
(
"Fo
n
dManger"
)
@
ccclass
(
"Fo
o
dManger"
)
export
class
Fo
n
dManger
extends
Component
{
export
class
Fo
o
dManger
extends
Component
{
@
property
({
type
:
[
SpriteFrame
]
})
@
property
({
type
:
[
SpriteFrame
]
})
private
foods
:
SpriteFrame
[]
=
[];
private
foods
:
SpriteFrame
[]
=
[];
...
@@ -20,6 +30,9 @@ export class FondManger extends Component {
...
@@ -20,6 +30,9 @@ export class FondManger extends Component {
maxFood
:
number
=
100
;
maxFood
:
number
=
100
;
onLoad
()
{
}
/**
/**
* 获取当前食物数量
* 获取当前食物数量
*/
*/
...
@@ -32,9 +45,7 @@ export class FondManger extends Component {
...
@@ -32,9 +45,7 @@ export class FondManger extends Component {
*/
*/
async
init
(
maxFood
:
number
=
100
)
{
async
init
(
maxFood
:
number
=
100
)
{
this
.
maxFood
=
maxFood
;
this
.
maxFood
=
maxFood
;
await
executePreFrame
(
getItemGenerator
(
maxFood
,
this
.
initItem
),
1
,
this
);
await
this
.
initFond
(
maxFood
);
this
.
schedule
(
this
.
checkFood
,
1
);
this
.
schedule
(
this
.
checkFood
,
1
);
}
}
...
@@ -50,7 +61,7 @@ export class FondManger extends Component {
...
@@ -50,7 +61,7 @@ export class FondManger extends Component {
/**
/**
* 设置食物
* 设置食物
*/
*/
addFood
(
x
?:
number
,
y
?:
number
)
{
addFood
(
x
?:
number
,
y
?:
number
,
energy
:
number
=
1
)
{
// 如果没有指定位置,随机生成位置
// 如果没有指定位置,随机生成位置
if
(
!
x
)
{
if
(
!
x
)
{
...
@@ -61,26 +72,25 @@ export class FondManger extends Component {
...
@@ -61,26 +72,25 @@ export class FondManger extends Component {
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_HEIGHT
/
2
-
50
),
Global
.
MAP_HEIGHT
/
2
-
50
);
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_HEIGHT
/
2
-
50
),
Global
.
MAP_HEIGHT
/
2
-
50
);
}
}
// 从对象池获取食物节点
const
food
=
foodPool
.
get
()
||
instantiate
(
this
.
foodPrefab
);
const
node
=
PoolManager
.
instance
.
getNode
(
this
.
foodPrefab
);
// 随机选择食物类型
// 随机选择食物类型
const
index
=
math
.
randomRangeInt
(
0
,
this
.
foods
.
length
);
const
index
=
math
.
randomRangeInt
(
0
,
this
.
foods
.
length
);
this
.
node
.
addChild
(
node
);
// 设置食物属性
// 设置食物属性
node
.
angle
=
math
.
randomRange
(
0
,
360
);
food
.
angle
=
math
.
randomRange
(
0
,
360
);
node
.
setScale
(
1
,
1
);
food
.
setPosition
(
x
,
y
);
node
.
setPosition
(
x
,
y
);
food
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
foods
[
index
];
node
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
foods
[
index
];
food
.
getComponent
(
Food
).
init
(
energy
);
this
.
node
.
addChild
(
food
);
// 设置碰撞类型
// 设置碰撞类型
const
collider
=
node
.
getComponent
(
Collider2D
);
const
collider
=
food
.
getComponent
(
Collider2D
);
if
(
collider
)
{
if
(
collider
)
{
collider
.
tag
=
FoodType
.
FOOD
;
collider
.
tag
=
FoodType
.
FOOD
;
}
}
node
.
active
=
true
;
food
.
active
=
true
;
}
}
/**
/**
...
@@ -94,11 +104,15 @@ export class FondManger extends Component {
...
@@ -94,11 +104,15 @@ export class FondManger extends Component {
this
.
addFood
(
x
,
y
);
this
.
addFood
(
x
,
y
);
};
};
/**
async
initFoods
(
infoArr
:
{
x
:
number
,
y
:
number
,
energy
:
number
}[])
{
* 初始化食物
*/
const
initItem
=
(
index
:
number
)
=>
{
async
initFond
(
count
:
number
)
{
const
bp
=
infoArr
[
index
];
await
executePreFrame
(
getItemGenerator
(
count
,
this
.
initItem
),
1
,
this
);
this
.
addFood
(
bp
.
x
,
bp
.
y
,
bp
.
energy
);
};
await
executePreFrame
(
getItemGenerator
(
infoArr
.
length
,
initItem
),
1
,
this
);
}
}
}
}
assets/Scripts/Scenes/MainGame/Manager/Fo
n
dManger.ts.meta
→
assets/Scripts/Scenes/MainGame/Manager/Fo
o
dManger.ts.meta
View file @
5454f9ee
File moved
assets/Scripts/Scenes/MainGame/Manager/PoolManager.ts
deleted
100644 → 0
View file @
61249a20
import
{
_decorator
,
instantiate
,
Node
,
NodePool
,
Prefab
,
Vec3
}
from
"cc"
;
const
{
ccclass
}
=
_decorator
;
/**
* 对象池管理器
*/
@
ccclass
(
"PoolManager"
)
export
class
PoolManager
{
/** 单例实例 */
private
static
_instance
:
PoolManager
;
/** 对象池字典 */
private
_dictPool
:
{
[
key
:
string
]:
NodePool
}
=
{};
/** 预制体字典 */
private
_dictPrefab
:
{
[
key
:
string
]:
Node
}
=
{};
/** 获取单例 */
public
static
get
instance
():
PoolManager
{
if
(
!
this
.
_instance
)
{
this
.
_instance
=
new
PoolManager
();
}
return
this
.
_instance
;
}
/**
* 复制节点
* @param sourceNode 源节点
* @param parent 父节点
* @returns 复制的节点
*/
public
copyNode
(
sourceNode
:
Node
,
parent
?:
Node
):
Node
{
const
name
=
sourceNode
.
name
;
this
.
_dictPrefab
[
name
]
=
sourceNode
;
let
node
=
null
;
if
(
this
.
_dictPool
.
hasOwnProperty
(
name
))
{
// 从对象池获取
const
pool
=
this
.
_dictPool
[
name
];
node
=
pool
.
size
()
>
0
?
pool
.
get
()
:
instantiate
(
sourceNode
);
}
else
{
// 创建新的对象池
const
pool
=
new
NodePool
();
this
.
_dictPool
[
name
]
=
pool
;
node
=
instantiate
(
sourceNode
);
}
if
(
parent
)
{
node
.
parent
=
parent
;
node
.
active
=
true
;
}
return
node
;
}
/**
* 获取节点
* @param prefab 预制体或预制体名称
* @param parent 父节点
* @param position 位置
* @returns 节点实例
*/
public
getNode
(
prefab
:
Node
|
Prefab
|
string
,
parent
?:
Node
,
position
?:
Vec3
):
Node
{
let
prefabNode
:
Node
|
Prefab
;
let
prefabName
:
string
;
if
(
typeof
prefab
===
"string"
)
{
prefabName
=
prefab
;
prefabNode
=
this
.
_dictPrefab
[
prefab
];
if
(
!
prefabNode
)
{
console
.
log
(
"Pool invalid prefab name = "
,
prefabName
);
return
null
;
}
}
else
{
prefabNode
=
prefab
;
prefabName
=
prefab
[
"data"
].
name
;
}
let
node
=
null
;
if
(
this
.
_dictPool
.
hasOwnProperty
(
prefabName
))
{
// 从对象池获取
const
pool
=
this
.
_dictPool
[
prefabName
];
node
=
pool
.
size
()
>
0
?
pool
.
get
()
:
instantiate
(
prefabNode
);
}
else
{
// 创建新的对象池
this
.
_dictPool
[
prefabName
]
=
new
NodePool
();
node
=
instantiate
(
prefabNode
);
}
if
(
parent
)
{
node
.
parent
=
parent
;
node
.
active
=
true
;
if
(
position
)
{
node
.
position
=
position
;
}
}
return
node
;
}
/**
* 回收节点
* @param node 要回收的节点
*/
public
putNode
(
node
:
Node
):
void
{
if
(
!
node
)
{
console
.
log
(
"putNode-node is null"
);
return
;
}
const
name
=
node
.
name
;
let
pool
:
NodePool
=
null
;
if
(
this
.
_dictPool
.
hasOwnProperty
(
name
))
{
pool
=
this
.
_dictPool
[
name
];
}
else
{
pool
=
new
NodePool
();
this
.
_dictPool
[
name
]
=
pool
;
}
pool
.
put
(
node
);
}
/**
* 清空指定名称的对象池
* @param name 对象池名称
*/
public
clearPool
(
name
:
string
):
void
{
if
(
this
.
_dictPool
.
hasOwnProperty
(
name
))
{
this
.
_dictPool
[
name
].
clear
();
}
}
/**
* 设置预制体
* @param name 预制体名称
* @param prefab 预制体
*/
public
setPrefab
(
name
:
string
,
prefab
:
Node
):
void
{
this
.
_dictPrefab
[
name
]
=
prefab
;
}
/**
* 获取预制体
* @param name 预制体名称
* @returns 预制体
*/
public
getPrefab
(
name
:
string
):
Node
{
return
this
.
_dictPrefab
[
name
];
}
}
\ No newline at end of file
assets/Scripts/Scenes/MainGame/Props/Food.ts
0 → 100644
View file @
5454f9ee
import
{
_decorator
,
Component
}
from
"cc"
;
import
{
foodPool
}
from
"../Manager/CommonPool"
;
const
{
ccclass
,
property
}
=
_decorator
;
@
ccclass
(
"Food"
)
export
class
Food
extends
Component
{
_energy
:
number
=
1
;
@
property
get
energy
()
{
return
this
.
_energy
;
}
set
energy
(
energy
:
number
)
{
this
.
_energy
=
energy
;
const
scale
=
1
+
(
energy
-
1
)
/
10
;
this
.
node
.
scale
.
set
(
scale
,
scale
);
}
init
(
energy
:
number
=
1
)
{
this
.
energy
=
energy
;
}
protected
start
()
{
}
recycle
()
{
this
.
node
.
removeFromParent
();
foodPool
.
put
(
this
.
node
);
}
}
\ No newline at end of file
assets/Scripts/Scenes/MainGame/Props/Food.ts.meta
0 → 100644
View file @
5454f9ee
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "55174aa4-44a8-413b-bf24-4d273622f15c",
"files": [],
"subMetas": {},
"userData": {}
}
assets/Scripts/Scenes/MainGame/Snake.ts
View file @
5454f9ee
...
@@ -2,7 +2,7 @@ import {
...
@@ -2,7 +2,7 @@ import {
_decorator
,
_decorator
,
Collider2D
,
Collider2D
,
Component
,
Component
,
Contact2DType
,
Contact2DType
,
instantiate
,
IVec2Like
,
math
,
math
,
Node
,
Node
,
PhysicsGroup
,
PhysicsGroup
,
...
@@ -11,15 +11,15 @@ import {
...
@@ -11,15 +11,15 @@ import {
SpriteFrame
,
SpriteFrame
,
tween
,
UITransform
,
tween
,
UITransform
,
v2
,
v2
,
v3
,
v3
,
Vec2
,
Vec3
,
Vec3
,
}
from
"cc"
;
}
from
"cc"
;
import
{
FoodType
}
from
"./Common/Enums"
;
import
{
FoodType
}
from
"./Common/Enums"
;
import
{
Global
}
from
"./Global"
;
import
{
Global
}
from
"./Global"
;
import
{
PoolManager
}
from
"./Manager/PoolManager"
;
import
{
isIntersect
,
loadSkin
}
from
"./utils/uitl"
;
import
{
isIntersect
,
loadSkin
}
from
"./utils/uitl"
;
import
{
MainGame
}
from
"./MainGame"
;
import
{
MainGame
}
from
"./MainGame"
;
import
{
executePreFrame
,
getItemGenerator
}
from
"../../Utils/ExecutePreFrame"
;
import
{
bodyPool
}
from
"./Manager/CommonPool"
;
import
{
Food
}
from
"./Props/Food"
;
const
{
ccclass
,
property
}
=
_decorator
;
const
{
ccclass
,
property
}
=
_decorator
;
...
@@ -32,7 +32,6 @@ export interface IInitConfig {
...
@@ -32,7 +32,6 @@ export interface IInitConfig {
bodyCount
?:
number
;
bodyCount
?:
number
;
}
}
@
ccclass
(
"Snake"
)
@
ccclass
(
"Snake"
)
export
class
Snake
extends
Component
{
export
class
Snake
extends
Component
{
...
@@ -96,7 +95,7 @@ export class Snake extends Component {
...
@@ -96,7 +95,7 @@ export class Snake extends Component {
// 创建身体节点
// 创建身体节点
for
(
let
i
=
0
;
i
<
bodyCount
;
i
++
)
{
for
(
let
i
=
0
;
i
<
bodyCount
;
i
++
)
{
const
body
=
PoolManager
.
instance
.
getNode
(
this
.
bodyPrefab
,
this
.
node
);
const
body
=
bodyPool
.
get
()
||
instantiate
(
this
.
bodyPrefab
);
const
collider
=
body
.
getComponent
(
Collider2D
);
const
collider
=
body
.
getComponent
(
Collider2D
);
collider
.
tag
=
this
.
tag
;
collider
.
tag
=
this
.
tag
;
...
@@ -104,6 +103,8 @@ export class Snake extends Component {
...
@@ -104,6 +103,8 @@ export class Snake extends Component {
body
.
setPosition
(
-
99999
,
-
99999
);
body
.
setPosition
(
-
99999
,
-
99999
);
body
.
setScale
(
scale
,
scale
);
body
.
setScale
(
scale
,
scale
);
this
.
node
.
addChild
(
body
);
// 设置身体部分的贴图
// 设置身体部分的贴图
if
(
i
==
bodyCount
-
1
&&
this
.
imgTail
)
{
if
(
i
==
bodyCount
-
1
&&
this
.
imgTail
)
{
body
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
imgTail
;
body
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
imgTail
;
...
@@ -148,7 +149,7 @@ export class Snake extends Component {
...
@@ -148,7 +149,7 @@ export class Snake extends Component {
// 碰撞检测
// 碰撞检测
private
onBeginHead
(
selfCollider
:
Collider2D
,
otherCollider
:
Collider2D
)
{
private
onBeginHead
(
selfCollider
:
Collider2D
,
otherCollider
:
Collider2D
)
{
if
(
otherCollider
.
group
===
PhysicsGroup
[
"Body"
]
&&
otherCollider
.
tag
!=
this
.
tag
)
{
if
(
otherCollider
.
group
===
PhysicsGroup
[
"Body"
]
&&
otherCollider
.
tag
!=
this
.
tag
)
{
//
this.death();
this
.
death
();
}
}
}
}
...
@@ -163,7 +164,7 @@ export class Snake extends Component {
...
@@ -163,7 +164,7 @@ export class Snake extends Component {
scale
:
v3
(
0
,
0
)
scale
:
v3
(
0
,
0
)
})
})
.
call
(()
=>
{
.
call
(()
=>
{
PoolManager
.
instance
.
putNode
(
otherCollider
.
node
);
otherCollider
.
node
.
getComponent
(
Food
).
recycle
(
);
if
(
!
this
.
isLife
)
return
;
if
(
!
this
.
isLife
)
return
;
...
@@ -197,10 +198,12 @@ export class Snake extends Component {
...
@@ -197,10 +198,12 @@ export class Snake extends Component {
if
(
this
.
imgTail
)
{
if
(
this
.
imgTail
)
{
len
-=
1
;
len
-=
1
;
}
}
const
newBody
=
PoolManager
.
instance
.
getNode
(
this
.
bodyPrefab
,
this
.
node
);
const
newBody
=
bodyPool
.
get
()
||
instantiate
(
this
.
bodyPrefab
);
newBody
.
angle
=
this
.
bodyArr
[
len
-
1
].
angle
;
newBody
.
angle
=
this
.
bodyArr
[
len
-
1
].
angle
;
newBody
.
setPosition
(
this
.
bodyArr
[
len
-
1
].
getPosition
());
newBody
.
setPosition
(
this
.
bodyArr
[
len
-
1
].
getPosition
());
newBody
.
setScale
(
this
.
scale
,
this
.
scale
);
newBody
.
setScale
(
this
.
scale
,
this
.
scale
);
this
.
node
.
addChild
(
newBody
);
newBody
.
getComponent
(
Sprite
).
spriteFrame
=
len
%
2
==
0
?
this
.
imgBody1
:
this
.
imgBody2
;
newBody
.
getComponent
(
Sprite
).
spriteFrame
=
len
%
2
==
0
?
this
.
imgBody1
:
this
.
imgBody2
;
newBody
.
getComponent
(
Collider2D
).
tag
=
this
.
tag
;
newBody
.
getComponent
(
Collider2D
).
tag
=
this
.
tag
;
...
@@ -319,8 +322,18 @@ export class Snake extends Component {
...
@@ -319,8 +322,18 @@ export class Snake extends Component {
this
.
isLife
=
false
;
this
.
isLife
=
false
;
this
.
node
.
active
=
false
;
this
.
node
.
active
=
false
;
this
.
initFond
(
this
.
bodyArr
.
length
);
const
foodArr
=
this
.
bodyArr
.
map
((
body
)
=>
{
body
.
removeFromParent
();
bodyPool
.
put
(
body
);
// TODO 计算能量
return
{
x
:
body
.
position
.
x
,
y
:
body
.
position
.
y
,
energy
:
1
,
};
});
MainGame
.
ins
.
fondManger
.
initFoods
(
foodArr
);
}
}
protected
getNewPos
(
angle
:
number
,
dt
:
number
,
currentPos
:
Vec3
,
speed
:
number
=
this
.
speed
):
Vec3
{
protected
getNewPos
(
angle
:
number
,
dt
:
number
,
currentPos
:
Vec3
,
speed
:
number
=
this
.
speed
):
Vec3
{
...
@@ -333,30 +346,6 @@ export class Snake extends Component {
...
@@ -333,30 +346,6 @@ export class Snake extends Component {
return
v2
(
Math
.
cos
(
radian
),
Math
.
sin
(
radian
));
return
v2
(
Math
.
cos
(
radian
),
Math
.
sin
(
radian
));
}
}
/**
* 初始化食物
*/
initItem
=
(
index
:
number
)
=>
{
const
bp
=
this
.
bodyArr
[
index
].
getPosition
();
MainGame
.
ins
.
fondManger
.
addFood
(
math
.
randomRangeInt
(
bp
.
x
-
5
,
bp
.
x
+
5
),
math
.
randomRangeInt
(
bp
.
y
-
5
,
bp
.
y
+
5
)
);
this
.
bodyArr
[
index
].
setPosition
(
9999
,
9999
);
this
.
bodyArr
[
index
].
active
=
false
;
};
/**
* 初始化食物
*/
async
initFond
(
count
:
number
)
{
console
.
log
(
count
);
await
executePreFrame
(
getItemGenerator
(
count
,
this
.
initItem
),
1
,
this
);
this
.
ready
=
true
;
}
/**
/**
* 护盾时间
* 护盾时间
* 假如有两个护盾同时生效,则取时间大的,正是因为这样,才设计成数字
* 假如有两个护盾同时生效,则取时间大的,正是因为这样,才设计成数字
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment