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
b2d1c691
Commit
b2d1c691
authored
Nov 22, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
5e372767
Changes
8
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
1120 additions
and
49 deletions
+1120
-49
AISnake.ts
assets/Scripts/Scenes/MainGame/AISnake.ts
+999
-26
AISnakeBack.ts
assets/Scripts/Scenes/MainGame/AISnakeBack.ts
+1
-1
AISnakeBack2.ts
assets/Scripts/Scenes/MainGame/AISnakeBack2.ts
+63
-0
AISnakeBack2.ts.meta
assets/Scripts/Scenes/MainGame/AISnakeBack2.ts.meta
+9
-0
MainGame.ts
assets/Scripts/Scenes/MainGame/MainGame.ts
+3
-1
Food.ts
assets/Scripts/Scenes/MainGame/Props/Food.ts
+1
-1
Snake.ts
assets/Scripts/Scenes/MainGame/Snake.ts
+22
-20
index.html
index.html
+22
-0
No files found.
assets/Scripts/Scenes/MainGame/AISnake.ts
View file @
b2d1c691
This diff is collapsed.
Click to expand it.
assets/Scripts/Scenes/MainGame/AISnakeBack.ts
View file @
b2d1c691
...
...
@@ -57,7 +57,7 @@ export class AISnake extends Snake {
// 碰到其他蛇身
// this.setAngle(this.head.angle + 180);
// this.isFast = true;
this
.
setState
(
AIState
.
ESCAPING
,
otherCollider
.
node
.
parent
.
getComponent
(
Snake
));
this
.
setState
(
AIState
.
ESCAPING
,
otherCollider
.
node
?.
parent
?
.
getComponent
(
Snake
));
}
}
...
...
assets/Scripts/Scenes/MainGame/AISnakeBack2.ts
0 → 100644
View file @
b2d1c691
import
{
_decorator
}
from
"cc"
;
import
{
Snake
}
from
"./Snake"
;
import
{
MainGame
}
from
"./MainGame"
;
import
{
AIController
}
from
"./AI/AIController"
;
import
{
aiPool
}
from
"./Manager/CommonPool"
;
const
{
ccclass
}
=
_decorator
;
@
ccclass
(
"AISnake"
)
export
class
AISnake
extends
Snake
{
private
aiController
:
AIController
;
private
updateTimer
:
number
=
0
;
private
readonly
UPDATE_INTERVAL
:
number
=
0.1
;
async
init
(
config
:
any
)
{
await
super
.
init
(
config
);
// const difficulty = 1 + Math.random() * 4;
this
.
aiController
=
new
AIController
(
this
,
5
);
}
onEnable
()
{
super
.
onEnable
();
// const eye = this.head.getChildByName("范围").getComponent(Collider2D);
// eye.on(Contact2DType.BEGIN_CONTACT, this.onBeginEye, this);
}
// onBeginEye(selfCollider: Collider2D, otherCollider: Collider2D) {
// super.onBeginEye(selfCollider, otherCollider);
// if (otherCollider.group === PhysicsGroup["Body"] && otherCollider.tag != this.tag) {
// // 碰到其他蛇身
// // this.setAngle(this.head.angle + 180);
// // this.isFast = true;
// this.setState(AIState.ESCAPING, otherCollider.node.parent.getComponent(Snake));
// }
// }
death
()
{
super
.
death
();
this
.
node
.
removeFromParent
();
aiPool
.
put
(
this
.
node
);
MainGame
.
ins
.
initAnimal
(
1
);
}
onUpdate
(
dt
:
number
)
{
if
(
!
this
.
isLife
)
return
;
this
.
updateTimer
+=
dt
;
if
(
this
.
updateTimer
>=
this
.
UPDATE_INTERVAL
)
{
this
.
updateTimer
=
0
;
this
.
aiController
.
update
(
dt
);
}
super
.
onUpdate
(
dt
);
}
}
\ No newline at end of file
assets/Scripts/Scenes/MainGame/AISnakeBack2.ts.meta
0 → 100644
View file @
b2d1c691
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "fc484593-2f1d-43f4-8f3a-99561dbc1481",
"files": [],
"subMetas": {},
"userData": {}
}
assets/Scripts/Scenes/MainGame/MainGame.ts
View file @
b2d1c691
...
...
@@ -122,7 +122,9 @@ export class MainGame extends Scene {
wallRight
.
setPosition
(
Global
.
MAP_WIDTH
/
2
,
0
);
wallRight
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_HEIGHT
;
this
.
player
.
init
();
this
.
player
.
init
({
// initEnergy: 100
});
// 初始化食物和NPC
this
.
fondManger
.
init
(
this
.
maxFood
);
...
...
assets/Scripts/Scenes/MainGame/Props/Food.ts
View file @
b2d1c691
...
...
@@ -15,7 +15,7 @@ export class Food extends Component {
set
energy
(
energy
:
number
)
{
this
.
_energy
=
energy
;
const
scale
=
1
+
(
energy
-
1
)
/
10
;
const
scale
=
1
+
(
energy
-
1
)
/
3
;
this
.
node
.
scale
.
set
(
scale
,
scale
);
}
...
...
assets/Scripts/Scenes/MainGame/Snake.ts
View file @
b2d1c691
...
...
@@ -53,7 +53,7 @@ export class Snake extends Component {
// 蛇的状态
isLife
:
boolean
=
false
;
private
scale
:
number
=
0.2
;
speed
:
number
=
6
00
;
speed
:
number
=
3
00
;
private
energy
:
number
=
0
;
protected
tag
:
number
=
0
;
...
...
@@ -62,8 +62,6 @@ 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
;
}
...
...
@@ -74,7 +72,6 @@ export class Snake extends Component {
const
{
x
=
0
,
y
=
0
,
angle
=
0
,
scale
=
0.5
,
skinName
=
"default"
,
bodyCount
=
5
,
initEnergy
=
5
,
}
=
config
;
...
...
@@ -84,7 +81,7 @@ export class Snake extends Component {
this
.
energy
=
0
;
this
.
bodyArr
=
[];
this
.
scale
=
scale
;
this
.
speed
=
this
.
speed
*
scale
;
//
this.speed = this.speed * scale;
this
.
tag
=
Snake
.
tag
++
;
// 设置头部
...
...
@@ -98,7 +95,6 @@ export class Snake extends Component {
// this.head.getComponent(UITransform).anchorX = (bw / 2) / hw;
this
.
head
.
getComponent
(
UITransform
).
anchorX
=
(
bw
/
2
)
/
hw
;
this
.
addEnergy
(
initEnergy
);
// 创建尾巴节点
const
tile
=
bodyPool
.
get
()
||
instantiate
(
this
.
bodyPrefab
);
...
...
@@ -120,6 +116,9 @@ export class Snake extends Component {
this
.
node
.
addChild
(
tile
);
this
.
bodyArr
.
push
(
tile
);
// 创建身体节点
this
.
addEnergy
(
initEnergy
);
this
.
isLife
=
true
;
this
.
ready
=
true
;
}
...
...
@@ -166,33 +165,34 @@ export class Snake extends Component {
scale
:
v3
(
0
,
0
)
})
.
call
(()
=>
{
otherCollider
.
node
.
getComponent
(
Food
).
recycle
();
if
(
!
this
.
isLife
)
return
;
if
(
foodType
==
FoodType
.
FOOD
)
{
this
.
addEnergy
(
1
);
}
const
foodTs
=
otherCollider
.
getComponent
(
Food
);
this
.
addEnergy
(
foodTs
.
energy
);
foodTs
.
recycle
();
})
.
start
();
}
}
// 能量与成长
lastRemaining
=
0
;
private
addEnergy
(
value
:
number
)
{
this
.
energy
+=
value
;
const
growthThreshold
=
Math
.
floor
(
4
*
this
.
scale
);
while
(
this
.
energy
>=
growthThreshold
)
{
value
+=
this
.
lastRemaining
;
while
(
value
>=
growthThreshold
)
{
this
.
grow
();
this
.
energy
-=
growthThreshold
;
value
-=
growthThreshold
;
if
(
this
.
scale
<
1
)
{
this
.
scale
+=
0.005
;
}
}
this
.
speed
=
600
*
this
.
scal
e
;
this
.
lastRemaining
=
valu
e
;
// this.speed = 600 * this.scale;
}
// 蛇身体生长
...
...
@@ -229,7 +229,7 @@ export class Snake extends Component {
isFast
=
false
;
private
positions
:
Vec3
[]
=
[];
// 存储历史位置点
private
readonly
HISTORY_LENGTH
=
100
;
// 增加历史点数量
private
readonly
SEGMENT_SPACING
=
4
;
// 增加节点间距
private
readonly
SEGMENT_SPACING
=
8
;
// 增加节点间距
moveTime
=
1
/
60
;
totalTime
=
0
;
...
...
@@ -262,10 +262,12 @@ export class Snake extends Component {
this
.
head
.
setPosition
(
newHeadPos
);
this
.
head
.
setScale
(
this
.
scale
,
this
.
scale
);
const
space
=
~~
(
this
.
SEGMENT_SPACING
*
this
.
scale
);
// 存储历史位置
this
.
positions
.
unshift
(
newHeadPos
.
clone
());
// 确保历史位置点足够多,以容纳所有身体节点
const
requiredLength
=
this
.
bodyArr
.
length
*
this
.
SEGMENT_SPACING
+
1
;
const
requiredLength
=
this
.
bodyArr
.
length
*
space
+
1
;
if
(
this
.
positions
.
length
>
Math
.
max
(
requiredLength
,
this
.
HISTORY_LENGTH
))
{
this
.
positions
.
pop
();
}
...
...
@@ -275,7 +277,7 @@ export class Snake extends Component {
const
body
=
this
.
bodyArr
[
i
];
// 为每个节点计算一个固定的偏移量
const
offset
=
(
i
+
1
)
*
this
.
SEGMENT_SPACING
;
const
offset
=
(
i
+
1
)
*
space
;
// 确保不会超出历史位置数组范围
if
(
offset
<
this
.
positions
.
length
)
{
...
...
@@ -332,8 +334,8 @@ export class Snake extends Component {
bodyPool
.
put
(
body
);
return
{
x
:
body
.
position
.
x
+
10
-
5
,
y
:
body
.
position
.
y
+
10
-
5
,
x
:
body
.
position
.
x
+
Math
.
random
()
*
10
-
5
,
y
:
body
.
position
.
y
+
Math
.
random
()
*
10
-
5
,
energy
:
~~
(
this
.
energy
/
len
),
};
});
...
...
index.html
0 → 100644
View file @
b2d1c691
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Title
</title>
</head>
<body>
<script>
const
canvas
=
document
.
createElement
(
"canvas"
);
canvas
.
width
=
500
;
canvas
.
height
=
500
;
const
ctx
=
canvas
.
getContext
(
"2d"
);
ctx
.
fillStyle
=
"transparent"
;
ctx
.
fillRect
(
0
,
0
,
500
,
500
);
const
b64
=
canvas
.
toDataURL
(
"image/png"
);
console
.
log
(
b64
);
</script>
</body>
</html>
\ No newline at end of file
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