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
1c3ec0d6
Commit
1c3ec0d6
authored
Nov 19, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
d973657c
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
244 additions
and
459 deletions
+244
-459
MainGame.scene
assets/Bundles/MainGame/MainGame.scene
+2
-2
Scene.ts
assets/Module/Scene.ts
+2
-2
Animal.ts
assets/Scripts/Scenes/MainGame/Animal.ts
+12
-12
GamePlayer.ts
assets/Scripts/Scenes/MainGame/GamePlayer.ts
+0
-253
GamePlayer.ts.meta
assets/Scripts/Scenes/MainGame/GamePlayer.ts.meta
+0
-9
Global.ts
assets/Scripts/Scenes/MainGame/Global.ts
+0
-20
MainGame.ts
assets/Scripts/Scenes/MainGame/MainGame.ts
+222
-85
MainGame.ts.meta
assets/Scripts/Scenes/MainGame/MainGame.ts.meta
+1
-1
Snake.ts
assets/Scripts/Scenes/MainGame/Snake.ts
+5
-4
Snake1.ts
assets/Scripts/Scenes/MainGame/Snake1.ts
+0
-62
Snake1.ts.meta
assets/Scripts/Scenes/MainGame/Snake1.ts.meta
+0
-9
No files found.
assets/Bundles/MainGame/MainGame.scene
View file @
1c3ec0d6
...
...
@@ -1275,7 +1275,7 @@
"_id": "4b4n24QAlBAJTbqsXR4lUh"
},
{
"__type__": "
21251JLe89C+bNR2HvMGDkB
",
"__type__": "
82c44Ork3tL+o56WmoE8TcQ
",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
...
...
@@ -1314,7 +1314,7 @@
"camera": {
"__id__": 4
},
"_id": "
17e87LmmFPGJM7y37eKcPY
"
"_id": "
212vDeP7dNBZXi0fJjPuCy
"
},
{
"__type__": "4552b9T1AJHJ4xMNlpYdqL/",
...
...
assets/Module/Scene.ts
View file @
1c3ec0d6
...
...
@@ -6,7 +6,7 @@ const {ccclass, property} = _decorator;
@
ccclass
()
export
default
class
Scene
extends
Module
{
@
property
(
UILayer
)
uiLayer
:
UILayer
=
null
;
//
@property(UILayer)
//
uiLayer: UILayer = null;
}
assets/Scripts/Scenes/MainGame/Animal.ts
View file @
1c3ec0d6
...
...
@@ -7,7 +7,7 @@ import { Global } from "./Global";
import
{
PoolManager
}
from
"./PoolManager"
;
import
{
FoodType
,
DirectionType
}
from
"./Enums"
;
import
{
isIntersect
}
from
"./uitl"
;
import
{
GamePlayer
}
from
"./GamePlayer
"
;
import
{
MainGame
}
from
"./MainGame
"
;
const
{
ccclass
,
property
}
=
_decorator
;
...
...
@@ -101,7 +101,7 @@ export class Animal extends Component {
this
.
Head
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
imgHead
;
this
.
Head
.
active
=
isIntersect
(
this
.
Head
.
getPosition
(),
GamePlayer
.
ins
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -148,7 +148,7 @@ export class Animal extends Component {
}
this
.
uiSanke
=
this
.
getComponent
(
UITransform
);
this
.
uiFondManger
=
Global
.
gamePlayer
.
fondManger
.
node
.
getComponent
(
UITransform
);
this
.
uiFondManger
=
MainGame
.
ins
.
fondManger
.
node
.
getComponent
(
UITransform
);
}
onDisable
()
{
...
...
@@ -247,7 +247,7 @@ export class Animal extends Component {
body
.
active
=
isIntersect
(
body
.
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -280,7 +280,7 @@ export class Animal extends Component {
this
.
Bodys
[
i
].
setSiblingIndex
(
len
-
i
);
this
.
Bodys
[
i
].
active
=
isIntersect
(
this
.
Bodys
[
i
].
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -293,7 +293,7 @@ export class Animal extends Component {
this
.
Bodys
[
1
].
setSiblingIndex
(
len
-
1
);
this
.
Bodys
[
1
].
active
=
isIntersect
(
this
.
Bodys
[
1
].
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -305,7 +305,7 @@ export class Animal extends Component {
this
.
Bodys
[
0
].
setSiblingIndex
(
len
);
this
.
Bodys
[
0
].
active
=
isIntersect
(
this
.
Bodys
[
0
].
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -318,7 +318,7 @@ export class Animal extends Component {
this
.
Head
.
setSiblingIndex
(
len
+
1
);
this
.
Head
.
active
=
isIntersect
(
this
.
Head
.
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -340,7 +340,7 @@ export class Animal extends Component {
this
.
Bodys
[
i
].
setSiblingIndex
(
len
-
i
);
this
.
Bodys
[
i
].
active
=
isIntersect
(
this
.
Bodys
[
i
].
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -353,7 +353,7 @@ export class Animal extends Component {
this
.
Bodys
[
0
].
setSiblingIndex
(
len
);
this
.
Bodys
[
0
].
active
=
isIntersect
(
this
.
Bodys
[
0
].
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -366,7 +366,7 @@ export class Animal extends Component {
this
.
Head
.
setSiblingIndex
(
len
+
1
);
this
.
Head
.
active
=
isIntersect
(
this
.
Head
.
getPosition
(),
Global
.
gamePlayer
.
camera
.
node
.
getPosition
(),
MainGame
.
ins
.
camera
.
node
.
getPosition
(),
this
.
vw
,
this
.
vh
);
...
...
@@ -486,7 +486,7 @@ export class Animal extends Component {
*/
private
_initItem
(
index
:
number
)
{
this
.
b
=
this
.
Bodys
[
index
].
getPosition
();
Global
.
gamePlayer
.
fondManger
.
setFood
(
MainGame
.
ins
.
fondManger
.
setFood
(
math
.
randomRangeInt
(
this
.
b
.
x
-
10
,
this
.
b
.
x
+
11
),
math
.
randomRangeInt
(
this
.
b
.
y
-
20
,
this
.
b
.
y
+
21
)
);
...
...
assets/Scripts/Scenes/MainGame/GamePlayer.ts
deleted
100644 → 0
View file @
d973657c
import
{
_decorator
,
UITransform
,
Node
,
Label
,
Camera
,
director
,
math
,
Component
,
Prefab
,
PhysicsSystem2D
,
PhysicsGroup
}
from
"cc"
;
import
{
AngleBtn
}
from
"./AngleBtn"
;
import
{
Snake
}
from
"./Snake"
;
import
{
FastBtn
}
from
"./FastBtn"
;
import
{
Animal
}
from
"./Animal"
;
import
{
FondManger
}
from
"./FondManger"
;
import
{
Config
}
from
"./Config"
;
import
{
Global
}
from
"./Global"
;
import
{
GameState
,
Events
}
from
"./Enums"
;
import
{
PoolManager
}
from
"./PoolManager"
;
import
{
showToast
}
from
"../../../Module/UIFast"
;
const
{
ccclass
,
property
}
=
_decorator
;
@
ccclass
(
"GamePlayer"
)
export
class
GamePlayer
extends
Component
{
@
property
(
Prefab
)
private
animalPrefab
:
Prefab
=
null
;
@
property
({
displayName
:
"最多食物"
,
tooltip
:
"地图上随机产生食物,如果超过当前值不在产生。"
})
private
maxFood
:
number
=
200
;
@
property
({
displayName
:
"NPC数量"
,
tooltip
:
"当前游戏最大NPC角色"
})
private
maxAnimal
:
number
=
20
;
@
property
({
type
:
AngleBtn
,
displayName
:
"旋转按钮"
})
private
angleBtn
:
AngleBtn
=
null
;
@
property
({
type
:
FastBtn
,
displayName
:
"快进按钮"
})
private
fashBtn
:
FastBtn
=
null
;
@
property
(
UITransform
)
private
uiBg
:
UITransform
=
null
;
@
property
(
Snake
)
private
snake
:
Snake
=
null
;
@
property
(
FondManger
)
private
fondManger
:
FondManger
=
null
;
@
property
(
Node
)
private
animalNode
:
Node
=
null
;
@
property
(
Label
)
private
LTips
:
Label
=
null
;
@
property
(
Camera
)
camera
:
Camera
=
null
;
private
state
:
GameState
=
GameState
.
READY
;
private
rebirthSum
:
number
=
0
;
private
static
_ins
:
GamePlayer
=
null
;
static
get
ins
():
GamePlayer
{
return
GamePlayer
.
_ins
;
}
onLoad
()
{
GamePlayer
.
_ins
=
this
;
PhysicsSystem2D
.
instance
.
enable
=
true
;
Global
.
gamePlayer
=
this
;
Global
.
MAP_WIDTH
=
this
.
uiBg
.
contentSize
.
x
;
Global
.
MAP_HIGHT
=
this
.
uiBg
.
contentSize
.
y
;
// 初始化墙壁
const
wallTop
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_TOP"
);
wallTop
.
setPosition
(
0
,
Global
.
MAP_HIGHT
/
2
);
wallTop
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
const
wallBottom
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_BOTTOM"
);
wallBottom
.
setPosition
(
0
,
-
Global
.
MAP_HIGHT
/
2
);
wallBottom
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
const
wallLeft
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_LEFT"
);
wallLeft
.
setPosition
(
-
Global
.
MAP_WIDTH
/
2
,
0
);
wallLeft
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_HIGHT
;
const
wallRight
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_RIGHT"
);
wallRight
.
setPosition
(
Global
.
MAP_WIDTH
/
2
,
0
);
wallRight
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_HIGHT
;
// 初始化蛇
const
skinId
=
Global
.
skinId
;
this
.
snake
.
init
(
0
,
0
,
0
,
skinId
,
0.2
,
Config
.
SKIN_STYLE
[
skinId
].
len
,
Config
.
SKIN_STYLE
[
skinId
].
taitLen
,
0
);
// 初始化食物和NPC
this
.
fondManger
.
init
(
this
.
maxFood
);
this
.
initAnimal
(
this
.
maxAnimal
);
// 设置游戏状态
this
.
setGameState
(
GameState
.
PLAY
);
// 注册事件
director
.
on
(
Events
.
showGOver
,
this
.
showGOver
,
this
);
director
.
on
(
Events
.
setGameState
,
this
.
setGameState
,
this
);
}
onDestroy
()
{
GamePlayer
.
_ins
=
null
;
}
update
(
dt
:
number
)
{
if
(
this
.
state
==
GameState
.
READY
)
return
;
// 更新UI提示
this
.
LTips
.
string
=
`长度:
${
this
.
snake
.
getSnakeLen
()}
金币:
${
this
.
snake
.
getmoneySum
()}
`
;
// 更新蛇的移动
this
.
snake
.
setAngle
((
360
-
this
.
angleBtn
.
angle
)
%
360
);
if
(
this
.
fashBtn
.
isFash
)
{
this
.
snake
.
running
(
dt
);
}
else
{
this
.
snake
.
move
(
dt
);
}
// 更新NPC移动
this
.
animalNode
.
children
.
forEach
(
child
=>
{
child
.
getComponent
(
Animal
)?.
autoMove
(
dt
);
});
}
onPause
()
{
this
.
setGameState
(
GameState
.
PAUSE
);
console
.
log
(
"pause"
);
}
setGameState
(
state
:
GameState
)
{
this
.
state
=
Number
(
state
);
switch
(
this
.
state
)
{
case
GameState
.
READY
:
break
;
case
GameState
.
PLAY
:
director
.
resume
();
break
;
case
GameState
.
PAUSE
:
director
.
pause
();
break
;
case
GameState
.
OVER
:
this
.
rebirthSum
++
;
if
(
this
.
rebirthSum
>
2
)
return
this
.
showGOver
();
showToast
(
"你已死亡!"
);
break
;
case
GameState
.
WIN
:
director
.
pause
();
console
.
log
(
"win"
,
this
.
snake
.
getSnakeLen
(),
this
.
snake
.
getmoneySum
());
break
;
case
GameState
.
QUIT
:
director
.
resume
();
director
.
loadScene
(
"GameMain"
);
break
;
default
:
console
.
log
(
"err"
);
}
}
play
()
{
if
(
this
.
state
==
GameState
.
OVER
)
{
showToast
(
"你已复活!"
);
this
.
state
=
GameState
.
PLAY
;
this
.
snake
.
reInit
(
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
),
math
.
randomRangeInt
(
-
(
Global
.
MAP_HIGHT
/
2
-
50
),
Global
.
MAP_HIGHT
/
2
-
50
)
);
// this.showGOver();
}
}
showGOver
()
{
console
.
log
(
"showGOver"
,
this
.
snake
.
getSnakeLen
(),
this
.
snake
.
getmoneySum
());
}
private
*
_getItemGenerator
(
count
:
number
)
{
for
(
let
i
=
0
;
i
<
count
;
i
++
)
{
yield
this
.
_initItem
(
i
);
}
}
private
_initItem
(
index
:
number
)
{
const
node
=
PoolManager
.
instance
.
getNode
(
this
.
animalPrefab
,
this
.
animalNode
);
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_HIGHT
/
2
-
50
),
Global
.
MAP_HIGHT
/
2
-
50
);
// const skinId = math.randomRangeInt(0, Config.SKIN_STYLE.length);
const
skinId
=
0
;
node
.
getComponent
(
Animal
)?.
init
(
x
,
y
,
math
.
randomRangeInt
(
0
,
360
),
skinId
,
0.2
,
Config
.
SKIN_STYLE
[
skinId
].
len
,
Config
.
SKIN_STYLE
[
skinId
].
taitLen
,
index
+
1
);
}
async
initAnimal
(
count
:
number
)
{
await
this
.
executePreFrame
(
this
.
_getItemGenerator
(
count
),
1
);
}
executePreFrame
(
generator
:
Generator
,
frameTime
:
number
):
Promise
<
boolean
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
iter
=
generator
;
const
next
=
()
=>
{
const
startTime
=
new
Date
().
getTime
();
for
(
let
result
=
iter
.
next
();
;
result
=
iter
.
next
())
{
if
(
!
result
||
result
.
done
)
{
resolve
(
true
);
return
;
}
if
(
new
Date
().
getTime
()
-
startTime
>
frameTime
)
{
this
.
scheduleOnce
(()
=>
{
next
();
});
return
;
}
}
};
next
();
});
}
}
\ No newline at end of file
assets/Scripts/Scenes/MainGame/GamePlayer.ts.meta
deleted
100644 → 0
View file @
d973657c
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "2125124b-7bcf-42f9-b351-d87bcc183901",
"files": [],
"subMetas": {},
"userData": {}
}
assets/Scripts/Scenes/MainGame/Global.ts
View file @
1c3ec0d6
...
...
@@ -6,23 +6,6 @@ const { ccclass } = _decorator;
*/
@
ccclass
(
'Global'
)
export
class
Global
{
/** 插屏广告ID */
public
static
intersitialAdUnitId
:
string
;
/** 激励视频广告ID */
public
static
rewardedVideoAdUnitId
:
string
;
/** Banner广告ID */
public
static
bannerAdAdUnitId
:
string
;
/** 自定义广告ID */
public
static
customAdUnitId
:
string
;
/** 加载进度 */
public
static
LoadingRate
:
number
=
0
;
/** 调试模式 */
public
static
Debug
:
boolean
=
false
;
/** 设计分辨率 */
public
static
designResolutionSize
=
View
.
instance
.
getDesignResolutionSize
();
...
...
@@ -35,9 +18,6 @@ export class Global {
(
Global
.
visibleSize
.
width
/
Global
.
designResolutionSize
.
width
)
*
100
)
/
100
;
/** 游戏玩家实例 */
public
static
gamePlayer
=
null
;
/** 地图宽度 */
public
static
MAP_WIDTH
:
number
=
5000
;
...
...
assets/Scripts/Scenes/MainGame/MainGame.ts
View file @
1c3ec0d6
import
{
_decorator
,
EventKeyboard
,
Input
,
input
,
KeyCode
,
Node
,
}
from
"cc"
;
import
Scene
from
"db://assets/Module/Scene"
;
import
{
_decorator
,
UITransform
,
Node
,
Label
,
Camera
,
director
,
math
,
Prefab
,
PhysicsSystem2D
,
}
from
"cc"
;
import
{
AngleBtn
}
from
"./AngleBtn"
;
import
{
Snake
}
from
"./Snake"
;
import
{
FastBtn
}
from
"./FastBtn"
;
import
{
Animal
}
from
"./Animal"
;
import
{
FondManger
}
from
"./FondManger"
;
import
{
Config
}
from
"./Config"
;
import
{
Global
}
from
"./Global"
;
import
{
GameState
,
Events
}
from
"./Enums"
;
import
{
PoolManager
}
from
"./PoolManager"
;
import
{
showToast
}
from
"../../../Module/UIFast"
;
import
Scene
from
"../../../Module/Scene"
;
const
{
ccclass
,
property
}
=
_decorator
;
export
enum
DIR
{
UP
,
DOWN
,
LEFT
,
RIGHT
,
}
@
ccclass
(
"MainGame"
)
export
class
MainGame
extends
Scene
{
static
bundle
:
string
=
"MainGame"
;
static
skin
:
string
=
"MainGame"
;
@
property
(
Prefab
)
private
animalPrefab
:
Prefab
=
null
;
@
property
({
displayName
:
"最多食物"
,
tooltip
:
"地图上随机产生食物,如果超过当前值不在产生。"
})
maxFood
:
number
=
200
;
@
property
({
displayName
:
"NPC数量"
,
tooltip
:
"当前游戏最大NPC角色"
})
private
maxAnimal
:
number
=
20
;
@
property
({
type
:
AngleBtn
,
displayName
:
"旋转按钮"
})
private
angleBtn
:
AngleBtn
=
null
;
@
property
({
type
:
FastBtn
,
displayName
:
"快进按钮"
})
private
fashBtn
:
FastBtn
=
null
;
@
property
(
UITransform
)
private
uiBg
:
UITransform
=
null
;
@
property
(
Snake
)
private
snake
:
Snake
=
null
;
@
property
(
FondManger
)
fondManger
:
FondManger
=
null
;
@
property
(
Node
)
snak
e
:
Node
=
null
;
private
animalNod
e
:
Node
=
null
;
@
property
speed
:
number
=
200
;
@
property
(
Label
)
private
LTips
:
Label
=
null
;
onLoad
()
{
input
.
on
(
Input
.
EventType
.
TOUCH_START
,
this
.
onTouchStart
,
this
);
input
.
on
(
Input
.
EventType
.
TOUCH_END
,
this
.
onTouchEnd
,
this
);
input
.
on
(
Input
.
EventType
.
TOUCH_CANCEL
,
this
.
onTouchEnd
,
this
);
@
property
(
Camera
)
camera
:
Camera
=
null
;
private
state
:
GameState
=
GameState
.
READY
;
private
rebirthSum
:
number
=
0
;
input
.
on
(
Input
.
EventType
.
KEY_DOWN
,
this
.
onKeyDown
,
this
);
input
.
on
(
Input
.
EventType
.
KEY_UP
,
this
.
onKeyUp
,
this
);
private
static
_ins
:
MainGame
=
null
;
static
get
ins
():
MainGame
{
return
MainGame
.
_ins
;
}
curDir
:
DIR
=
DIR
.
UP
;
onLoad
()
{
MainGame
.
_ins
=
this
;
PhysicsSystem2D
.
instance
.
enable
=
true
;
keyArr
=
[];
Global
.
MAP_WIDTH
=
this
.
uiBg
.
contentSize
.
x
;
Global
.
MAP_HIGHT
=
this
.
uiBg
.
contentSize
.
y
;
async
start
()
{
// this.schedule(() => {
// this.snake.getComponent(Snake).addNode();
// }, 1);
// 初始化墙壁
const
wallTop
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_TOP"
);
wallTop
.
setPosition
(
0
,
Global
.
MAP_HIGHT
/
2
);
wallTop
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
const
wallBottom
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_BOTTOM"
);
wallBottom
.
setPosition
(
0
,
-
Global
.
MAP_HIGHT
/
2
);
wallBottom
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
const
wallLeft
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_LEFT"
);
wallLeft
.
setPosition
(
-
Global
.
MAP_WIDTH
/
2
,
0
);
wallLeft
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_HIGHT
;
const
wallRight
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_RIGHT"
);
wallRight
.
setPosition
(
Global
.
MAP_WIDTH
/
2
,
0
);
wallRight
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_HIGHT
;
// 初始化蛇
const
skinId
=
Global
.
skinId
;
this
.
snake
.
init
(
0
,
0
,
0
,
skinId
,
0.2
,
Config
.
SKIN_STYLE
[
skinId
].
len
,
Config
.
SKIN_STYLE
[
skinId
].
taitLen
,
0
);
// 初始化食物和NPC
this
.
fondManger
.
init
(
this
.
maxFood
);
this
.
initAnimal
(
this
.
maxAnimal
);
// 设置游戏状态
this
.
setGameState
(
GameState
.
PLAY
);
// 注册事件
director
.
on
(
Events
.
showGOver
,
this
.
showGOver
,
this
);
director
.
on
(
Events
.
setGameState
,
this
.
setGameState
,
this
);
}
onDestroy
()
{
input
.
off
(
Input
.
EventType
.
TOUCH_START
,
this
.
onTouchStart
,
this
);
input
.
off
(
Input
.
EventType
.
TOUCH_END
,
this
.
onTouchEnd
,
this
);
input
.
off
(
Input
.
EventType
.
TOUCH_CANCEL
,
this
.
onTouchEnd
,
this
);
MainGame
.
_ins
=
null
;
input
.
off
(
Input
.
EventType
.
KEY_DOWN
,
this
.
onKeyDown
,
this
);
input
.
off
(
Input
.
EventType
.
KEY_UP
,
this
.
onKeyUp
,
this
);
}
onKeyDown
(
event
:
EventKeyboard
)
{
update
(
dt
:
number
)
{
if
(
this
.
state
==
GameState
.
READY
)
return
;
const
keyArr
=
[
KeyCode
.
KEY_W
,
KeyCode
.
KEY_S
,
KeyCode
.
KEY_A
,
KeyCode
.
KEY_D
,
];
// 更新UI提示
this
.
LTips
.
string
=
`长度:
${
this
.
snake
.
getSnakeLen
()}
金币:
${
this
.
snake
.
getmoneySum
()}
`
;
if
(
keyArr
.
indexOf
(
event
.
keyCode
)
>
-
1
)
{
this
.
keyArr
.
push
(
event
.
keyCode
);
// 更新蛇的移动
this
.
snake
.
setAngle
((
360
-
this
.
angleBtn
.
angle
)
%
360
);
if
(
this
.
fashBtn
.
isFash
)
{
this
.
snake
.
running
(
dt
);
}
else
{
this
.
snake
.
move
(
dt
);
}
this
.
setDir
();
// 更新NPC移动
this
.
animalNode
.
children
.
forEach
(
child
=>
{
child
.
getComponent
(
Animal
)?.
autoMove
(
dt
);
});
}
onKeyUp
(
event
:
EventKeyboard
)
{
const
index
=
this
.
keyArr
.
indexOf
(
event
.
keyCode
);
if
(
index
>
-
1
)
{
this
.
keyArr
.
splice
(
index
,
1
);
onPause
()
{
this
.
setGameState
(
GameState
.
PAUSE
);
console
.
log
(
"pause"
);
}
this
.
setDir
();
setGameState
(
state
:
GameState
)
{
this
.
state
=
Number
(
state
);
switch
(
this
.
state
)
{
case
GameState
.
READY
:
break
;
case
GameState
.
PLAY
:
director
.
resume
();
break
;
case
GameState
.
PAUSE
:
director
.
pause
();
break
;
case
GameState
.
OVER
:
this
.
rebirthSum
++
;
if
(
this
.
rebirthSum
>
2
)
return
this
.
showGOver
();
showToast
(
"你已死亡!"
);
break
;
case
GameState
.
WIN
:
director
.
pause
();
console
.
log
(
"win"
,
this
.
snake
.
getSnakeLen
(),
this
.
snake
.
getmoneySum
());
break
;
case
GameState
.
QUIT
:
director
.
resume
();
director
.
loadScene
(
"GameMain"
);
break
;
default
:
console
.
log
(
"err"
);
}
}
setDir
()
{
if
(
!
this
.
keyArr
.
length
)
return
;
this
.
curDir
=
{
[
KeyCode
.
KEY_W
]:
DIR
.
UP
,
[
KeyCode
.
KEY_S
]:
DIR
.
DOWN
,
[
KeyCode
.
KEY_A
]:
DIR
.
LEFT
,
[
KeyCode
.
KEY_D
]:
DIR
.
RIGHT
,
}[
this
.
keyArr
[
this
.
keyArr
.
length
-
1
]];
play
()
{
if
(
this
.
state
==
GameState
.
OVER
)
{
showToast
(
"你已复活!"
);
this
.
state
=
GameState
.
PLAY
;
this
.
snake
.
reInit
(
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
),
math
.
randomRangeInt
(
-
(
Global
.
MAP_HIGHT
/
2
-
50
),
Global
.
MAP_HIGHT
/
2
-
50
)
);
// this.showGOver();
}
}
onTouchStart
(
event
:
any
)
{
showGOver
()
{
console
.
log
(
"showGOver"
,
this
.
snake
.
getSnakeLen
(),
this
.
snake
.
getmoneySum
());
}
private
*
_getItemGenerator
(
count
:
number
)
{
for
(
let
i
=
0
;
i
<
count
;
i
++
)
{
yield
this
.
_initItem
(
i
);
}
}
onTouchEnd
(
event
:
any
)
{
private
_initItem
(
index
:
number
)
{
const
node
=
PoolManager
.
instance
.
getNode
(
this
.
animalPrefab
,
this
.
animalNode
);
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_HIGHT
/
2
-
50
),
Global
.
MAP_HIGHT
/
2
-
50
);
// const skinId = math.randomRangeInt(0, Config.SKIN_STYLE.length);
const
skinId
=
0
;
node
.
getComponent
(
Animal
)?.
init
(
x
,
y
,
math
.
randomRangeInt
(
0
,
360
),
skinId
,
0.2
,
Config
.
SKIN_STYLE
[
skinId
].
len
,
Config
.
SKIN_STYLE
[
skinId
].
taitLen
,
index
+
1
);
}
async
initAnimal
(
count
:
number
)
{
await
this
.
executePreFrame
(
this
.
_getItemGenerator
(
count
),
1
);
}
update
(
dt
:
number
)
{
executePreFrame
(
generator
:
Generator
,
frameTime
:
number
):
Promise
<
boolean
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
iter
=
generator
;
if
(
this
.
curDir
==
DIR
.
UP
)
{
const
newY
=
this
.
snake
.
position
.
y
+
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
this
.
snake
.
position
.
x
,
newY
);
}
else
if
(
this
.
curDir
==
DIR
.
DOWN
)
{
const
newY
=
this
.
snake
.
position
.
y
-
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
this
.
snake
.
position
.
x
,
newY
);
}
else
if
(
this
.
curDir
==
DIR
.
LEFT
)
{
const
newX
=
this
.
snake
.
position
.
x
-
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
newX
,
this
.
snake
.
position
.
y
);
}
else
if
(
this
.
curDir
==
DIR
.
RIGHT
)
{
const
newX
=
this
.
snake
.
position
.
x
+
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
newX
,
this
.
snake
.
position
.
y
);
const
next
=
()
=>
{
const
startTime
=
new
Date
().
getTime
();
for
(
let
result
=
iter
.
next
();
;
result
=
iter
.
next
())
{
if
(
!
result
||
result
.
done
)
{
resolve
(
true
);
return
;
}
if
(
new
Date
().
getTime
()
-
startTime
>
frameTime
)
{
this
.
scheduleOnce
(()
=>
{
next
();
});
return
;
}
}
};
next
();
});
}
}
\ No newline at end of file
assets/Scripts/Scenes/MainGame/MainGame.ts.meta
View file @
1c3ec0d6
...
...
@@ -2,7 +2,7 @@
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "
701bec3a-f5a6-484b-92cf-5cb67aff8961
",
"uuid": "
82c443ab-937b-4bfa-8e7a-5a6a04f13710
",
"files": [],
"subMetas": {},
"userData": {}
...
...
assets/Scripts/Scenes/MainGame/Snake.ts
View file @
1c3ec0d6
...
...
@@ -14,6 +14,7 @@ import { FoodType } from "./Enums";
import
{
Global
}
from
"./Global"
;
import
{
PoolManager
}
from
"./PoolManager"
;
import
{
isIntersect
}
from
"./uitl"
;
import
{
MainGame
}
from
"./MainGame"
;
const
{
ccclass
,
property
}
=
_decorator
;
@
ccclass
(
"Snake"
)
...
...
@@ -162,8 +163,8 @@ export class Snake extends Component {
}
// 生成新的食物
if
(
Global
.
gamePlayer
.
fondManger
.
getFoodSum
()
<
Global
.
gamePlayer
.
maxFood
)
{
Global
.
gamePlayer
.
fondManger
.
setFood
();
if
(
MainGame
.
ins
.
fondManger
.
getFoodSum
()
<
MainGame
.
ins
.
maxFood
)
{
MainGame
.
ins
.
fondManger
.
setFood
();
}
})
.
start
();
...
...
@@ -258,7 +259,7 @@ export class Snake extends Component {
this
.
Head
.
setSiblingIndex
(
lastIndex
+
1
);
// 更新相机位置
Global
.
gamePlayer
.
camera
.
node
.
setPosition
(
this
.
Head
.
getPosition
());
MainGame
.
ins
.
camera
.
node
.
setPosition
(
this
.
Head
.
getPosition
());
}
move
(
dt
:
number
)
{
...
...
@@ -327,7 +328,7 @@ export class Snake extends Component {
this
.
Head
.
setSiblingIndex
(
lastIndex
+
1
);
// 更新相机位置,跟随头部
Global
.
gamePlayer
.
camera
.
node
.
setPosition
(
this
.
Head
.
getPosition
());
MainGame
.
ins
.
camera
.
node
.
setPosition
(
this
.
Head
.
getPosition
());
}
getSnakeLen
()
{
...
...
assets/Scripts/Scenes/MainGame/Snake1.ts
deleted
100644 → 0
View file @
d973657c
import
{
_decorator
,
Component
,
instantiate
,
Node
,
Prefab
,
Vec3
}
from
"cc"
;
const
{
ccclass
,
property
}
=
_decorator
;
class
SnakeNode
{
node
:
Node
=
null
;
next
:
SnakeNode
=
null
;
}
const
_tempPos
=
new
Vec3
();
const
_tempPrePos
=
new
Vec3
();
@
ccclass
(
"Snake"
)
export
class
Snake
extends
Component
{
@
property
(
Prefab
)
nodePrefab
:
Prefab
=
null
;
@
property
gap
:
number
=
50
;
head
:
SnakeNode
=
null
;
last
:
SnakeNode
=
null
;
onLoad
()
{
this
.
last
=
this
.
head
=
new
SnakeNode
();
this
.
head
.
node
=
this
.
node
;
}
addNode
()
{
const
node
=
instantiate
(
this
.
nodePrefab
);
this
.
node
.
parent
.
addChild
(
node
);
node
.
position
=
this
.
last
.
node
.
position
;
const
snakeNode
=
new
SnakeNode
();
snakeNode
.
node
=
node
;
this
.
last
.
next
=
snakeNode
;
this
.
last
=
snakeNode
;
}
update
(
dt
:
number
)
{
let
pre
:
SnakeNode
=
this
.
head
;
let
current
:
SnakeNode
=
this
.
head
.
next
;
while
(
current
)
{
const
posDir
=
current
.
node
.
getPosition
(
_tempPos
)
.
subtract
(
pre
.
node
.
position
)
.
normalize
();
const
pos
=
pre
.
node
.
getPosition
(
_tempPrePos
)
.
add
(
posDir
.
multiplyScalar
(
this
.
gap
));
current
.
node
.
setPosition
(
pos
);
pre
=
current
;
current
=
current
.
next
;
}
}
}
\ No newline at end of file
assets/Scripts/Scenes/MainGame/Snake1.ts.meta
deleted
100644 → 0
View file @
d973657c
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "917a4745-8677-4e4a-83f1-07545cfd07ef",
"files": [],
"subMetas": {},
"userData": {}
}
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