Commit 5e372767 authored by haiyoucuv's avatar haiyoucuv

init

parent 5454f9ee
......@@ -44,8 +44,6 @@ export class AISnake extends Snake {
aiPool.put(this.node);
console.log(MainGame.ins.animalNode.children.length)
MainGame.ins.initAnimal(1);
}
......
......@@ -3,6 +3,7 @@ import { Snake } from "./Snake";
import { DirectionType } from "./Common/Enums";
import { Global } from "./Global";
import { MainGame } from "./MainGame";
import { aiPool } from "./Manager/CommonPool";
const { ccclass, property } = _decorator;
......@@ -64,9 +65,10 @@ export class AISnake extends Snake {
super.death();
this.node.removeFromParent();
this.destroy();
MainGame.ins.initAnimal(1);
aiPool.put(this.node);
MainGame.ins.initAnimal(1);
}
private get difficultyParams() {
......
......@@ -30,6 +30,7 @@ export interface IInitConfig {
skinName?: string;
scale?: number;
bodyCount?: number;
initEnergy?: number,
}
@ccclass("Snake")
......@@ -61,6 +62,8 @@ 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;
}
......@@ -70,7 +73,9 @@ export class Snake extends Component {
const {
x = 0, y = 0, angle = 0, scale = 0.5,
skinName = "default", bodyCount = 5,
skinName = "default",
bodyCount = 5,
initEnergy = 5,
} = config;
await this.setSkin(skinName);
......@@ -93,31 +98,28 @@ export class Snake extends Component {
// this.head.getComponent(UITransform).anchorX = (bw / 2) / hw;
this.head.getComponent(UITransform).anchorX = (bw / 2) / hw;
// 创建身体节点
for (let i = 0; i < bodyCount; i++) {
const body = bodyPool.get() || instantiate(this.bodyPrefab);
const collider = body.getComponent(Collider2D);
collider.tag = this.tag;
body.angle = angle;
body.setPosition(-99999, -99999);
body.setScale(scale, scale);
this.node.addChild(body);
// 设置身体部分的贴图
if (i == bodyCount - 1 && this.imgTail) {
body.getComponent(Sprite).spriteFrame = this.imgTail;
const tw = this.imgTail.originalSize.width;
body.getComponent(UITransform).anchorX = (bw / 2) / tw;
} else {
body.getComponent(Sprite).spriteFrame = i % 2 == 0 ? this.imgBody1 : this.imgBody2;
}
this.addEnergy(initEnergy);
// body.active = false;
this.bodyArr.push(body);
// 创建尾巴节点
const tile = bodyPool.get() || instantiate(this.bodyPrefab);
tile.angle = angle;
tile.setPosition(x, y);
tile.setScale(scale, scale);
if (this.imgTail) {
tile.getComponent(Sprite).spriteFrame = this.imgTail;
const tw = this.imgTail.originalSize.width;
tile.getComponent(UITransform).anchorX = (bw / 2) / tw;
} else {
tile.getComponent(Sprite).spriteFrame = this.imgBody1;
}
const collider = tile.getComponent(Collider2D);
collider.tag = this.tag;
this.node.addChild(tile);
this.bodyArr.push(tile);
this.isLife = true;
this.ready = true;
}
......@@ -181,27 +183,29 @@ export class Snake extends Component {
this.energy += value;
const growthThreshold = Math.floor(4 * this.scale);
if (this.energy >= growthThreshold) {
while (this.energy >= growthThreshold) {
this.grow();
this.energy -= growthThreshold;
if (this.scale < 1) {
this.scale += 0.005;
}
this.speed = 600 * this.scale;
}
this.speed = 600 * this.scale;
}
// 蛇身体生长
private grow() {
let len = this.bodyArr.length;
if (this.imgTail) {
len -= 1;
}
if (this.imgTail) len -= 1;
const newBody = bodyPool.get() || instantiate(this.bodyPrefab);
newBody.angle = this.bodyArr[len - 1].angle;
newBody.setPosition(this.bodyArr[len - 1].getPosition());
const pre = this.bodyArr[len - 1] || this.head;
newBody.angle = pre.angle;
newBody.setPosition(pre.position);
newBody.setScale(this.scale, this.scale);
this.node.addChild(newBody);
......@@ -322,14 +326,15 @@ export class Snake extends Component {
this.isLife = false;
this.node.active = false;
const len = 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,
x: body.position.x + 10 - 5,
y: body.position.y + 10 - 5,
energy: ~~(this.energy / len),
};
});
......
......@@ -8,9 +8,9 @@ export function* getItemGenerator(length: number, func: (index: number) => any)
export function executePreFrame(generator: Generator, duration: number, context: Component) {
return new Promise<void>((resolve, reject) => {
let gen = generator;
const gen = generator;
// 创建执行函数
let execute = () => {
const execute = () => {
// 执行之前,先记录开始时间戳
let startTime = Date.now();
......
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