Commit b2d1c691 authored by haiyoucuv's avatar haiyoucuv

init

parent 5e372767
This diff is collapsed.
...@@ -57,7 +57,7 @@ export class AISnake extends Snake { ...@@ -57,7 +57,7 @@ export class AISnake extends Snake {
// 碰到其他蛇身 // 碰到其他蛇身
// this.setAngle(this.head.angle + 180); // this.setAngle(this.head.angle + 180);
// this.isFast = true; // this.isFast = true;
this.setState(AIState.ESCAPING, otherCollider.node.parent.getComponent(Snake)); this.setState(AIState.ESCAPING, otherCollider.node?.parent?.getComponent(Snake));
} }
} }
......
import { _decorator } from "cc";
import { Snake } from "./Snake";
import { MainGame } from "./MainGame";
import { AIController } from "./AI/AIController";
import { aiPool } from "./Manager/CommonPool";
const { ccclass } = _decorator;
@ccclass("AISnake")
export class AISnake extends Snake {
private aiController: AIController;
private updateTimer: number = 0;
private readonly UPDATE_INTERVAL: number = 0.1;
async init(config: any) {
await super.init(config);
// const difficulty = 1 + Math.random() * 4;
this.aiController = new AIController(this, 5);
}
onEnable() {
super.onEnable();
// const eye = this.head.getChildByName("范围").getComponent(Collider2D);
// eye.on(Contact2DType.BEGIN_CONTACT, this.onBeginEye, this);
}
// onBeginEye(selfCollider: Collider2D, otherCollider: Collider2D) {
// super.onBeginEye(selfCollider, otherCollider);
// if (otherCollider.group === PhysicsGroup["Body"] && otherCollider.tag != this.tag) {
// // 碰到其他蛇身
// // this.setAngle(this.head.angle + 180);
// // this.isFast = true;
// this.setState(AIState.ESCAPING, otherCollider.node.parent.getComponent(Snake));
// }
// }
death() {
super.death();
this.node.removeFromParent();
aiPool.put(this.node);
MainGame.ins.initAnimal(1);
}
onUpdate(dt: number) {
if (!this.isLife) return;
this.updateTimer += dt;
if (this.updateTimer >= this.UPDATE_INTERVAL) {
this.updateTimer = 0;
this.aiController.update(dt);
}
super.onUpdate(dt);
}
}
\ No newline at end of file
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "fc484593-2f1d-43f4-8f3a-99561dbc1481",
"files": [],
"subMetas": {},
"userData": {}
}
...@@ -122,7 +122,9 @@ export class MainGame extends Scene { ...@@ -122,7 +122,9 @@ export class MainGame extends Scene {
wallRight.setPosition(Global.MAP_WIDTH / 2, 0); wallRight.setPosition(Global.MAP_WIDTH / 2, 0);
wallRight.getComponent(UITransform).height = Global.MAP_HEIGHT; wallRight.getComponent(UITransform).height = Global.MAP_HEIGHT;
this.player.init(); this.player.init({
// initEnergy: 100
});
// 初始化食物和NPC // 初始化食物和NPC
this.fondManger.init(this.maxFood); this.fondManger.init(this.maxFood);
......
...@@ -15,7 +15,7 @@ export class Food extends Component { ...@@ -15,7 +15,7 @@ export class Food extends Component {
set energy(energy: number) { set energy(energy: number) {
this._energy = energy; this._energy = energy;
const scale = 1 + (energy - 1) / 10; const scale = 1 + (energy - 1) / 3;
this.node.scale.set(scale, scale); this.node.scale.set(scale, scale);
} }
......
...@@ -53,7 +53,7 @@ export class Snake extends Component { ...@@ -53,7 +53,7 @@ export class Snake extends Component {
// 蛇的状态 // 蛇的状态
isLife: boolean = false; isLife: boolean = false;
private scale: number = 0.2; private scale: number = 0.2;
speed: number = 600; speed: number = 300;
private energy: number = 0; private energy: number = 0;
protected tag: number = 0; protected tag: number = 0;
...@@ -62,8 +62,6 @@ export class Snake extends Component { ...@@ -62,8 +62,6 @@ 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;
} }
...@@ -74,7 +72,6 @@ export class Snake extends Component { ...@@ -74,7 +72,6 @@ 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", skinName = "default",
bodyCount = 5,
initEnergy = 5, initEnergy = 5,
} = config; } = config;
...@@ -84,7 +81,7 @@ export class Snake extends Component { ...@@ -84,7 +81,7 @@ export class Snake extends Component {
this.energy = 0; this.energy = 0;
this.bodyArr = []; this.bodyArr = [];
this.scale = scale; this.scale = scale;
this.speed = this.speed * scale; // this.speed = this.speed * scale;
this.tag = Snake.tag++; this.tag = Snake.tag++;
// 设置头部 // 设置头部
...@@ -98,7 +95,6 @@ export class Snake extends Component { ...@@ -98,7 +95,6 @@ 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);
// 创建尾巴节点 // 创建尾巴节点
const tile = bodyPool.get() || instantiate(this.bodyPrefab); const tile = bodyPool.get() || instantiate(this.bodyPrefab);
...@@ -120,6 +116,9 @@ export class Snake extends Component { ...@@ -120,6 +116,9 @@ export class Snake extends Component {
this.node.addChild(tile); this.node.addChild(tile);
this.bodyArr.push(tile); this.bodyArr.push(tile);
// 创建身体节点
this.addEnergy(initEnergy);
this.isLife = true; this.isLife = true;
this.ready = true; this.ready = true;
} }
...@@ -166,33 +165,34 @@ export class Snake extends Component { ...@@ -166,33 +165,34 @@ export class Snake extends Component {
scale: v3(0, 0) scale: v3(0, 0)
}) })
.call(() => { .call(() => {
otherCollider.node.getComponent(Food).recycle();
if (!this.isLife) return; if (!this.isLife) return;
const foodTs = otherCollider.getComponent(Food);
if (foodType == FoodType.FOOD) { this.addEnergy(foodTs.energy);
this.addEnergy(1); foodTs.recycle();
}
}) })
.start(); .start();
} }
} }
// 能量与成长 // 能量与成长
lastRemaining = 0;
private addEnergy(value: number) { private addEnergy(value: number) {
this.energy += value; this.energy += value;
const growthThreshold = Math.floor(4 * this.scale); const growthThreshold = Math.floor(4 * this.scale);
while (this.energy >= growthThreshold) { value += this.lastRemaining;
while (value >= growthThreshold) {
this.grow(); this.grow();
this.energy -= growthThreshold; value -= growthThreshold;
if (this.scale < 1) { if (this.scale < 1) {
this.scale += 0.005; this.scale += 0.005;
} }
} }
this.speed = 600 * this.scale; this.lastRemaining = value;
// this.speed = 600 * this.scale;
} }
// 蛇身体生长 // 蛇身体生长
...@@ -229,7 +229,7 @@ export class Snake extends Component { ...@@ -229,7 +229,7 @@ export class Snake extends Component {
isFast = false; isFast = false;
private positions: Vec3[] = []; // 存储历史位置点 private positions: Vec3[] = []; // 存储历史位置点
private readonly HISTORY_LENGTH = 100; // 增加历史点数量 private readonly HISTORY_LENGTH = 100; // 增加历史点数量
private readonly SEGMENT_SPACING = 4; // 增加节点间距 private readonly SEGMENT_SPACING = 8; // 增加节点间距
moveTime = 1 / 60; moveTime = 1 / 60;
totalTime = 0; totalTime = 0;
...@@ -262,10 +262,12 @@ export class Snake extends Component { ...@@ -262,10 +262,12 @@ export class Snake extends Component {
this.head.setPosition(newHeadPos); this.head.setPosition(newHeadPos);
this.head.setScale(this.scale, this.scale); this.head.setScale(this.scale, this.scale);
const space = ~~(this.SEGMENT_SPACING * this.scale);
// 存储历史位置 // 存储历史位置
this.positions.unshift(newHeadPos.clone()); this.positions.unshift(newHeadPos.clone());
// 确保历史位置点足够多,以容纳所有身体节点 // 确保历史位置点足够多,以容纳所有身体节点
const requiredLength = this.bodyArr.length * this.SEGMENT_SPACING + 1; const requiredLength = this.bodyArr.length * space + 1;
if (this.positions.length > Math.max(requiredLength, this.HISTORY_LENGTH)) { if (this.positions.length > Math.max(requiredLength, this.HISTORY_LENGTH)) {
this.positions.pop(); this.positions.pop();
} }
...@@ -275,7 +277,7 @@ export class Snake extends Component { ...@@ -275,7 +277,7 @@ export class Snake extends Component {
const body = this.bodyArr[i]; const body = this.bodyArr[i];
// 为每个节点计算一个固定的偏移量 // 为每个节点计算一个固定的偏移量
const offset = (i + 1) * this.SEGMENT_SPACING; const offset = (i + 1) * space;
// 确保不会超出历史位置数组范围 // 确保不会超出历史位置数组范围
if (offset < this.positions.length) { if (offset < this.positions.length) {
...@@ -332,8 +334,8 @@ export class Snake extends Component { ...@@ -332,8 +334,8 @@ export class Snake extends Component {
bodyPool.put(body); bodyPool.put(body);
return { return {
x: body.position.x + 10 - 5, x: body.position.x + Math.random() * 10 - 5,
y: body.position.y + 10 - 5, y: body.position.y + Math.random() * 10 - 5,
energy: ~~(this.energy / len), energy: ~~(this.energy / len),
}; };
}); });
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
const canvas = document.createElement("canvas");
canvas.width = 500;
canvas.height = 500;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "transparent";
ctx.fillRect(0, 0, 500, 500);
const b64 = canvas.toDataURL("image/png");
console.log(b64);
</script>
</body>
</html>
\ No newline at end of file
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