Commit aa7315ad authored by haiyoucuv's avatar haiyoucuv

init

parent 29f47b46
This diff is collapsed.
...@@ -365,7 +365,7 @@ export class Animal extends Component { ...@@ -365,7 +365,7 @@ export class Animal extends Component {
); );
const halfWidth = Global.MAP_WIDTH / 2; const halfWidth = Global.MAP_WIDTH / 2;
const halfHeight = Global.MAP_HIGHT / 2; const halfHeight = Global.MAP_HEIGHT / 2;
// 如果要超出地图边界,则转向 // 如果要超出地图边界,则转向
if (this.v.x <= -halfWidth || this.v.x >= halfWidth || if (this.v.x <= -halfWidth || this.v.x >= halfWidth ||
...@@ -418,8 +418,8 @@ export class Animal extends Component { ...@@ -418,8 +418,8 @@ export class Animal extends Component {
Global.MAP_WIDTH / 2 - 50 Global.MAP_WIDTH / 2 - 50
); );
const y = math.randomRangeInt( const y = math.randomRangeInt(
-(Global.MAP_HIGHT / 2 - 50), -(Global.MAP_HEIGHT / 2 - 50),
Global.MAP_HIGHT / 2 - 50 Global.MAP_HEIGHT / 2 - 50
); );
this.reInit(x, y); this.reInit(x, y);
}); });
......
...@@ -70,7 +70,7 @@ export class FondManger extends Component { ...@@ -70,7 +70,7 @@ export class FondManger extends Component {
} }
if (!y) { if (!y) {
y = math.randomRangeInt(-(Global.MAP_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50); y = math.randomRangeInt(-(Global.MAP_HEIGHT / 2 - 50), Global.MAP_HEIGHT / 2 - 50);
} }
// 从对象池获取食物节点 // 从对象池获取食物节点
...@@ -101,7 +101,7 @@ export class FondManger extends Component { ...@@ -101,7 +101,7 @@ export class FondManger extends Component {
initItem = (_: number) => { initItem = (_: number) => {
// 随机生成位置 // 随机生成位置
const x = math.randomRangeInt(-(Global.MAP_WIDTH / 2 - 50), Global.MAP_WIDTH / 2 - 50); 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 y = math.randomRangeInt(-(Global.MAP_HEIGHT / 2 - 50), Global.MAP_HEIGHT / 2 - 50);
this.addFood(x, y); this.addFood(x, y);
}; };
......
...@@ -22,7 +22,7 @@ export class Global { ...@@ -22,7 +22,7 @@ export class Global {
public static MAP_WIDTH: number = 5000; public static MAP_WIDTH: number = 5000;
/** 地图高度 */ /** 地图高度 */
public static MAP_HIGHT: number = 4000; public static MAP_HEIGHT: number = 4000;
/** 当前皮肤ID */ /** 当前皮肤ID */
public static skinId: number = 0; public static skinId: number = 0;
......
...@@ -95,24 +95,24 @@ export class MainGame extends Scene { ...@@ -95,24 +95,24 @@ export class MainGame extends Scene {
// EPhysics2DDrawFlags.Shape; // EPhysics2DDrawFlags.Shape;
Global.MAP_WIDTH = this.uiBg.contentSize.x; Global.MAP_WIDTH = this.uiBg.contentSize.x;
Global.MAP_HIGHT = this.uiBg.contentSize.y; Global.MAP_HEIGHT = this.uiBg.contentSize.y;
// 初始化墙壁 // 初始化墙壁
const wallTop = this.uiBg.node.getChildByName("WALL_TOP"); const wallTop = this.uiBg.node.getChildByName("WALL_TOP");
wallTop.setPosition(0, Global.MAP_HIGHT / 2); wallTop.setPosition(0, Global.MAP_HEIGHT / 2);
wallTop.getComponent(UITransform).width = Global.MAP_WIDTH; wallTop.getComponent(UITransform).width = Global.MAP_WIDTH;
const wallBottom = this.uiBg.node.getChildByName("WALL_BOTTOM"); const wallBottom = this.uiBg.node.getChildByName("WALL_BOTTOM");
wallBottom.setPosition(0, -Global.MAP_HIGHT / 2); wallBottom.setPosition(0, -Global.MAP_HEIGHT / 2);
wallBottom.getComponent(UITransform).width = Global.MAP_WIDTH; wallBottom.getComponent(UITransform).width = Global.MAP_WIDTH;
const wallLeft = this.uiBg.node.getChildByName("WALL_LEFT"); const wallLeft = this.uiBg.node.getChildByName("WALL_LEFT");
wallLeft.setPosition(-Global.MAP_WIDTH / 2, 0); wallLeft.setPosition(-Global.MAP_WIDTH / 2, 0);
wallLeft.getComponent(UITransform).height = Global.MAP_HIGHT; wallLeft.getComponent(UITransform).height = Global.MAP_HEIGHT;
const wallRight = this.uiBg.node.getChildByName("WALL_RIGHT"); const wallRight = this.uiBg.node.getChildByName("WALL_RIGHT");
wallRight.setPosition(Global.MAP_WIDTH / 2, 0); wallRight.setPosition(Global.MAP_WIDTH / 2, 0);
wallRight.getComponent(UITransform).height = Global.MAP_HIGHT; wallRight.getComponent(UITransform).height = Global.MAP_HEIGHT;
this.player.init(); this.player.init();
...@@ -190,7 +190,7 @@ export class MainGame extends Scene { ...@@ -190,7 +190,7 @@ export class MainGame extends Scene {
initItem = (index: number) => { initItem = (index: number) => {
const node = PoolManager.instance.getNode(this.animalPrefab); const node = PoolManager.instance.getNode(this.animalPrefab);
const x = math.randomRangeInt(-(Global.MAP_WIDTH / 2 - 50), Global.MAP_WIDTH / 2 - 50); 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 y = math.randomRangeInt(-(Global.MAP_HEIGHT / 2 - 50), Global.MAP_HEIGHT / 2 - 50);
node.getComponent(AISnake)?.init({ node.getComponent(AISnake)?.init({
x, y, x, y,
......
...@@ -46,15 +46,15 @@ export class Snake extends Component { ...@@ -46,15 +46,15 @@ export class Snake extends Component {
@property(Prefab) bodyPrefab: Prefab = null; @property(Prefab) bodyPrefab: Prefab = null;
// 私有成员变量 // 私有成员变量
private bodyArr: Node[] = []; bodyArr: Node[] = [];
private imgHead: SpriteFrame = null; private imgHead: SpriteFrame = null;
private imgBody1: SpriteFrame = null; private imgBody1: SpriteFrame = null;
private imgBody2: SpriteFrame = null; private imgBody2: SpriteFrame = null;
// 蛇的状态 // 蛇的状态
protected isLife: boolean = false; isLife: boolean = false;
private scale: number = 0.2; private scale: number = 0.2;
protected speed: number = 600; speed: number = 600;
private energy: number = 0; private energy: number = 0;
private tag: number = 0; private tag: number = 0;
...@@ -63,6 +63,10 @@ export class Snake extends Component { ...@@ -63,6 +63,10 @@ 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;
get radius() {
return this.scale * 50;
}
// 初始化方法 // 初始化方法
public async init(config: IInitConfig = {}) { public async init(config: IInitConfig = {}) {
...@@ -280,7 +284,7 @@ export class Snake extends Component { ...@@ -280,7 +284,7 @@ export class Snake extends Component {
// 边界检查 // 边界检查
const mapHalfWidth = Global.MAP_WIDTH / 2; const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HIGHT / 2; const mapHalfHeight = Global.MAP_HEIGHT / 2;
if ( if (
newHeadPos.x <= -mapHalfWidth || newHeadPos.x >= mapHalfWidth || newHeadPos.x <= -mapHalfWidth || newHeadPos.x >= mapHalfWidth ||
newHeadPos.y <= -mapHalfHeight || newHeadPos.y >= mapHalfHeight newHeadPos.y <= -mapHalfHeight || newHeadPos.y >= mapHalfHeight
......
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