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
f5e3a6f2
Commit
f5e3a6f2
authored
Nov 26, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
f86dcb7f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
87 additions
and
73 deletions
+87
-73
MainGame.ts
assets/Scripts/Scenes/MainGame/MainGame.ts
+14
-67
Player.ts
assets/Scripts/Scenes/MainGame/Player.ts
+14
-4
LuckyBag.ts
assets/Scripts/Scenes/MainGame/Props/LuckyBag.ts
+1
-1
QuadTreeTools.ts
assets/Scripts/Scenes/MainGame/QuadTree/QuadTreeTools.ts
+43
-0
Snake.ts
assets/Scripts/Scenes/MainGame/Snake.ts
+9
-1
gameStore.ts
assets/Scripts/store/gameStore.ts
+6
-0
No files found.
assets/Scripts/Scenes/MainGame/MainGame.ts
View file @
f5e3a6f2
...
...
@@ -19,14 +19,14 @@ import Scene from "../../../Module/Scene";
import
{
executePreFrame
,
getItemGenerator
}
from
"../../Utils/ExecutePreFrame"
;
import
{
Player
}
from
"./Player"
;
import
{
AISnake
}
from
"./AISnake"
;
import
{
Quadtree
}
from
"./QuadTree/QuadTree"
;
import
{
QuadTreeNode
}
from
"./QuadTree/QuadTreeNode"
;
import
{
aiPool
,
clearAllPool
,
foodPool
}
from
"./Manager/CommonPool"
;
import
{
aiPool
,
clearAllPool
}
from
"./Manager/CommonPool"
;
import
{
useNick
}
from
"./Common/AINick"
;
import
{
observer
,
render
}
from
"../../store/decorators"
;
import
gameStore
from
"../../store/gameStore"
;
const
{
ccclass
,
property
}
=
_decorator
;
@
observer
@
ccclass
(
"MainGame"
)
export
class
MainGame
extends
Scene
{
...
...
@@ -66,16 +66,6 @@ export class MainGame extends Scene {
@
property
({
type
:
Label
,
group
:
"UI"
})
lengthTxt
:
Label
=
null
;
@
property
({
type
:
Label
,
group
:
"UI"
})
luckyNum
:
Label
=
null
;
private
_killNum
=
0
;
set
killNum
(
n
:
number
)
{
this
.
_killNum
=
n
;
this
.
killTxt
.
string
=
`
${
this
.
_killNum
}
名`
;
}
get
killNum
()
{
return
this
.
_killNum
;
}
private
state
:
GameState
=
GameState
.
READY
;
private
static
_ins
:
MainGame
=
null
;
...
...
@@ -83,19 +73,9 @@ export class MainGame extends Scene {
return
MainGame
.
_ins
;
}
// quadTree: Quadtree = null;
// private _lastQuadTreeUpdate: number = 0;
// private readonly QUAD_TREE_UPDATE_INTERVAL = 1 / 60; // 可以根据需求调整更新频率
async
onLoad
()
{
MainGame
.
_ins
=
this
;
// this.quadTree = new Quadtree({
// x: 0, y: 0,
// width: Global.MAP_WIDTH,
// height: Global.MAP_HEIGHT,
// }, 25, 6);
PhysicsSystem2D
.
instance
.
enable
=
true
;
// PhysicsSystem2D.instance.gravity = math.Vec2.ZERO;
// PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
...
...
@@ -132,6 +112,16 @@ export class MainGame extends Scene {
clearAllPool
();
}
@
render
render
()
{
const
{
length
,
killNum
,
luckNum
}
=
gameStore
.
gameInfo
||
{};
this
.
lengthTxt
.
string
=
`
${
length
}
`
;
this
.
killTxt
.
string
=
`
${
killNum
}
名`
;
this
.
luckyNum
.
string
=
`×
${
luckNum
}
`
;
}
update
(
dt
:
number
)
{
if
(
this
.
state
==
GameState
.
READY
)
return
;
...
...
@@ -144,51 +134,8 @@ export class MainGame extends Scene {
this
.
animalNode
.
children
.
forEach
(
child
=>
{
child
.
getComponent
(
AISnake
)?.
onUpdate
(
dt
);
});
// 更新四叉树
// this._lastQuadTreeUpdate += dt;
// if (this._lastQuadTreeUpdate >= this.QUAD_TREE_UPDATE_INTERVAL) {
// this.updateQuadTree();
// this._lastQuadTreeUpdate = 0;
// }
// // 处理碰撞检测
// this.handleCollisions();
}
// private updateQuadTree() {
// // 清空四叉树
// this.quadTree.clear();
// // 重新插入所有节点
// for (const node of QuadTreeNode.caches) {
// if (node.node.active && node.enabled) {
// this.quadTree.insert(node);
// }
// }
// for (const node of QuadTreeNode.staticCaches) {
// if (node.node.active && node.enabled) {
// this.quadTree.insert(node);
// }
// }
// }
// private handleCollisions() {
// // 使用四叉树进行碰撞检测
// for (const node of QuadTreeNode.caches) {
// if (!node.node.active) continue;
// const potentialCollisions = this.quadTree.retrieve(node);
// // 检查具体的碰撞
// for (const other of potentialCollisions) {
// if (node.intersects(other)) {
// // 处理碰撞
// node.emit(QuadTreeNode.EventType.COLLISION, node, other);
// }
// }
// }
// }
setGameState
(
state
:
GameState
)
{
this
.
state
=
Number
(
state
);
switch
(
this
.
state
)
{
...
...
assets/Scripts/Scenes/MainGame/Player.ts
View file @
f5e3a6f2
...
...
@@ -7,6 +7,7 @@ import { Joystick } from "./Components/Joystick";
import
{
FastBtn
}
from
"./Components/FastBtn"
;
import
{
Events
,
GameState
}
from
"./Common/Enums"
;
import
{
MainGame
}
from
"./MainGame"
;
import
gameStore
from
"../../store/gameStore"
;
const
{
ccclass
,
property
}
=
_decorator
;
...
...
@@ -31,13 +32,22 @@ export class Player extends Snake {
this
.
camera
.
orthoHeight
=
lerp
(
275
,
612
,
value
);
}
get
length
():
number
{
return
super
.
length
;
get
length
()
{
return
gameStore
.
gameInfo
.
length
;
}
set
length
(
value
:
number
)
{
super
.
length
=
value
;
this
.
updateLength
(
value
);
gameStore
.
gameInfo
.
length
=
value
;
}
get
luckNum
()
{
return
gameStore
.
gameInfo
.
luckNum
;
}
set
luckNum
(
value
:
number
)
{
gameStore
.
gameInfo
.
luckNum
=
value
;
}
onLoad
()
{
...
...
assets/Scripts/Scenes/MainGame/Props/LuckyBag.ts
View file @
f5e3a6f2
...
...
@@ -9,7 +9,7 @@ const { ccclass, property } = _decorator;
export
class
LuckyBag
extends
PropBase
{
beEaten
=
(
target
:
Snake
)
=>
{
target
.
luckNum
+=
1
;
};
recycle
()
{
...
...
assets/Scripts/Scenes/MainGame/QuadTree/QuadTreeTools.ts
0 → 100644
View file @
f5e3a6f2
import
{
QuadTreeNode
}
from
"./QuadTreeNode"
;
import
{
Quadtree
}
from
"./QuadTree"
;
// this.quadTree = new Quadtree({
// x: 0, y: 0,
// width: Global.MAP_WIDTH,
// height: Global.MAP_HEIGHT,
// }, 25, 6
export
function
handleCollisions
(
quadTree
:
Quadtree
)
{
// 使用四叉树进行碰撞检测
for
(
const
node
of
QuadTreeNode
.
caches
)
{
if
(
!
node
.
node
.
active
)
continue
;
const
potentialCollisions
=
quadTree
.
retrieve
(
node
);
// 检查具体的碰撞
for
(
const
other
of
potentialCollisions
)
{
if
(
node
.
intersects
(
other
))
{
// 处理碰撞
node
.
emit
(
QuadTreeNode
.
EventType
.
COLLISION
,
node
,
other
);
}
}
}
}
export
function
updateQuadTree
(
quadTree
:
Quadtree
)
{
// 清空四叉树
quadTree
.
clear
();
// 重新插入所有节点
for
(
const
node
of
QuadTreeNode
.
caches
)
{
if
(
node
.
node
.
active
&&
node
.
enabled
)
{
quadTree
.
insert
(
node
);
}
}
for
(
const
node
of
QuadTreeNode
.
staticCaches
)
{
if
(
node
.
node
.
active
&&
node
.
enabled
)
{
quadTree
.
insert
(
node
);
}
}
}
assets/Scripts/Scenes/MainGame/Snake.ts
View file @
f5e3a6f2
...
...
@@ -17,7 +17,6 @@ import { Global } from "./Global";
import
{
isIntersect
,
loadSkin
}
from
"./utils/uitl"
;
import
{
MainGame
}
from
"./MainGame"
;
import
{
bodyPool
}
from
"./Manager/CommonPool"
;
import
{
Food
}
from
"./Props/Food"
;
import
{
PropBase
}
from
"./Props/PropBase"
;
const
{
ccclass
,
property
}
=
_decorator
;
...
...
@@ -57,6 +56,15 @@ export class Snake extends Component {
// 蛇的状态
isLife
:
boolean
=
false
;
// 福袋个数
protected
_luckNum
=
0
;
get
luckNum
()
{
return
this
.
_luckNum
;
}
set
luckNum
(
value
:
number
)
{
this
.
_luckNum
=
value
;
}
private
_scale
:
number
=
0.2
;
get
scale
()
{
...
...
assets/Scripts/store/gameStore.ts
View file @
f5e3a6f2
...
...
@@ -34,6 +34,12 @@ export interface IStartInfo {
class
GameStore
{
gameInfo
=
{
length
:
0
,
killNum
:
0
,
luckNum
:
0
,
};
startInfo
:
IStartInfo
=
null
;
async
startGame
()
{
...
...
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