Commit 25358c2f authored by haiyoucuv's avatar haiyoucuv

init

parent aa6d55ca
...@@ -20,22 +20,19 @@ ...@@ -20,22 +20,19 @@
"_children": [ "_children": [
{ {
"__id__": 2 "__id__": 2
},
{
"__id__": 12
} }
], ],
"_active": true, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 20 "__id__": 12
}, },
{ {
"__id__": 22 "__id__": 14
} }
], ],
"_prefab": { "_prefab": {
"__id__": 24 "__id__": 16
}, },
"_lpos": { "_lpos": {
"__type__": "cc.Vec3", "__type__": "cc.Vec3",
...@@ -272,164 +269,6 @@ ...@@ -272,164 +269,6 @@
"targetOverrides": null, "targetOverrides": null,
"nestedPrefabInstanceRoots": null "nestedPrefabInstanceRoots": null
}, },
{
"__type__": "cc.Node",
"_name": "water",
"_objFlags": 0,
"__editorExtras__": {},
"_parent": {
"__id__": 1
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 13
},
{
"__id__": 15
},
{
"__id__": 17
}
],
"_prefab": {
"__id__": 19
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_mobility": 0,
"_layer": 1,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
"node": {
"__id__": 12
},
"_enabled": true,
"__prefab": {
"__id__": 14
},
"_contentSize": {
"__type__": "cc.Size",
"width": 40,
"height": 36
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "5831s867ZOQIJwgm+uwEOn"
},
{
"__type__": "cc.RigidBody2D",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
"node": {
"__id__": 12
},
"_enabled": true,
"__prefab": {
"__id__": 16
},
"enabledContactListener": true,
"bullet": false,
"awakeOnLoad": true,
"_group": 8,
"_type": 2,
"_allowSleep": true,
"_gravityScale": 1,
"_linearDamping": 0,
"_angularDamping": 0,
"_linearVelocity": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_angularVelocity": 0,
"_fixedRotation": false,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "3b9xj0LydCnLxcVCUVNbXT"
},
{
"__type__": "cc.CircleCollider2D",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
"node": {
"__id__": 12
},
"_enabled": true,
"__prefab": {
"__id__": 18
},
"tag": 0,
"_group": 8,
"_density": 1,
"_sensor": true,
"_friction": 0.2,
"_restitution": 0,
"_offset": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_radius": 20,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "29rS1KzWxEIJcsCRKPGORx"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "10yUmi7JZE/5iqdarulMdp",
"instance": null,
"targetOverrides": null,
"nestedPrefabInstanceRoots": null
},
{ {
"__type__": "cc.UITransform", "__type__": "cc.UITransform",
"_name": "", "_name": "",
...@@ -440,7 +279,7 @@ ...@@ -440,7 +279,7 @@
}, },
"_enabled": true, "_enabled": true,
"__prefab": { "__prefab": {
"__id__": 21 "__id__": 13
}, },
"_contentSize": { "_contentSize": {
"__type__": "cc.Size", "__type__": "cc.Size",
...@@ -468,14 +307,12 @@ ...@@ -468,14 +307,12 @@
}, },
"_enabled": true, "_enabled": true,
"__prefab": { "__prefab": {
"__id__": 23 "__id__": 15
}, },
"Head": { "Head": {
"__id__": 2 "__id__": 2
}, },
"water": { "water": null,
"__id__": 12
},
"bodyPrefab": { "bodyPrefab": {
"__uuid__": "8c4f22c7-20c7-4868-88ea-dedb8004999f", "__uuid__": "8c4f22c7-20c7-4868-88ea-dedb8004999f",
"__expectedType__": "cc.Prefab" "__expectedType__": "cc.Prefab"
......
import { import {
_decorator, Node, assetManager, SpriteAtlas, Sprite, CircleCollider2D, _decorator, Node, assetManager, SpriteAtlas, Sprite, CircleCollider2D,
math, Collider2D, Contact2DType, ParticleSystem2D, UITransform, math, Collider2D, Contact2DType, ParticleSystem2D, UITransform,
tween, v3, v2, Component, Prefab, PhysicsGroup tween, v3, v2, Component, Prefab, PhysicsGroup, resources
} from "cc"; } from "cc";
import { Global } from "./Global"; import { Global } from "./Global";
import { PoolManager } from "./PoolManager"; import { PoolManager } from "./PoolManager";
...@@ -20,18 +20,11 @@ export class Animal extends Component { ...@@ -20,18 +20,11 @@ export class Animal extends Component {
}) })
private Head: Node = null; private Head: Node = null;
@property({
type: Node,
displayName: "水花"
})
private water: Node = null;
@property(Prefab) @property(Prefab)
private bodyPrefab: Prefab = null; private bodyPrefab: Prefab = null;
private uiSanke: UITransform = null; private uiSanke: UITransform = null;
private uiFondManger: UITransform = null; private uiFondManger: UITransform = null;
private eye: Collider2D = null;
private Bodys: Node[] = []; private Bodys: Node[] = [];
// 蛇身图片资源 // 蛇身图片资源
...@@ -67,14 +60,12 @@ export class Animal extends Component { ...@@ -67,14 +60,12 @@ export class Animal extends Component {
this.ready = false; this.ready = false;
const skinPath = `skin/s${skinId}`; const skinPath = `skin/s${skinId}`;
// 加载皮肤资源 this.Head.angle = angle;
assetManager.loadBundle("resources", (err, bundle) => { this.Head.setPosition(x, y);
if (err) { this.Head.setScale(scale, scale);
console.warn(err);
return;
}
bundle.load(skinPath, SpriteAtlas, (err, atlas) => { // 加载皮肤资源
resources.load(skinPath, SpriteAtlas, (err, atlas) => {
if (err) { if (err) {
console.warn(err); console.warn(err);
return; return;
...@@ -96,9 +87,6 @@ export class Animal extends Component { ...@@ -96,9 +87,6 @@ export class Animal extends Component {
this.tag = tag; this.tag = tag;
// 设置头部 // 设置头部
this.Head.angle = 0;
this.Head.setPosition(x, y);
this.Head.setScale(scale, scale);
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(),
...@@ -107,9 +95,6 @@ export class Animal extends Component { ...@@ -107,9 +95,6 @@ export class Animal extends Component {
this.vh this.vh
); );
// 设置水花特效
this.water.getComponent(CircleCollider2D).radius = math.randomRangeInt(100, 200);
// 创建身体节点 // 创建身体节点
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
const body = PoolManager.instance.getNode(this.bodyPrefab, this.node); const body = PoolManager.instance.getNode(this.bodyPrefab, this.node);
...@@ -133,7 +118,6 @@ export class Animal extends Component { ...@@ -133,7 +118,6 @@ export class Animal extends Component {
}); });
this.ready = true; this.ready = true;
});
} }
onEnable() { onEnable() {
...@@ -141,15 +125,10 @@ export class Animal extends Component { ...@@ -141,15 +125,10 @@ export class Animal extends Component {
const headCollider = this.Head.getComponent(Collider2D); const headCollider = this.Head.getComponent(Collider2D);
if (headCollider) { if (headCollider) {
headCollider.on(Contact2DType.BEGIN_CONTACT, this.onBeginHead, this); headCollider.on(Contact2DType.BEGIN_CONTACT, this.onBeginHead, this);
headCollider.on(Contact2DType.BEGIN_CONTACT, this.onBeginEye, this);
} }
this.eye = this.water.getComponent(Collider2D);
if (this.eye) {
this.eye.on(Contact2DType.BEGIN_CONTACT, this.onBeginEye, this);
}
this.uiSanke = this.getComponent(UITransform); this.uiSanke = this.getComponent(UITransform);
this.uiFondManger = MainGame.ins.fondManger.node.getComponent(UITransform); this.uiFondManger = MainGame.ins.fondManger.getComponent(UITransform);
} }
onDisable() { onDisable() {
...@@ -157,9 +136,7 @@ export class Animal extends Component { ...@@ -157,9 +136,7 @@ export class Animal extends Component {
const headCollider = this.Head.getComponent(Collider2D); const headCollider = this.Head.getComponent(Collider2D);
if (headCollider) { if (headCollider) {
headCollider.off(Contact2DType.BEGIN_CONTACT, this.onBeginHead, this); headCollider.off(Contact2DType.BEGIN_CONTACT, this.onBeginHead, this);
} headCollider.off(Contact2DType.BEGIN_CONTACT, this.onBeginEye, this);
if (this.eye) {
this.eye.off(Contact2DType.BEGIN_CONTACT, this.onBeginEye, this);
} }
} }
...@@ -177,8 +154,8 @@ export class Animal extends Component { ...@@ -177,8 +154,8 @@ export class Animal extends Component {
* 视野碰撞回调 * 视野碰撞回调
*/ */
onBeginEye(selfCollider: Collider2D, otherCollider: Collider2D) { onBeginEye(selfCollider: Collider2D, otherCollider: Collider2D) {
// 碰到食物
if (otherCollider.group === PhysicsGroup["Prop"]) { if (otherCollider.group === PhysicsGroup["Prop"]) {
// 碰到食物
const foodType = otherCollider.tag; const foodType = otherCollider.tag;
this.isRunning = true; this.isRunning = true;
...@@ -205,10 +182,8 @@ export class Animal extends Component { ...@@ -205,10 +182,8 @@ export class Animal extends Component {
} }
}) })
.start(); .start();
} } else if (otherCollider.group === PhysicsGroup["Body"] && otherCollider.tag != this.tag) {
// 碰到其他蛇身 // 碰到其他蛇身
else if (otherCollider.group === PhysicsGroup["Body"] &&
otherCollider.tag != this.tag) {
this.setAngle(this.Head.angle + 180); this.setAngle(this.Head.angle + 180);
this.isRunning = true; this.isRunning = true;
} }
...@@ -478,7 +453,7 @@ export class Animal extends Component { ...@@ -478,7 +453,7 @@ export class Animal extends Component {
*/ */
initItem = (index: number) => { initItem = (index: number) => {
this.b = this.Bodys[index].getPosition(); this.b = this.Bodys[index].getPosition();
MainGame.ins.fondManger.setFood( MainGame.ins.fondManger.addFood(
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)
); );
......
...@@ -3,6 +3,7 @@ import { PoolManager } from "./PoolManager"; ...@@ -3,6 +3,7 @@ import { PoolManager } from "./PoolManager";
import { FoodType } from "./Enums"; import { FoodType } from "./Enums";
import { Global } from "./Global"; import { Global } from "./Global";
import { MainGame } from "./MainGame"; import { MainGame } from "./MainGame";
import { executePreFrame, getItemGenerator } from "../../Utils/ExecutePreFrame";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
...@@ -40,14 +41,14 @@ export class FondManger extends Component { ...@@ -40,14 +41,14 @@ export class FondManger extends Component {
/** /**
* 初始化食物 * 初始化食物
*/ */
init(count: number = 100) { async init(count: number = 100) {
this.initFond(count); await this.initFond(count);
} }
/** /**
* 设置食物 * 设置食物
*/ */
setFood(x?: number, y?: number) { addFood(x?: number, y?: number) {
// 如果没有指定位置,随机生成位置 // 如果没有指定位置,随机生成位置
if (!x) { if (!x) {
...@@ -58,19 +59,17 @@ export class FondManger extends Component { ...@@ -58,19 +59,17 @@ export class FondManger extends Component {
y = math.randomRangeInt(-(Global.MAP_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50); y = math.randomRangeInt(-(Global.MAP_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50);
} }
// 从对象池获取食物节点
const node = PoolManager.instance.getNode(this.foodPrefab);
// 随机选择食物类型 // 随机选择食物类型
const index = math.randomRangeInt(0, this.foods.length); const index = math.randomRangeInt(0, this.foods.length);
// 从对象池获取食物节点 this.node.addChild(node);
const node = PoolManager.instance.getNode(
this.foodPrefab,
this.node,
v3(x, y, 0)
);
// 设置食物属性 // 设置食物属性
node.angle = 0; node.angle = math.randomRange(0, 360);
node.setScale(1, 1); node.setScale(1, 1);
node.setPosition(x, y);
node.getComponent(Sprite).spriteFrame = this.foods[index].sp; node.getComponent(Sprite).spriteFrame = this.foods[index].sp;
// 设置碰撞类型 // 设置碰撞类型
...@@ -85,39 +84,19 @@ export class FondManger extends Component { ...@@ -85,39 +84,19 @@ export class FondManger extends Component {
/** /**
* 初始化单个食物 * 初始化单个食物
*/ */
private _initItem(index: number) { initItem = (_: number) => {
// 从对象池获取食物节点
const node = PoolManager.instance.getNode(this.foodPrefab, this.node);
// 随机生成位置 // 随机生成位置
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_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50);
// 随机选择食物类型 this.addFood(x, y);
index = math.randomRangeInt(0, this.foods.length);
// 设置食物属性
node.angle = 0;
node.setScale(1, 1);
node.setPosition(x, y, 0);
node.getComponent(Sprite).spriteFrame = this.foods[index].sp;
// 设置碰撞类型
const collider = node.getComponent(Collider2D);
if (collider) {
collider.tag = this.foods[index].type;
}
node.active = true;
} }
/** /**
* 初始化食物 * 初始化食物
*/ */
initFond(count: number) { async initFond(count: number) {
for (let i = 0; i < count; i++) { await executePreFrame(getItemGenerator(count, this.initItem), 1, this);
this._initItem(i);
}
} }
} }
...@@ -7,7 +7,7 @@ import { ...@@ -7,7 +7,7 @@ import {
director, director,
math, math,
Prefab, Prefab,
PhysicsSystem2D, PhysicsSystem2D, EPhysics2DDrawFlags,
} from "cc"; } from "cc";
import { Joystick } from "./Joystick"; import { Joystick } from "./Joystick";
import { Snake } from "./Snake"; import { Snake } from "./Snake";
...@@ -20,6 +20,7 @@ import { GameState, Events } from "./Enums"; ...@@ -20,6 +20,7 @@ import { GameState, Events } from "./Enums";
import { PoolManager } from "./PoolManager"; import { PoolManager } from "./PoolManager";
import { showToast } from "../../../Module/UIFast"; import { showToast } from "../../../Module/UIFast";
import Scene from "../../../Module/Scene"; import Scene from "../../../Module/Scene";
import { executePreFrame, getItemGenerator } from "../../Utils/ExecutePreFrame";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
...@@ -78,9 +79,9 @@ export class MainGame extends Scene { ...@@ -78,9 +79,9 @@ export class MainGame extends Scene {
return MainGame._ins; return MainGame._ins;
} }
onLoad() { async onLoad() {
MainGame._ins = this; MainGame._ins = this;
PhysicsSystem2D.instance.enable = true; PhysicsSystem2D.instance.enable = true;
Global.MAP_WIDTH = this.uiBg.contentSize.x; Global.MAP_WIDTH = this.uiBg.contentSize.x;
...@@ -110,7 +111,7 @@ export class MainGame extends Scene { ...@@ -110,7 +111,7 @@ export class MainGame extends Scene {
Config.SKIN_STYLE[skinId].taitLen, 0); Config.SKIN_STYLE[skinId].taitLen, 0);
// 初始化食物和NPC // 初始化食物和NPC
this.fondManger.init(this.maxFood); this.fondManger.init(this.maxFood + 1000);
this.initAnimal(this.maxAnimal); this.initAnimal(this.maxAnimal);
// 设置游戏状态 // 设置游戏状态
...@@ -123,7 +124,6 @@ export class MainGame extends Scene { ...@@ -123,7 +124,6 @@ export class MainGame extends Scene {
onDestroy() { onDestroy() {
MainGame._ins = null; MainGame._ins = null;
} }
update(dt: number) { update(dt: number) {
...@@ -196,14 +196,8 @@ export class MainGame extends Scene { ...@@ -196,14 +196,8 @@ export class MainGame extends Scene {
console.log("showGOver", this.snake.getSnakeLen(), this.snake.getMoneySum()); console.log("showGOver", this.snake.getSnakeLen(), this.snake.getMoneySum());
} }
private* _getItemGenerator(count: number) { initItem = (index: number) => {
for (let i = 0; i < count; i++) { const node = PoolManager.instance.getNode(this.animalPrefab);
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 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_HIGHT / 2 - 50), Global.MAP_HIGHT / 2 - 50);
// const skinId = math.randomRangeInt(0, Config.SKIN_STYLE.length); // const skinId = math.randomRangeInt(0, Config.SKIN_STYLE.length);
...@@ -212,38 +206,18 @@ export class MainGame extends Scene { ...@@ -212,38 +206,18 @@ export class MainGame extends Scene {
node.getComponent(Animal)?.init( node.getComponent(Animal)?.init(
x, y, x, y,
math.randomRangeInt(0, 360), math.randomRangeInt(0, 360),
skinId, 0.2, skinId,
0.2,
Config.SKIN_STYLE[skinId].len, Config.SKIN_STYLE[skinId].len,
Config.SKIN_STYLE[skinId].taitLen, Config.SKIN_STYLE[skinId].taitLen,
index + 1 index + 1
); );
this.animalNode.addChild(node);
} }
async initAnimal(count: number) { async initAnimal(count: number) {
await this.executePreFrame(this._getItemGenerator(count), 1); await executePreFrame(getItemGenerator(count, this.initItem), 1, this);
} }
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
...@@ -164,7 +164,7 @@ export class Snake extends Component { ...@@ -164,7 +164,7 @@ export class Snake extends Component {
// 生成新的食物 // 生成新的食物
if (MainGame.ins.fondManger.getFoodSum() < MainGame.ins.maxFood) { if (MainGame.ins.fondManger.getFoodSum() < MainGame.ins.maxFood) {
MainGame.ins.fondManger.setFood(); MainGame.ins.fondManger.addFood();
} }
}) })
.start(); .start();
......
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