Commit 25358c2f authored by haiyoucuv's avatar haiyoucuv

init

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