Commit 5454f9ee authored by haiyoucuv's avatar haiyoucuv

init

parent 61249a20
......@@ -190,7 +190,7 @@
"_priority": 40000,
"_fov": 45,
"_fovAxis": 0,
"_orthoHeight": 697.1836734693877,
"_orthoHeight": 810.6237288135594,
"_near": 0,
"_far": 2000,
"_color": {
......@@ -1737,7 +1737,7 @@
"_priority": 50000,
"_fov": 45,
"_fovAxis": 0,
"_orthoHeight": 697.1836734693877,
"_orthoHeight": 810.6237288135594,
"_near": 1,
"_far": 2000,
"_color": {
......@@ -2171,7 +2171,7 @@
"__prefab": null,
"_contentSize": {
"__type__": "cc.Size",
"width": 1624,
"width": 1624.0000000000002,
"height": 750
},
"_anchorPoint": {
......@@ -2439,7 +2439,7 @@
"__prefab": null,
"_contentSize": {
"__type__": "cc.Size",
"width": 1624,
"width": 1624.0000000000002,
"height": 750
},
"_anchorPoint": {
......
......@@ -331,10 +331,7 @@
"b": 255,
"a": 255
},
"_spriteFrame": {
"__uuid__": "5a57c4ce-88f6-4582-af09-4eff049952e0@9296f",
"__expectedType__": "cc.SpriteFrame"
},
"_spriteFrame": null,
"_type": 0,
"_fillType": 0,
"_sizeMode": 1,
......
......@@ -31,10 +31,13 @@
},
{
"__id__": 8
},
{
"__id__": 10
}
],
"_prefab": {
"__id__": 10
"__id__": 12
},
"_lpos": {
"__type__": "cc.Vec3",
......@@ -202,6 +205,24 @@
"__type__": "cc.CompPrefabInfo",
"fileId": "bbd/hzdzVJ1rG0lXtNgF7s"
},
{
"__type__": "55174qkRKhBO78kTSc2IvFc",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 11
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "d6rQ0kR1dF0Z5Wb7aK/Nmz"
},
{
"__type__": "cc.PrefabInfo",
"root": {
......
......@@ -2,6 +2,7 @@ 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;
......@@ -40,7 +41,8 @@ export class AISnake extends Snake {
super.death();
this.node.removeFromParent();
this.destroy();
aiPool.put(this.node);
console.log(MainGame.ins.animalNode.children.length)
......
import {
_decorator,
Camera,
director,
director, instantiate,
Label,
math,
Node,
......@@ -11,10 +11,9 @@ import {
} from "cc";
import { Joystick } from "./Components/Joystick";
import { FastBtn } from "./Components/FastBtn";
import { FondManger } from "./Manager/FondManger";
import { FoodManger } from "./Manager/FoodManger";
import { Global } from "./Global";
import { Events, GameState } from "./Common/Enums";
import { PoolManager } from "./Manager/PoolManager";
import { showToast } from "../../../Module/UIFast";
import Scene from "../../../Module/Scene";
import { executePreFrame, getItemGenerator } from "../../Utils/ExecutePreFrame";
......@@ -22,6 +21,7 @@ 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";
const { ccclass, property } = _decorator;
......@@ -61,8 +61,8 @@ export class MainGame extends Scene {
@property(Player)
player: Player = null;
@property(FondManger)
fondManger: FondManger = null;
@property(FoodManger)
fondManger: FoodManger = null;
@property(Node)
animalNode: Node = null;
......@@ -139,6 +139,7 @@ export class MainGame extends Scene {
onDestroy() {
MainGame._ins = null;
clearAllPool();
}
update(dt: number) {
......@@ -240,7 +241,7 @@ export class MainGame extends Scene {
}
initItem = (index: number) => {
const node = PoolManager.instance.getNode(this.animalPrefab);
const node = aiPool.get() || instantiate(this.animalPrefab);
const x = math.randomRangeInt(-(Global.MAP_WIDTH / 2 - 50), Global.MAP_WIDTH / 2 - 50);
const y = math.randomRangeInt(-(Global.MAP_HEIGHT / 2 - 50), Global.MAP_HEIGHT / 2 - 50);
......@@ -248,7 +249,7 @@ export class MainGame extends Scene {
x, y,
// x: 50,
// y: 50,
bodyCount: 100,
// bodyCount: 100,
angle: math.randomRangeInt(0, 360),
skinName: "default",
});
......
import { NodePool } from "cc";
export const foodPool = new NodePool();
export const bodyPool = new NodePool();
export const aiPool = new NodePool();
export function clearAllPool() {
foodPool.clear();
bodyPool.clear();
aiPool.clear();
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "bdf18a2f-1ee1-4fcd-bcf5-e17972dee33d",
"uuid": "f68fa372-122a-4b96-89ea-35313b0129a6",
"files": [],
"subMetas": {},
"userData": {}
......
import { _decorator, Enum, SpriteFrame, math, v3, Sprite, Collider2D, Component, Prefab, ccenum } from "cc";
import { PoolManager } from "db://assets/Scripts/Scenes/MainGame/Manager/PoolManager";
import {
_decorator,
SpriteFrame,
math,
Sprite,
Collider2D,
Component,
Prefab,
instantiate, Vec2
} from "cc";
import { FoodType } from "db://assets/Scripts/Scenes/MainGame/Common/Enums";
import { Global } from "db://assets/Scripts/Scenes/MainGame/Global";
import { executePreFrame, getItemGenerator } from "db://assets/Scripts/Utils/ExecutePreFrame";
import { Food } from "../Props/Food";
import { foodPool } from "./CommonPool";
const { ccclass, property } = _decorator;
/**
* 食物管理器
*/
@ccclass("FondManger")
export class FondManger extends Component {
@ccclass("FoodManger")
export class FoodManger extends Component {
@property({ type: [SpriteFrame] })
private foods: SpriteFrame[] = [];
......@@ -20,6 +30,9 @@ export class FondManger extends Component {
maxFood: number = 100;
onLoad() {
}
/**
* 获取当前食物数量
*/
......@@ -32,9 +45,7 @@ export class FondManger extends Component {
*/
async init(maxFood: number = 100) {
this.maxFood = maxFood;
await this.initFond(maxFood);
await executePreFrame(getItemGenerator(maxFood, this.initItem), 1, this);
this.schedule(this.checkFood, 1);
}
......@@ -50,7 +61,7 @@ export class FondManger extends Component {
/**
* 设置食物
*/
addFood(x?: number, y?: number) {
addFood(x?: number, y?: number, energy: number = 1) {
// 如果没有指定位置,随机生成位置
if (!x) {
......@@ -61,26 +72,25 @@ export class FondManger extends Component {
y = math.randomRangeInt(-(Global.MAP_HEIGHT / 2 - 50), Global.MAP_HEIGHT / 2 - 50);
}
// 从对象池获取食物节点
const node = PoolManager.instance.getNode(this.foodPrefab);
const food = foodPool.get() || instantiate(this.foodPrefab);
// 随机选择食物类型
const index = math.randomRangeInt(0, this.foods.length);
this.node.addChild(node);
// 设置食物属性
node.angle = math.randomRange(0, 360);
node.setScale(1, 1);
node.setPosition(x, y);
node.getComponent(Sprite).spriteFrame = this.foods[index];
food.angle = math.randomRange(0, 360);
food.setPosition(x, y);
food.getComponent(Sprite).spriteFrame = this.foods[index];
food.getComponent(Food).init(energy);
this.node.addChild(food);
// 设置碰撞类型
const collider = node.getComponent(Collider2D);
const collider = food.getComponent(Collider2D);
if (collider) {
collider.tag = FoodType.FOOD;
}
node.active = true;
food.active = true;
}
/**
......@@ -94,11 +104,15 @@ export class FondManger extends Component {
this.addFood(x, y);
};
/**
* 初始化食物
*/
async initFond(count: number) {
await executePreFrame(getItemGenerator(count, this.initItem), 1, this);
async initFoods(infoArr: { x: number, y: number, energy: number }[]) {
const initItem = (index: number) => {
const bp = infoArr[index];
this.addFood(bp.x, bp.y, bp.energy);
};
await executePreFrame(getItemGenerator(infoArr.length, initItem), 1, this);
}
}
import { _decorator, instantiate, Node, NodePool, Prefab, Vec3 } from "cc";
const { ccclass } = _decorator;
/**
* 对象池管理器
*/
@ccclass("PoolManager")
export class PoolManager {
/** 单例实例 */
private static _instance: PoolManager;
/** 对象池字典 */
private _dictPool: { [key: string]: NodePool } = {};
/** 预制体字典 */
private _dictPrefab: { [key: string]: Node } = {};
/** 获取单例 */
public static get instance(): PoolManager {
if (!this._instance) {
this._instance = new PoolManager();
}
return this._instance;
}
/**
* 复制节点
* @param sourceNode 源节点
* @param parent 父节点
* @returns 复制的节点
*/
public copyNode(sourceNode: Node, parent?: Node): Node {
const name = sourceNode.name;
this._dictPrefab[name] = sourceNode;
let node = null;
if (this._dictPool.hasOwnProperty(name)) {
// 从对象池获取
const pool = this._dictPool[name];
node = pool.size() > 0 ? pool.get() : instantiate(sourceNode);
} else {
// 创建新的对象池
const pool = new NodePool();
this._dictPool[name] = pool;
node = instantiate(sourceNode);
}
if (parent) {
node.parent = parent;
node.active = true;
}
return node;
}
/**
* 获取节点
* @param prefab 预制体或预制体名称
* @param parent 父节点
* @param position 位置
* @returns 节点实例
*/
public getNode(prefab: Node | Prefab | string, parent?: Node, position?: Vec3): Node {
let prefabNode: Node | Prefab;
let prefabName: string;
if (typeof prefab === "string") {
prefabName = prefab;
prefabNode = this._dictPrefab[prefab];
if (!prefabNode) {
console.log("Pool invalid prefab name = ", prefabName);
return null;
}
} else {
prefabNode = prefab;
prefabName = prefab["data"].name;
}
let node = null;
if (this._dictPool.hasOwnProperty(prefabName)) {
// 从对象池获取
const pool = this._dictPool[prefabName];
node = pool.size() > 0 ? pool.get() : instantiate(prefabNode);
} else {
// 创建新的对象池
this._dictPool[prefabName] = new NodePool();
node = instantiate(prefabNode);
}
if (parent) {
node.parent = parent;
node.active = true;
if (position) {
node.position = position;
}
}
return node;
}
/**
* 回收节点
* @param node 要回收的节点
*/
public putNode(node: Node): void {
if (!node) {
console.log("putNode-node is null");
return;
}
const name = node.name;
let pool: NodePool = null;
if (this._dictPool.hasOwnProperty(name)) {
pool = this._dictPool[name];
} else {
pool = new NodePool();
this._dictPool[name] = pool;
}
pool.put(node);
}
/**
* 清空指定名称的对象池
* @param name 对象池名称
*/
public clearPool(name: string): void {
if (this._dictPool.hasOwnProperty(name)) {
this._dictPool[name].clear();
}
}
/**
* 设置预制体
* @param name 预制体名称
* @param prefab 预制体
*/
public setPrefab(name: string, prefab: Node): void {
this._dictPrefab[name] = prefab;
}
/**
* 获取预制体
* @param name 预制体名称
* @returns 预制体
*/
public getPrefab(name: string): Node {
return this._dictPrefab[name];
}
}
\ No newline at end of file
import { _decorator, Component } from "cc";
import { foodPool } from "../Manager/CommonPool";
const { ccclass, property } = _decorator;
@ccclass("Food")
export class Food extends Component {
_energy: number = 1;
@property
get energy() {
return this._energy;
}
set energy(energy: number) {
this._energy = energy;
const scale = 1 + (energy - 1) / 10;
this.node.scale.set(scale, scale);
}
init(energy: number = 1) {
this.energy = energy;
}
protected start() {
}
recycle() {
this.node.removeFromParent();
foodPool.put(this.node);
}
}
\ No newline at end of file
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "55174aa4-44a8-413b-bf24-4d273622f15c",
"files": [],
"subMetas": {},
"userData": {}
}
......@@ -2,7 +2,7 @@ import {
_decorator,
Collider2D,
Component,
Contact2DType,
Contact2DType, instantiate, IVec2Like,
math,
Node,
PhysicsGroup,
......@@ -11,15 +11,15 @@ import {
SpriteFrame,
tween, UITransform,
v2,
v3,
v3, Vec2,
Vec3,
} from "cc";
import { FoodType } from "./Common/Enums";
import { Global } from "./Global";
import { PoolManager } from "./Manager/PoolManager";
import { isIntersect, loadSkin } from "./utils/uitl";
import { MainGame } from "./MainGame";
import { executePreFrame, getItemGenerator } from "../../Utils/ExecutePreFrame";
import { bodyPool } from "./Manager/CommonPool";
import { Food } from "./Props/Food";
const { ccclass, property } = _decorator;
......@@ -32,7 +32,6 @@ export interface IInitConfig {
bodyCount?: number;
}
@ccclass("Snake")
export class Snake extends Component {
......@@ -96,7 +95,7 @@ export class Snake extends Component {
// 创建身体节点
for (let i = 0; i < bodyCount; i++) {
const body = PoolManager.instance.getNode(this.bodyPrefab, this.node);
const body = bodyPool.get() || instantiate(this.bodyPrefab);
const collider = body.getComponent(Collider2D);
collider.tag = this.tag;
......@@ -104,6 +103,8 @@ export class Snake extends Component {
body.setPosition(-99999, -99999);
body.setScale(scale, scale);
this.node.addChild(body);
// 设置身体部分的贴图
if (i == bodyCount - 1 && this.imgTail) {
body.getComponent(Sprite).spriteFrame = this.imgTail;
......@@ -148,7 +149,7 @@ export class Snake extends Component {
// 碰撞检测
private onBeginHead(selfCollider: Collider2D, otherCollider: Collider2D) {
if (otherCollider.group === PhysicsGroup["Body"] && otherCollider.tag != this.tag) {
// this.death();
this.death();
}
}
......@@ -163,7 +164,7 @@ export class Snake extends Component {
scale: v3(0, 0)
})
.call(() => {
PoolManager.instance.putNode(otherCollider.node);
otherCollider.node.getComponent(Food).recycle();
if (!this.isLife) return;
......@@ -197,10 +198,12 @@ export class Snake extends Component {
if (this.imgTail) {
len -= 1;
}
const newBody = PoolManager.instance.getNode(this.bodyPrefab, this.node);
const newBody = bodyPool.get() || instantiate(this.bodyPrefab);
newBody.angle = this.bodyArr[len - 1].angle;
newBody.setPosition(this.bodyArr[len - 1].getPosition());
newBody.setScale(this.scale, this.scale);
this.node.addChild(newBody);
newBody.getComponent(Sprite).spriteFrame = len % 2 == 0 ? this.imgBody1 : this.imgBody2;
newBody.getComponent(Collider2D).tag = this.tag;
......@@ -319,8 +322,18 @@ export class Snake extends Component {
this.isLife = false;
this.node.active = false;
this.initFond(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,
};
});
MainGame.ins.fondManger.initFoods(foodArr);
}
protected getNewPos(angle: number, dt: number, currentPos: Vec3, speed: number = this.speed): Vec3 {
......@@ -333,30 +346,6 @@ export class Snake extends Component {
return v2(Math.cos(radian), Math.sin(radian));
}
/**
* 初始化食物
*/
initItem = (index: number) => {
const bp = this.bodyArr[index].getPosition();
MainGame.ins.fondManger.addFood(
math.randomRangeInt(bp.x - 5, bp.x + 5),
math.randomRangeInt(bp.y - 5, bp.y + 5)
);
this.bodyArr[index].setPosition(9999, 9999);
this.bodyArr[index].active = false;
};
/**
* 初始化食物
*/
async initFond(count: number) {
console.log(count);
await executePreFrame(getItemGenerator(count, this.initItem), 1, this);
this.ready = true;
}
/**
* 护盾时间
* 假如有两个护盾同时生效,则取时间大的,正是因为这样,才设计成数字
......
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