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
1cd3961f
Commit
1cd3961f
authored
Dec 12, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
0e4e57bd
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
53 additions
and
53 deletions
+53
-53
AIDecision.ts
assets/Scripts/Scenes/MainGame/AI/AIDecision.ts
+4
-4
AIMovement.ts
assets/Scripts/Scenes/MainGame/AI/AIMovement.ts
+4
-4
AIPerception.ts
assets/Scripts/Scenes/MainGame/AI/AIPerception.ts
+6
-6
AISnake.ts
assets/Scripts/Scenes/MainGame/AISnake.ts
+18
-26
Global.ts
assets/Scripts/Scenes/MainGame/Global.ts
+4
-2
LuckyBagManager.ts
assets/Scripts/Scenes/MainGame/Manager/LuckyBagManager.ts
+1
-5
Snake.ts
assets/Scripts/Scenes/MainGame/Snake.ts
+6
-6
测试性能.mjs
测试性能.mjs
+10
-0
No files found.
assets/Scripts/Scenes/MainGame/AI/AIDecision.ts
View file @
1cd3961f
...
...
@@ -400,8 +400,8 @@ export class AIDecision {
}
// 确保在地图范围内
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
const
margin
=
this
.
SAFE_DISTANCE
;
targetPos
.
x
=
math
.
clamp
(
targetPos
.
x
,
-
mapHalfWidth
+
margin
,
mapHalfWidth
-
margin
);
...
...
@@ -425,8 +425,8 @@ export class AIDecision {
private
calculateBoundaryEscapeVector
(
perception
:
PerceptionResult
):
Vec3
{
const
myPos
=
perception
.
snake
.
head
.
getPosition
();
const
escapeVector
=
new
Vec3
();
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
// 根据到边界的距离计算逃离向量
if
(
myPos
.
x
>
mapHalfWidth
*
0.8
)
escapeVector
.
x
=
-
1
;
...
...
assets/Scripts/Scenes/MainGame/AI/AIMovement.ts
View file @
1cd3961f
...
...
@@ -137,8 +137,8 @@ export class AIMovement {
currentPos
.
add
(
stepVector
);
// 计算边界危险度
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
const
boundaryDanger
=
Math
.
max
(
Math
.
max
(
0
,
(
Math
.
abs
(
currentPos
.
x
)
-
(
mapHalfWidth
-
this
.
ESCAPE_BOUNDARY
))
/
this
.
ESCAPE_BOUNDARY
),
...
...
@@ -186,8 +186,8 @@ export class AIMovement {
private
getObstacleDistance
(
from
:
Vec3
,
to
:
Vec3
):
number
{
// 检查地图边界
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
const
margin
=
50
;
if
(
Math
.
abs
(
to
.
x
)
>
mapHalfWidth
-
margin
||
...
...
assets/Scripts/Scenes/MainGame/AI/AIPerception.ts
View file @
1cd3961f
...
...
@@ -186,8 +186,8 @@ export class AIPerception {
}
private
calculateBoundaryRisk
(
position
:
Vec3
):
number
{
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
const
distanceToEdge
=
Math
.
min
(
mapHalfWidth
-
Math
.
abs
(
position
.
x
),
...
...
@@ -198,8 +198,8 @@ export class AIPerception {
}
private
evaluatePositionSafety
(
pos
:
Vec3
):
number
{
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
const
distToBoundaryX
=
Math
.
min
(
mapHalfWidth
-
Math
.
abs
(
pos
.
x
),
mapHalfWidth
);
const
distToBoundaryY
=
Math
.
min
(
mapHalfHeight
-
Math
.
abs
(
pos
.
y
),
mapHalfHeight
);
...
...
@@ -307,8 +307,8 @@ export class AIPerception {
}
private
getBoundaryDistance
(
position
:
Vec3
):
number
{
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
const
distanceToRight
=
mapHalfWidth
-
position
.
x
;
const
distanceToLeft
=
position
.
x
+
mapHalfWidth
;
...
...
assets/Scripts/Scenes/MainGame/AISnake.ts
View file @
1cd3961f
...
...
@@ -156,14 +156,11 @@ export class AISnake extends Snake {
// 判断是否在极度危险的位置(非常靠近边界)
private
isInDangerousPosition
(
position
:
Vec3
):
boolean
{
const
dangerBuffer
=
this
.
ESCAPE_BOUNDARY
;
const
mapWidth
=
Global
.
MAP_WIDTH
;
const
mapHeight
=
Global
.
MAP_HEIGHT
;
return
(
position
.
x
>
mapWidth
/
2
-
dangerBuffer
||
position
.
x
<
-
mapWidth
/
2
+
dangerBuffer
||
position
.
y
>
mapHeight
/
2
-
dangerBuffer
||
position
.
y
<
-
mapHeight
/
2
+
dangerBuffer
position
.
x
>
Global
.
HALF_MAP_WIDTH
-
dangerBuffer
||
position
.
x
<
-
Global
.
HALF_MAP_WIDTH
+
dangerBuffer
||
position
.
y
>
Global
.
HALF_MAP_HEIGHT
-
dangerBuffer
||
position
.
y
<
-
Global
.
HALF_MAP_HEIGHT
+
dangerBuffer
);
}
...
...
@@ -726,12 +723,9 @@ export class AISnake extends Snake {
// 获取到边界的距离
private
getDistanceToBoundary
(
position
:
IVec2Like
):
number
{
const
mapWidth
=
Global
.
MAP_WIDTH
;
const
mapHeight
=
Global
.
MAP_HEIGHT
;
return
Math
.
min
(
mapWidth
/
2
-
Math
.
abs
(
position
.
x
),
mapHeight
/
2
-
Math
.
abs
(
position
.
y
)
Global
.
HALF_MAP_WIDTH
-
Math
.
abs
(
position
.
x
),
Global
.
HALF_MAP_HEIGHT
-
Math
.
abs
(
position
.
y
)
);
}
...
...
@@ -754,37 +748,36 @@ export class AISnake extends Snake {
private
avoidBoundary
()
{
const
myPos
=
this
.
head
.
getPosition
();
const
mapWidth
=
Global
.
MAP_WIDTH
;
const
mapHeight
=
Global
.
MAP_HEIGHT
;
const
{
HALF_MAP_WIDTH
,
HALF_MAP_HEIGHT
}
=
Global
;
const
boundaryBuffer
=
this
.
ESCAPE_BOUNDARY
;
let
targetAngle
=
this
.
head
.
angle
;
let
isCorner
=
false
;
// 检查四角区域
if
(
myPos
.
x
<
-
mapWidth
/
2
+
boundaryBuffer
&&
myPos
.
y
<
-
mapHeight
/
2
+
boundaryBuffer
)
{
if
(
myPos
.
x
<
-
HALF_MAP_WIDTH
+
boundaryBuffer
&&
myPos
.
y
<
-
HALF_MAP_HEIGHT
+
boundaryBuffer
)
{
targetAngle
=
45
;
// 右上
isCorner
=
true
;
}
else
if
(
myPos
.
x
>
mapWidth
/
2
-
boundaryBuffer
&&
myPos
.
y
<
-
mapHeight
/
2
+
boundaryBuffer
)
{
}
else
if
(
myPos
.
x
>
HALF_MAP_WIDTH
-
boundaryBuffer
&&
myPos
.
y
<
-
HALF_MAP_HEIGHT
+
boundaryBuffer
)
{
targetAngle
=
135
;
// 左上
isCorner
=
true
;
}
else
if
(
myPos
.
x
<
-
mapWidth
/
2
+
boundaryBuffer
&&
myPos
.
y
>
mapHeight
/
2
-
boundaryBuffer
)
{
}
else
if
(
myPos
.
x
<
-
HALF_MAP_WIDTH
+
boundaryBuffer
&&
myPos
.
y
>
HALF_MAP_HEIGHT
-
boundaryBuffer
)
{
targetAngle
=
315
;
// 右下
isCorner
=
true
;
}
else
if
(
myPos
.
x
>
mapWidth
/
2
-
boundaryBuffer
&&
myPos
.
y
>
mapHeight
/
2
-
boundaryBuffer
)
{
}
else
if
(
myPos
.
x
>
HALF_MAP_WIDTH
-
boundaryBuffer
&&
myPos
.
y
>
HALF_MAP_HEIGHT
-
boundaryBuffer
)
{
targetAngle
=
225
;
// 左下
isCorner
=
true
;
}
else
{
// 检查边界
if
(
myPos
.
x
<
-
mapWidth
/
2
+
boundaryBuffer
)
{
if
(
myPos
.
x
<
-
HALF_MAP_WIDTH
+
boundaryBuffer
)
{
targetAngle
=
0
;
// 向右
}
else
if
(
myPos
.
x
>
mapWidth
/
2
-
boundaryBuffer
)
{
}
else
if
(
myPos
.
x
>
HALF_MAP_WIDTH
-
boundaryBuffer
)
{
targetAngle
=
180
;
// 向左
}
if
(
myPos
.
y
<
-
mapHeight
/
2
+
boundaryBuffer
)
{
if
(
myPos
.
y
<
-
HALF_MAP_HEIGHT
+
boundaryBuffer
)
{
targetAngle
=
90
;
// 向上
}
else
if
(
myPos
.
y
>
mapHeight
/
2
-
boundaryBuffer
)
{
}
else
if
(
myPos
.
y
>
HALF_MAP_HEIGHT
-
boundaryBuffer
)
{
targetAngle
=
270
;
// 向下
}
}
...
...
@@ -804,8 +797,7 @@ export class AISnake extends Snake {
const
foods
=
FoodManger
.
ins
.
node
.
children
;
const
boundaryBuffer
=
this
.
ESCAPE_BOUNDARY
;
const
mapWidth
=
Global
.
MAP_WIDTH
;
const
mapHeight
=
Global
.
MAP_HEIGHT
;
const
{
HALF_MAP_WIDTH
,
HALF_MAP_HEIGHT
}
=
Global
;
for
(
const
food
of
foods
)
{
if
(
!
food
.
isValid
||
!
food
.
active
)
continue
;
...
...
@@ -814,8 +806,8 @@ export class AISnake extends Snake {
const
distance
=
Vec3
.
distance
(
myPos
,
foodPos
);
// 检查食物是否靠近墙体
if
(
foodPos
.
x
<
-
mapWidth
/
2
+
boundaryBuffer
||
foodPos
.
x
>
mapWidth
/
2
-
boundaryBuffer
||
foodPos
.
y
<
-
mapHeight
/
2
+
boundaryBuffer
||
foodPos
.
y
>
mapHeight
/
2
-
boundaryBuffer
)
{
if
(
foodPos
.
x
<
-
HALF_MAP_WIDTH
+
boundaryBuffer
||
foodPos
.
x
>
HALF_MAP_WIDTH
-
boundaryBuffer
||
foodPos
.
y
<
-
HALF_MAP_HEIGHT
+
boundaryBuffer
||
foodPos
.
y
>
HALF_MAP_HEIGHT
-
boundaryBuffer
)
{
continue
;
// 跳过靠近墙体的食物
}
...
...
assets/Scripts/Scenes/MainGame/Global.ts
View file @
1cd3961f
...
...
@@ -10,17 +10,19 @@ export class Global {
/** 地图宽度 */
static
MAP_WIDTH
:
number
=
6000
;
static
HALF_MAP_WIDTH
:
number
=
Global
.
MAP_WIDTH
*
0.5
;
/** 地图高度 */
static
MAP_HEIGHT
:
number
=
4200
;
static
HALF_MAP_HEIGHT
:
number
=
Global
.
MAP_HEIGHT
*
0.5
;
/** 道具CD */
static
PROP_CD
:
number
=
15
;
static
getRandomPosition
(
padding
:
number
=
0
):
IVec2Like
{
const
maxW
=
Global
.
MAP_WIDTH
/
2
-
padding
;
const
maxH
=
Global
.
MAP_HEIGHT
/
2
-
padding
;
const
maxW
=
Global
.
HALF_MAP_WIDTH
-
padding
;
const
maxH
=
Global
.
HALF_MAP_HEIGHT
-
padding
;
const
x
=
math
.
randomRange
(
-
maxW
,
maxW
);
const
y
=
math
.
randomRange
(
-
maxH
,
maxH
);
...
...
assets/Scripts/Scenes/MainGame/Manager/LuckyBagManager.ts
View file @
1cd3961f
...
...
@@ -86,12 +86,8 @@ export class LuckyBagManager extends Component {
add
(
num
:
number
=
1
)
{
if
(
!
num
||
num
<=
0
)
return
;
const
maxW
=
Global
.
MAP_WIDTH
/
2
-
100
;
const
maxH
=
Global
.
MAP_HEIGHT
/
2
-
100
;
for
(
let
i
=
1
;
i
<=
num
;
i
++
)
{
const
x
=
math
.
randomRange
(
-
maxW
,
maxW
);
const
y
=
math
.
randomRange
(
-
maxH
,
maxH
);
const
{
x
,
y
}
=
Global
.
getRandomPosition
(
100
);
const
node
=
instantiate
(
this
.
luckyBagPrefab
);
// 设置食物属性
...
...
assets/Scripts/Scenes/MainGame/Snake.ts
View file @
1cd3961f
...
...
@@ -436,13 +436,13 @@ export class Snake extends Component {
}
// 边界检查
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HEIGHT
/
2
;
const
mapHalfWidth
=
Global
.
HALF_MAP_WIDTH
;
const
mapHalfHeight
=
Global
.
HALF_MAP_HEIGHT
;
if
(
newHeadPos
.
x
<
=
-
mapHalfWidth
||
newHeadPos
.
x
>
=
mapHalfWidth
||
newHeadPos
.
y
<
=
-
mapHalfHeight
||
newHeadPos
.
y
>
=
mapHalfHeight
newHeadPos
.
x
<
-
mapHalfWidth
||
newHeadPos
.
x
>
mapHalfWidth
||
newHeadPos
.
y
<
-
mapHalfHeight
||
newHeadPos
.
y
>
mapHalfHeight
)
{
this
.
death
();
}
...
...
测试性能.mjs
0 → 100644
View file @
1cd3961f
const
now
=
performance
.
now
();
let
a
=
1
e10
;
for
(
let
i
=
0
;
i
<
1
e9
;
i
++
)
{
// a /= 2;
a
*=
0.5
;
}
console
.
log
(
performance
.
now
()
-
now
);
\ 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