Commit 1c3ec0d6 authored by haiyoucuv's avatar haiyoucuv

init

parent d973657c
...@@ -1275,7 +1275,7 @@ ...@@ -1275,7 +1275,7 @@
"_id": "4b4n24QAlBAJTbqsXR4lUh" "_id": "4b4n24QAlBAJTbqsXR4lUh"
}, },
{ {
"__type__": "21251JLe89C+bNR2HvMGDkB", "__type__": "82c44Ork3tL+o56WmoE8TcQ",
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
...@@ -1314,7 +1314,7 @@ ...@@ -1314,7 +1314,7 @@
"camera": { "camera": {
"__id__": 4 "__id__": 4
}, },
"_id": "17e87LmmFPGJM7y37eKcPY" "_id": "212vDeP7dNBZXi0fJjPuCy"
}, },
{ {
"__type__": "4552b9T1AJHJ4xMNlpYdqL/", "__type__": "4552b9T1AJHJ4xMNlpYdqL/",
......
...@@ -6,7 +6,7 @@ const {ccclass, property} = _decorator; ...@@ -6,7 +6,7 @@ const {ccclass, property} = _decorator;
@ccclass() @ccclass()
export default class Scene extends Module { export default class Scene extends Module {
@property(UILayer) // @property(UILayer)
uiLayer: UILayer = null; // uiLayer: UILayer = null;
} }
...@@ -7,7 +7,7 @@ import { Global } from "./Global"; ...@@ -7,7 +7,7 @@ import { Global } from "./Global";
import { PoolManager } from "./PoolManager"; import { PoolManager } from "./PoolManager";
import { FoodType, DirectionType } from "./Enums"; import { FoodType, DirectionType } from "./Enums";
import { isIntersect } from "./uitl"; import { isIntersect } from "./uitl";
import { GamePlayer } from "./GamePlayer"; import { MainGame } from "./MainGame";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
...@@ -101,7 +101,7 @@ export class Animal extends Component { ...@@ -101,7 +101,7 @@ export class Animal extends Component {
this.Head.getComponent(Sprite).spriteFrame = this.imgHead; this.Head.getComponent(Sprite).spriteFrame = this.imgHead;
this.Head.active = isIntersect( this.Head.active = isIntersect(
this.Head.getPosition(), this.Head.getPosition(),
GamePlayer.ins.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -148,7 +148,7 @@ export class Animal extends Component { ...@@ -148,7 +148,7 @@ export class Animal extends Component {
} }
this.uiSanke = this.getComponent(UITransform); this.uiSanke = this.getComponent(UITransform);
this.uiFondManger = Global.gamePlayer.fondManger.node.getComponent(UITransform); this.uiFondManger = MainGame.ins.fondManger.node.getComponent(UITransform);
} }
onDisable() { onDisable() {
...@@ -247,7 +247,7 @@ export class Animal extends Component { ...@@ -247,7 +247,7 @@ export class Animal extends Component {
body.active = isIntersect( body.active = isIntersect(
body.getPosition(), body.getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -280,7 +280,7 @@ export class Animal extends Component { ...@@ -280,7 +280,7 @@ export class Animal extends Component {
this.Bodys[i].setSiblingIndex(len - i); this.Bodys[i].setSiblingIndex(len - i);
this.Bodys[i].active = isIntersect( this.Bodys[i].active = isIntersect(
this.Bodys[i].getPosition(), this.Bodys[i].getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -293,7 +293,7 @@ export class Animal extends Component { ...@@ -293,7 +293,7 @@ export class Animal extends Component {
this.Bodys[1].setSiblingIndex(len - 1); this.Bodys[1].setSiblingIndex(len - 1);
this.Bodys[1].active = isIntersect( this.Bodys[1].active = isIntersect(
this.Bodys[1].getPosition(), this.Bodys[1].getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -305,7 +305,7 @@ export class Animal extends Component { ...@@ -305,7 +305,7 @@ export class Animal extends Component {
this.Bodys[0].setSiblingIndex(len); this.Bodys[0].setSiblingIndex(len);
this.Bodys[0].active = isIntersect( this.Bodys[0].active = isIntersect(
this.Bodys[0].getPosition(), this.Bodys[0].getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -318,7 +318,7 @@ export class Animal extends Component { ...@@ -318,7 +318,7 @@ export class Animal extends Component {
this.Head.setSiblingIndex(len + 1); this.Head.setSiblingIndex(len + 1);
this.Head.active = isIntersect( this.Head.active = isIntersect(
this.Head.getPosition(), this.Head.getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -340,7 +340,7 @@ export class Animal extends Component { ...@@ -340,7 +340,7 @@ export class Animal extends Component {
this.Bodys[i].setSiblingIndex(len - i); this.Bodys[i].setSiblingIndex(len - i);
this.Bodys[i].active = isIntersect( this.Bodys[i].active = isIntersect(
this.Bodys[i].getPosition(), this.Bodys[i].getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -353,7 +353,7 @@ export class Animal extends Component { ...@@ -353,7 +353,7 @@ export class Animal extends Component {
this.Bodys[0].setSiblingIndex(len); this.Bodys[0].setSiblingIndex(len);
this.Bodys[0].active = isIntersect( this.Bodys[0].active = isIntersect(
this.Bodys[0].getPosition(), this.Bodys[0].getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -366,7 +366,7 @@ export class Animal extends Component { ...@@ -366,7 +366,7 @@ export class Animal extends Component {
this.Head.setSiblingIndex(len + 1); this.Head.setSiblingIndex(len + 1);
this.Head.active = isIntersect( this.Head.active = isIntersect(
this.Head.getPosition(), this.Head.getPosition(),
Global.gamePlayer.camera.node.getPosition(), MainGame.ins.camera.node.getPosition(),
this.vw, this.vw,
this.vh this.vh
); );
...@@ -486,7 +486,7 @@ export class Animal extends Component { ...@@ -486,7 +486,7 @@ export class Animal extends Component {
*/ */
private _initItem(index: number) { private _initItem(index: number) {
this.b = this.Bodys[index].getPosition(); this.b = this.Bodys[index].getPosition();
Global.gamePlayer.fondManger.setFood( MainGame.ins.fondManger.setFood(
math.randomRangeInt(this.b.x - 10, this.b.x + 11), math.randomRangeInt(this.b.x - 10, this.b.x + 11),
math.randomRangeInt(this.b.y - 20, this.b.y + 21) math.randomRangeInt(this.b.y - 20, this.b.y + 21)
); );
......
import {
_decorator,
UITransform,
Node,
Label,
Camera,
director,
math,
Component,
Prefab,
PhysicsSystem2D,
PhysicsGroup
} from "cc";
import { AngleBtn } from "./AngleBtn";
import { Snake } from "./Snake";
import { FastBtn } from "./FastBtn";
import { Animal } from "./Animal";
import { FondManger } from "./FondManger";
import { Config } from "./Config";
import { Global } from "./Global";
import { GameState, Events } from "./Enums";
import { PoolManager } from "./PoolManager";
import { showToast } from "../../../Module/UIFast";
const { ccclass, property } = _decorator;
@ccclass("GamePlayer")
export class GamePlayer extends Component {
@property(Prefab)
private animalPrefab: Prefab = null;
@property({
displayName: "最多食物",
tooltip: "地图上随机产生食物,如果超过当前值不在产生。"
})
private maxFood: number = 200;
@property({
displayName: "NPC数量",
tooltip: "当前游戏最大NPC角色"
})
private maxAnimal: number = 20;
@property({
type: AngleBtn,
displayName: "旋转按钮"
})
private angleBtn: AngleBtn = null;
@property({
type: FastBtn,
displayName: "快进按钮"
})
private fashBtn: FastBtn = null;
@property(UITransform)
private uiBg: UITransform = null;
@property(Snake)
private snake: Snake = null;
@property(FondManger)
private fondManger: FondManger = null;
@property(Node)
private animalNode: Node = null;
@property(Label)
private LTips: Label = null;
@property(Camera)
camera: Camera = null;
private state: GameState = GameState.READY;
private rebirthSum: number = 0;
private static _ins: GamePlayer = null;
static get ins(): GamePlayer {
return GamePlayer._ins;
}
onLoad() {
GamePlayer._ins = this;
PhysicsSystem2D.instance.enable = true;
Global.gamePlayer = this;
Global.MAP_WIDTH = this.uiBg.contentSize.x;
Global.MAP_HIGHT = this.uiBg.contentSize.y;
// 初始化墙壁
const wallTop = this.uiBg.node.getChildByName("WALL_TOP");
wallTop.setPosition(0, Global.MAP_HIGHT / 2);
wallTop.getComponent(UITransform).width = Global.MAP_WIDTH;
const wallBottom = this.uiBg.node.getChildByName("WALL_BOTTOM");
wallBottom.setPosition(0, -Global.MAP_HIGHT / 2);
wallBottom.getComponent(UITransform).width = Global.MAP_WIDTH;
const wallLeft = this.uiBg.node.getChildByName("WALL_LEFT");
wallLeft.setPosition(-Global.MAP_WIDTH / 2, 0);
wallLeft.getComponent(UITransform).height = Global.MAP_HIGHT;
const wallRight = this.uiBg.node.getChildByName("WALL_RIGHT");
wallRight.setPosition(Global.MAP_WIDTH / 2, 0);
wallRight.getComponent(UITransform).height = Global.MAP_HIGHT;
// 初始化蛇
const skinId = Global.skinId;
this.snake.init(0, 0, 0, skinId, 0.2,
Config.SKIN_STYLE[skinId].len,
Config.SKIN_STYLE[skinId].taitLen, 0);
// 初始化食物和NPC
this.fondManger.init(this.maxFood);
this.initAnimal(this.maxAnimal);
// 设置游戏状态
this.setGameState(GameState.PLAY);
// 注册事件
director.on(Events.showGOver, this.showGOver, this);
director.on(Events.setGameState, this.setGameState, this);
}
onDestroy() {
GamePlayer._ins = null;
}
update(dt: number) {
if (this.state == GameState.READY) return;
// 更新UI提示
this.LTips.string = `长度:${this.snake.getSnakeLen()} 金币:${this.snake.getmoneySum()}`;
// 更新蛇的移动
this.snake.setAngle((360 - this.angleBtn.angle) % 360);
if (this.fashBtn.isFash) {
this.snake.running(dt);
} else {
this.snake.move(dt);
}
// 更新NPC移动
this.animalNode.children.forEach(child => {
child.getComponent(Animal)?.autoMove(dt);
});
}
onPause() {
this.setGameState(GameState.PAUSE);
console.log("pause");
}
setGameState(state: GameState) {
this.state = Number(state);
switch (this.state) {
case GameState.READY:
break;
case GameState.PLAY:
director.resume();
break;
case GameState.PAUSE:
director.pause();
break;
case GameState.OVER:
this.rebirthSum++;
if (this.rebirthSum > 2) return this.showGOver();
showToast("你已死亡!");
break;
case GameState.WIN:
director.pause();
console.log("win", this.snake.getSnakeLen(), this.snake.getmoneySum());
break;
case GameState.QUIT:
director.resume();
director.loadScene("GameMain");
break;
default:
console.log("err");
}
}
play() {
if (this.state == GameState.OVER) {
showToast("你已复活!");
this.state = GameState.PLAY;
this.snake.reInit(
math.randomRangeInt(-(Global.MAP_WIDTH / 2 - 50), Global.MAP_WIDTH / 2 - 50),
math.randomRangeInt(-(Global.MAP_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50)
);
// this.showGOver();
}
}
showGOver() {
console.log("showGOver", this.snake.getSnakeLen(), this.snake.getmoneySum());
}
private* _getItemGenerator(count: number) {
for (let i = 0; i < count; i++) {
yield this._initItem(i);
}
}
private _initItem(index: number) {
const node = PoolManager.instance.getNode(this.animalPrefab, this.animalNode);
const x = math.randomRangeInt(-(Global.MAP_WIDTH / 2 - 50), Global.MAP_WIDTH / 2 - 50);
const y = math.randomRangeInt(-(Global.MAP_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50);
// const skinId = math.randomRangeInt(0, Config.SKIN_STYLE.length);
const skinId = 0;
node.getComponent(Animal)?.init(
x, y,
math.randomRangeInt(0, 360),
skinId, 0.2,
Config.SKIN_STYLE[skinId].len,
Config.SKIN_STYLE[skinId].taitLen,
index + 1
);
}
async initAnimal(count: number) {
await this.executePreFrame(this._getItemGenerator(count), 1);
}
executePreFrame(generator: Generator, frameTime: number): Promise<boolean> {
return new Promise((resolve, reject) => {
const iter = generator;
const next = () => {
const startTime = new Date().getTime();
for (let result = iter.next(); ; result = iter.next()) {
if (!result || result.done) {
resolve(true);
return;
}
if (new Date().getTime() - startTime > frameTime) {
this.scheduleOnce(() => {
next();
});
return;
}
}
};
next();
});
}
}
\ No newline at end of file
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "2125124b-7bcf-42f9-b351-d87bcc183901",
"files": [],
"subMetas": {},
"userData": {}
}
...@@ -6,23 +6,6 @@ const { ccclass } = _decorator; ...@@ -6,23 +6,6 @@ const { ccclass } = _decorator;
*/ */
@ccclass('Global') @ccclass('Global')
export class Global { export class Global {
/** 插屏广告ID */
public static intersitialAdUnitId: string;
/** 激励视频广告ID */
public static rewardedVideoAdUnitId: string;
/** Banner广告ID */
public static bannerAdAdUnitId: string;
/** 自定义广告ID */
public static customAdUnitId: string;
/** 加载进度 */
public static LoadingRate: number = 0;
/** 调试模式 */
public static Debug: boolean = false;
/** 设计分辨率 */ /** 设计分辨率 */
public static designResolutionSize = View.instance.getDesignResolutionSize(); public static designResolutionSize = View.instance.getDesignResolutionSize();
...@@ -35,9 +18,6 @@ export class Global { ...@@ -35,9 +18,6 @@ export class Global {
(Global.visibleSize.width / Global.designResolutionSize.width) * 100 (Global.visibleSize.width / Global.designResolutionSize.width) * 100
) / 100; ) / 100;
/** 游戏玩家实例 */
public static gamePlayer = null;
/** 地图宽度 */ /** 地图宽度 */
public static MAP_WIDTH: number = 5000; public static MAP_WIDTH: number = 5000;
......
import { _decorator, EventKeyboard, Input, input, KeyCode, Node, } from "cc"; import {
import Scene from "db://assets/Module/Scene"; _decorator,
UITransform,
Node,
Label,
Camera,
director,
math,
Prefab,
PhysicsSystem2D,
} from "cc";
import { AngleBtn } from "./AngleBtn";
import { Snake } from "./Snake"; import { Snake } from "./Snake";
import { FastBtn } from "./FastBtn";
import { Animal } from "./Animal";
import { FondManger } from "./FondManger";
import { Config } from "./Config";
import { Global } from "./Global";
import { GameState, Events } from "./Enums";
import { PoolManager } from "./PoolManager";
import { showToast } from "../../../Module/UIFast";
import Scene from "../../../Module/Scene";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
export enum DIR {
UP,
DOWN,
LEFT,
RIGHT,
}
@ccclass("MainGame") @ccclass("MainGame")
export class MainGame extends Scene { export class MainGame extends Scene {
static bundle: string = "MainGame"; static bundle: string = "MainGame";
static skin: string = "MainGame"; static skin: string = "MainGame";
@property(Prefab)
private animalPrefab: Prefab = null;
@property({
displayName: "最多食物",
tooltip: "地图上随机产生食物,如果超过当前值不在产生。"
})
maxFood: number = 200;
@property({
displayName: "NPC数量",
tooltip: "当前游戏最大NPC角色"
})
private maxAnimal: number = 20;
@property({
type: AngleBtn,
displayName: "旋转按钮"
})
private angleBtn: AngleBtn = null;
@property({
type: FastBtn,
displayName: "快进按钮"
})
private fashBtn: FastBtn = null;
@property(UITransform)
private uiBg: UITransform = null;
@property(Snake)
private snake: Snake = null;
@property(FondManger)
fondManger: FondManger = null;
@property(Node) @property(Node)
snake: Node = null; private animalNode: Node = null;
@property @property(Label)
speed: number = 200; private LTips: Label = null;
onLoad() { @property(Camera)
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this); camera: Camera = null;
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
input.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this); private state: GameState = GameState.READY;
private rebirthSum: number = 0;
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this); private static _ins: MainGame = null;
input.on(Input.EventType.KEY_UP, this.onKeyUp, this); static get ins(): MainGame {
return MainGame._ins;
} }
curDir: DIR = DIR.UP; onLoad() {
MainGame._ins = this;
PhysicsSystem2D.instance.enable = true;
keyArr = []; Global.MAP_WIDTH = this.uiBg.contentSize.x;
Global.MAP_HIGHT = this.uiBg.contentSize.y;
async start() { // 初始化墙壁
// this.schedule(() => { const wallTop = this.uiBg.node.getChildByName("WALL_TOP");
// this.snake.getComponent(Snake).addNode(); wallTop.setPosition(0, Global.MAP_HIGHT / 2);
// }, 1); wallTop.getComponent(UITransform).width = Global.MAP_WIDTH;
const wallBottom = this.uiBg.node.getChildByName("WALL_BOTTOM");
wallBottom.setPosition(0, -Global.MAP_HIGHT / 2);
wallBottom.getComponent(UITransform).width = Global.MAP_WIDTH;
const wallLeft = this.uiBg.node.getChildByName("WALL_LEFT");
wallLeft.setPosition(-Global.MAP_WIDTH / 2, 0);
wallLeft.getComponent(UITransform).height = Global.MAP_HIGHT;
const wallRight = this.uiBg.node.getChildByName("WALL_RIGHT");
wallRight.setPosition(Global.MAP_WIDTH / 2, 0);
wallRight.getComponent(UITransform).height = Global.MAP_HIGHT;
// 初始化蛇
const skinId = Global.skinId;
this.snake.init(0, 0, 0, skinId, 0.2,
Config.SKIN_STYLE[skinId].len,
Config.SKIN_STYLE[skinId].taitLen, 0);
// 初始化食物和NPC
this.fondManger.init(this.maxFood);
this.initAnimal(this.maxAnimal);
// 设置游戏状态
this.setGameState(GameState.PLAY);
// 注册事件
director.on(Events.showGOver, this.showGOver, this);
director.on(Events.setGameState, this.setGameState, this);
} }
onDestroy() { onDestroy() {
input.off(Input.EventType.TOUCH_START, this.onTouchStart, this); MainGame._ins = null;
input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
input.off(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
} }
onKeyDown(event: EventKeyboard) { update(dt: number) {
if (this.state == GameState.READY) return;
const keyArr = [ // 更新UI提示
KeyCode.KEY_W, this.LTips.string = `长度:${this.snake.getSnakeLen()} 金币:${this.snake.getmoneySum()}`;
KeyCode.KEY_S,
KeyCode.KEY_A,
KeyCode.KEY_D,
];
if (keyArr.indexOf(event.keyCode) > -1) { // 更新蛇的移动
this.keyArr.push(event.keyCode); this.snake.setAngle((360 - this.angleBtn.angle) % 360);
if (this.fashBtn.isFash) {
this.snake.running(dt);
} else {
this.snake.move(dt);
} }
this.setDir(); // 更新NPC移动
this.animalNode.children.forEach(child => {
child.getComponent(Animal)?.autoMove(dt);
});
} }
onKeyUp(event: EventKeyboard) { onPause() {
const index = this.keyArr.indexOf(event.keyCode); this.setGameState(GameState.PAUSE);
if (index > -1) { console.log("pause");
this.keyArr.splice(index, 1);
} }
this.setDir(); setGameState(state: GameState) {
this.state = Number(state);
switch (this.state) {
case GameState.READY:
break;
case GameState.PLAY:
director.resume();
break;
case GameState.PAUSE:
director.pause();
break;
case GameState.OVER:
this.rebirthSum++;
if (this.rebirthSum > 2) return this.showGOver();
showToast("你已死亡!");
break;
case GameState.WIN:
director.pause();
console.log("win", this.snake.getSnakeLen(), this.snake.getmoneySum());
break;
case GameState.QUIT:
director.resume();
director.loadScene("GameMain");
break;
default:
console.log("err");
}
} }
setDir() { play() {
if (!this.keyArr.length) return; if (this.state == GameState.OVER) {
this.curDir = { showToast("你已复活!");
[KeyCode.KEY_W]: DIR.UP, this.state = GameState.PLAY;
[KeyCode.KEY_S]: DIR.DOWN, this.snake.reInit(
[KeyCode.KEY_A]: DIR.LEFT, math.randomRangeInt(-(Global.MAP_WIDTH / 2 - 50), Global.MAP_WIDTH / 2 - 50),
[KeyCode.KEY_D]: DIR.RIGHT, math.randomRangeInt(-(Global.MAP_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50)
}[this.keyArr[this.keyArr.length - 1]]; );
// this.showGOver();
}
} }
onTouchStart(event: any) { showGOver() {
console.log("showGOver", this.snake.getSnakeLen(), this.snake.getmoneySum());
}
private* _getItemGenerator(count: number) {
for (let i = 0; i < count; i++) {
yield this._initItem(i);
}
} }
onTouchEnd(event: any) { private _initItem(index: number) {
const node = PoolManager.instance.getNode(this.animalPrefab, this.animalNode);
const x = math.randomRangeInt(-(Global.MAP_WIDTH / 2 - 50), Global.MAP_WIDTH / 2 - 50);
const y = math.randomRangeInt(-(Global.MAP_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50);
// const skinId = math.randomRangeInt(0, Config.SKIN_STYLE.length);
const skinId = 0;
node.getComponent(Animal)?.init(
x, y,
math.randomRangeInt(0, 360),
skinId, 0.2,
Config.SKIN_STYLE[skinId].len,
Config.SKIN_STYLE[skinId].taitLen,
index + 1
);
}
async initAnimal(count: number) {
await this.executePreFrame(this._getItemGenerator(count), 1);
} }
update(dt: number) { executePreFrame(generator: Generator, frameTime: number): Promise<boolean> {
return new Promise((resolve, reject) => {
const iter = generator;
if (this.curDir == DIR.UP) { const next = () => {
const newY = this.snake.position.y + this.speed * dt; const startTime = new Date().getTime();
this.snake.setPosition(this.snake.position.x, newY); for (let result = iter.next(); ; result = iter.next()) {
} else if (this.curDir == DIR.DOWN) { if (!result || result.done) {
const newY = this.snake.position.y - this.speed * dt; resolve(true);
this.snake.setPosition(this.snake.position.x, newY); return;
} else if (this.curDir == DIR.LEFT) {
const newX = this.snake.position.x - this.speed * dt;
this.snake.setPosition(newX, this.snake.position.y);
} else if (this.curDir == DIR.RIGHT) {
const newX = this.snake.position.x + this.speed * dt;
this.snake.setPosition(newX, this.snake.position.y);
} }
if (new Date().getTime() - startTime > frameTime) {
this.scheduleOnce(() => {
next();
});
return;
} }
}
};
next();
});
}
} }
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"ver": "4.0.24", "ver": "4.0.24",
"importer": "typescript", "importer": "typescript",
"imported": true, "imported": true,
"uuid": "701bec3a-f5a6-484b-92cf-5cb67aff8961", "uuid": "82c443ab-937b-4bfa-8e7a-5a6a04f13710",
"files": [], "files": [],
"subMetas": {}, "subMetas": {},
"userData": {} "userData": {}
......
...@@ -14,6 +14,7 @@ import { FoodType } from "./Enums"; ...@@ -14,6 +14,7 @@ import { FoodType } from "./Enums";
import { Global } from "./Global"; import { Global } from "./Global";
import { PoolManager } from "./PoolManager"; import { PoolManager } from "./PoolManager";
import { isIntersect } from "./uitl"; import { isIntersect } from "./uitl";
import { MainGame } from "./MainGame";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("Snake") @ccclass("Snake")
...@@ -162,8 +163,8 @@ export class Snake extends Component { ...@@ -162,8 +163,8 @@ export class Snake extends Component {
} }
// 生成新的食物 // 生成新的食物
if (Global.gamePlayer.fondManger.getFoodSum() < Global.gamePlayer.maxFood) { if (MainGame.ins.fondManger.getFoodSum() < MainGame.ins.maxFood) {
Global.gamePlayer.fondManger.setFood(); MainGame.ins.fondManger.setFood();
} }
}) })
.start(); .start();
...@@ -258,7 +259,7 @@ export class Snake extends Component { ...@@ -258,7 +259,7 @@ export class Snake extends Component {
this.Head.setSiblingIndex(lastIndex + 1); this.Head.setSiblingIndex(lastIndex + 1);
// 更新相机位置 // 更新相机位置
Global.gamePlayer.camera.node.setPosition(this.Head.getPosition()); MainGame.ins.camera.node.setPosition(this.Head.getPosition());
} }
move(dt: number) { move(dt: number) {
...@@ -327,7 +328,7 @@ export class Snake extends Component { ...@@ -327,7 +328,7 @@ export class Snake extends Component {
this.Head.setSiblingIndex(lastIndex + 1); this.Head.setSiblingIndex(lastIndex + 1);
// 更新相机位置,跟随头部 // 更新相机位置,跟随头部
Global.gamePlayer.camera.node.setPosition(this.Head.getPosition()); MainGame.ins.camera.node.setPosition(this.Head.getPosition());
} }
getSnakeLen() { getSnakeLen() {
......
import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from "cc";
const { ccclass, property } = _decorator;
class SnakeNode {
node: Node = null;
next: SnakeNode = null;
}
const _tempPos = new Vec3();
const _tempPrePos = new Vec3();
@ccclass("Snake")
export class Snake extends Component {
@property(Prefab)
nodePrefab: Prefab = null;
@property
gap: number = 50;
head: SnakeNode = null;
last: SnakeNode = null;
onLoad() {
this.last = this.head = new SnakeNode();
this.head.node = this.node;
}
addNode() {
const node = instantiate(this.nodePrefab);
this.node.parent.addChild(node);
node.position = this.last.node.position;
const snakeNode = new SnakeNode();
snakeNode.node = node;
this.last.next = snakeNode;
this.last = snakeNode;
}
update(dt: number) {
let pre: SnakeNode = this.head;
let current: SnakeNode = this.head.next;
while (current) {
const posDir = current.node.getPosition(_tempPos)
.subtract(pre.node.position)
.normalize();
const pos = pre.node.getPosition(_tempPrePos)
.add(posDir.multiplyScalar(this.gap));
current.node.setPosition(pos);
pre = current;
current = current.next;
}
}
}
\ No newline at end of file
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "917a4745-8677-4e4a-83f1-07545cfd07ef",
"files": [],
"subMetas": {},
"userData": {}
}
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