Commit 5e372767 authored by haiyoucuv's avatar haiyoucuv

init

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