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
5e372767
Commit
5e372767
authored
Nov 22, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
5454f9ee
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
41 deletions
+46
-41
AISnake.ts
assets/Scripts/Scenes/MainGame/AISnake.ts
+0
-2
AISnakeBack.ts
assets/Scripts/Scenes/MainGame/AISnakeBack.ts
+4
-2
Snake.ts
assets/Scripts/Scenes/MainGame/Snake.ts
+40
-35
ExecutePreFrame.ts
assets/Scripts/Utils/ExecutePreFrame.ts
+2
-2
No files found.
assets/Scripts/Scenes/MainGame/AISnake.ts
View file @
5e372767
...
...
@@ -44,8 +44,6 @@ export class AISnake extends Snake {
aiPool
.
put
(
this
.
node
);
console
.
log
(
MainGame
.
ins
.
animalNode
.
children
.
length
)
MainGame
.
ins
.
initAnimal
(
1
);
}
...
...
assets/Scripts/Scenes/MainGame/AISnakeBack.ts
View file @
5e372767
...
...
@@ -3,6 +3,7 @@ import { Snake } from "./Snake";
import
{
DirectionType
}
from
"./Common/Enums"
;
import
{
Global
}
from
"./Global"
;
import
{
MainGame
}
from
"./MainGame"
;
import
{
aiPool
}
from
"./Manager/CommonPool"
;
const
{
ccclass
,
property
}
=
_decorator
;
...
...
@@ -64,9 +65,10 @@ export class AISnake extends Snake {
super
.
death
();
this
.
node
.
removeFromParent
();
this
.
destroy
();
MainGame
.
ins
.
initAnimal
(
1
);
aiPool
.
put
(
this
.
node
);
MainGame
.
ins
.
initAnimal
(
1
);
}
private
get
difficultyParams
()
{
...
...
assets/Scripts/Scenes/MainGame/Snake.ts
View file @
5e372767
...
...
@@ -30,6 +30,7 @@ export interface IInitConfig {
skinName
?:
string
;
scale
?:
number
;
bodyCount
?:
number
;
initEnergy
?:
number
,
}
@
ccclass
(
"Snake"
)
...
...
@@ -61,6 +62,8 @@ export class Snake extends Component {
private
vh
:
number
=
Global
.
visibleSize
.
height
/
2
+
100
;
private
ready
:
boolean
=
false
;
tileNode
:
Node
=
null
;
get
radius
()
{
return
this
.
scale
*
29
;
}
...
...
@@ -70,7 +73,9 @@ export class Snake extends Component {
const
{
x
=
0
,
y
=
0
,
angle
=
0
,
scale
=
0.5
,
skinName
=
"default"
,
bodyCount
=
5
,
skinName
=
"default"
,
bodyCount
=
5
,
initEnergy
=
5
,
}
=
config
;
await
this
.
setSkin
(
skinName
);
...
...
@@ -93,31 +98,28 @@ export class Snake extends Component {
// this.head.getComponent(UITransform).anchorX = (bw / 2) / hw;
this
.
head
.
getComponent
(
UITransform
).
anchorX
=
(
bw
/
2
)
/
hw
;
// 创建身体节点
for
(
let
i
=
0
;
i
<
bodyCount
;
i
++
)
{
const
body
=
bodyPool
.
get
()
||
instantiate
(
this
.
bodyPrefab
);
const
collider
=
body
.
getComponent
(
Collider2D
);
collider
.
tag
=
this
.
tag
;
body
.
angle
=
angle
;
body
.
setPosition
(
-
99999
,
-
99999
);
body
.
setScale
(
scale
,
scale
);
this
.
node
.
addChild
(
body
);
// 设置身体部分的贴图
if
(
i
==
bodyCount
-
1
&&
this
.
imgTail
)
{
body
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
imgTail
;
const
tw
=
this
.
imgTail
.
originalSize
.
width
;
body
.
getComponent
(
UITransform
).
anchorX
=
(
bw
/
2
)
/
tw
;
}
else
{
body
.
getComponent
(
Sprite
).
spriteFrame
=
i
%
2
==
0
?
this
.
imgBody1
:
this
.
imgBody2
;
}
this
.
addEnergy
(
initEnergy
);
// body.active = false;
this
.
bodyArr
.
push
(
body
);
// 创建尾巴节点
const
tile
=
bodyPool
.
get
()
||
instantiate
(
this
.
bodyPrefab
);
tile
.
angle
=
angle
;
tile
.
setPosition
(
x
,
y
);
tile
.
setScale
(
scale
,
scale
);
if
(
this
.
imgTail
)
{
tile
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
imgTail
;
const
tw
=
this
.
imgTail
.
originalSize
.
width
;
tile
.
getComponent
(
UITransform
).
anchorX
=
(
bw
/
2
)
/
tw
;
}
else
{
tile
.
getComponent
(
Sprite
).
spriteFrame
=
this
.
imgBody1
;
}
const
collider
=
tile
.
getComponent
(
Collider2D
);
collider
.
tag
=
this
.
tag
;
this
.
node
.
addChild
(
tile
);
this
.
bodyArr
.
push
(
tile
);
this
.
isLife
=
true
;
this
.
ready
=
true
;
}
...
...
@@ -181,27 +183,29 @@ export class Snake extends Component {
this
.
energy
+=
value
;
const
growthThreshold
=
Math
.
floor
(
4
*
this
.
scale
);
if
(
this
.
energy
>=
growthThreshold
)
{
while
(
this
.
energy
>=
growthThreshold
)
{
this
.
grow
();
this
.
energy
-=
growthThreshold
;
if
(
this
.
scale
<
1
)
{
this
.
scale
+=
0.005
;
}
this
.
speed
=
600
*
this
.
scale
;
}
this
.
speed
=
600
*
this
.
scale
;
}
// 蛇身体生长
private
grow
()
{
let
len
=
this
.
bodyArr
.
length
;
if
(
this
.
imgTail
)
{
len
-=
1
;
}
if
(
this
.
imgTail
)
len
-=
1
;
const
newBody
=
bodyPool
.
get
()
||
instantiate
(
this
.
bodyPrefab
);
newBody
.
angle
=
this
.
bodyArr
[
len
-
1
].
angle
;
newBody
.
setPosition
(
this
.
bodyArr
[
len
-
1
].
getPosition
());
const
pre
=
this
.
bodyArr
[
len
-
1
]
||
this
.
head
;
newBody
.
angle
=
pre
.
angle
;
newBody
.
setPosition
(
pre
.
position
);
newBody
.
setScale
(
this
.
scale
,
this
.
scale
);
this
.
node
.
addChild
(
newBody
);
...
...
@@ -322,14 +326,15 @@ export class Snake extends Component {
this
.
isLife
=
false
;
this
.
node
.
active
=
false
;
const
len
=
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
,
x
:
body
.
position
.
x
+
10
-
5
,
y
:
body
.
position
.
y
+
10
-
5
,
energy
:
~~
(
this
.
energy
/
len
)
,
};
});
...
...
assets/Scripts/Utils/ExecutePreFrame.ts
View file @
5e372767
...
...
@@ -8,9 +8,9 @@ export function* getItemGenerator(length: number, func: (index: number) => any)
export
function
executePreFrame
(
generator
:
Generator
,
duration
:
number
,
context
:
Component
)
{
return
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
le
t
gen
=
generator
;
cons
t
gen
=
generator
;
// 创建执行函数
le
t
execute
=
()
=>
{
cons
t
execute
=
()
=>
{
// 执行之前,先记录开始时间戳
let
startTime
=
Date
.
now
();
...
...
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