Commit f5e3a6f2 authored by haiyoucuv's avatar haiyoucuv

init

parent f86dcb7f
......@@ -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) {
......
......@@ -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() {
......
......@@ -9,7 +9,7 @@ const { ccclass, property } = _decorator;
export class LuckyBag extends PropBase {
beEaten = (target: Snake) => {
target.luckNum += 1;
};
recycle() {
......
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);
}
}
}
......@@ -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() {
......
......@@ -34,6 +34,12 @@ export interface IStartInfo {
class GameStore {
gameInfo = {
length: 0,
killNum: 0,
luckNum: 0,
};
startInfo: IStartInfo = null;
async startGame() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment