Commit 1cd3961f authored by haiyoucuv's avatar haiyoucuv

init

parent 0e4e57bd
......@@ -400,8 +400,8 @@ export class AIDecision {
}
// 确保在地图范围内
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
const margin = this.SAFE_DISTANCE;
targetPos.x = math.clamp(targetPos.x, -mapHalfWidth + margin, mapHalfWidth - margin);
......@@ -425,8 +425,8 @@ export class AIDecision {
private calculateBoundaryEscapeVector(perception: PerceptionResult): Vec3 {
const myPos = perception.snake.head.getPosition();
const escapeVector = new Vec3();
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
// 根据到边界的距离计算逃离向量
if (myPos.x > mapHalfWidth * 0.8) escapeVector.x = -1;
......
......@@ -137,8 +137,8 @@ export class AIMovement {
currentPos.add(stepVector);
// 计算边界危险度
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
const boundaryDanger = Math.max(
Math.max(0, (Math.abs(currentPos.x) - (mapHalfWidth - this.ESCAPE_BOUNDARY)) / this.ESCAPE_BOUNDARY),
......@@ -186,8 +186,8 @@ export class AIMovement {
private getObstacleDistance(from: Vec3, to: Vec3): number {
// 检查地图边界
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
const margin = 50;
if (Math.abs(to.x) > mapHalfWidth - margin ||
......
......@@ -186,8 +186,8 @@ export class AIPerception {
}
private calculateBoundaryRisk(position: Vec3): number {
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
const distanceToEdge = Math.min(
mapHalfWidth - Math.abs(position.x),
......@@ -198,8 +198,8 @@ export class AIPerception {
}
private evaluatePositionSafety(pos: Vec3): number {
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
const distToBoundaryX = Math.min(mapHalfWidth - Math.abs(pos.x), mapHalfWidth);
const distToBoundaryY = Math.min(mapHalfHeight - Math.abs(pos.y), mapHalfHeight);
......@@ -307,8 +307,8 @@ export class AIPerception {
}
private getBoundaryDistance(position: Vec3): number {
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
const distanceToRight = mapHalfWidth - position.x;
const distanceToLeft = position.x + mapHalfWidth;
......
......@@ -156,14 +156,11 @@ export class AISnake extends Snake {
// 判断是否在极度危险的位置(非常靠近边界)
private isInDangerousPosition(position: Vec3): boolean {
const dangerBuffer = this.ESCAPE_BOUNDARY;
const mapWidth = Global.MAP_WIDTH;
const mapHeight = Global.MAP_HEIGHT;
return (
position.x > mapWidth / 2 - dangerBuffer ||
position.x < -mapWidth / 2 + dangerBuffer ||
position.y > mapHeight / 2 - dangerBuffer ||
position.y < -mapHeight / 2 + dangerBuffer
position.x > Global.HALF_MAP_WIDTH - dangerBuffer ||
position.x < -Global.HALF_MAP_WIDTH + dangerBuffer ||
position.y > Global.HALF_MAP_HEIGHT - dangerBuffer ||
position.y < -Global.HALF_MAP_HEIGHT + dangerBuffer
);
}
......@@ -726,12 +723,9 @@ export class AISnake extends Snake {
// 获取到边界的距离
private getDistanceToBoundary(position: IVec2Like): number {
const mapWidth = Global.MAP_WIDTH;
const mapHeight = Global.MAP_HEIGHT;
return Math.min(
mapWidth / 2 - Math.abs(position.x),
mapHeight / 2 - Math.abs(position.y)
Global.HALF_MAP_WIDTH - Math.abs(position.x),
Global.HALF_MAP_HEIGHT - Math.abs(position.y)
);
}
......@@ -754,37 +748,36 @@ export class AISnake extends Snake {
private avoidBoundary() {
const myPos = this.head.getPosition();
const mapWidth = Global.MAP_WIDTH;
const mapHeight = Global.MAP_HEIGHT;
const { HALF_MAP_WIDTH, HALF_MAP_HEIGHT } = Global;
const boundaryBuffer = this.ESCAPE_BOUNDARY;
let targetAngle = this.head.angle;
let isCorner = false;
// 检查四角区域
if (myPos.x < -mapWidth / 2 + boundaryBuffer && myPos.y < -mapHeight / 2 + boundaryBuffer) {
if (myPos.x < -HALF_MAP_WIDTH + boundaryBuffer && myPos.y < -HALF_MAP_HEIGHT + boundaryBuffer) {
targetAngle = 45; // 右上
isCorner = true;
} else if (myPos.x > mapWidth / 2 - boundaryBuffer && myPos.y < -mapHeight / 2 + boundaryBuffer) {
} else if (myPos.x > HALF_MAP_WIDTH - boundaryBuffer && myPos.y < -HALF_MAP_HEIGHT + boundaryBuffer) {
targetAngle = 135; // 左上
isCorner = true;
} else if (myPos.x < -mapWidth / 2 + boundaryBuffer && myPos.y > mapHeight / 2 - boundaryBuffer) {
} else if (myPos.x < -HALF_MAP_WIDTH + boundaryBuffer && myPos.y > HALF_MAP_HEIGHT - boundaryBuffer) {
targetAngle = 315; // 右下
isCorner = true;
} else if (myPos.x > mapWidth / 2 - boundaryBuffer && myPos.y > mapHeight / 2 - boundaryBuffer) {
} else if (myPos.x > HALF_MAP_WIDTH - boundaryBuffer && myPos.y > HALF_MAP_HEIGHT - boundaryBuffer) {
targetAngle = 225; // 左下
isCorner = true;
} else {
// 检查边界
if (myPos.x < -mapWidth / 2 + boundaryBuffer) {
if (myPos.x < -HALF_MAP_WIDTH + boundaryBuffer) {
targetAngle = 0; // 向右
} else if (myPos.x > mapWidth / 2 - boundaryBuffer) {
} else if (myPos.x > HALF_MAP_WIDTH - boundaryBuffer) {
targetAngle = 180; // 向左
}
if (myPos.y < -mapHeight / 2 + boundaryBuffer) {
if (myPos.y < -HALF_MAP_HEIGHT + boundaryBuffer) {
targetAngle = 90; // 向上
} else if (myPos.y > mapHeight / 2 - boundaryBuffer) {
} else if (myPos.y > HALF_MAP_HEIGHT - boundaryBuffer) {
targetAngle = 270; // 向下
}
}
......@@ -804,8 +797,7 @@ export class AISnake extends Snake {
const foods = FoodManger.ins.node.children;
const boundaryBuffer = this.ESCAPE_BOUNDARY;
const mapWidth = Global.MAP_WIDTH;
const mapHeight = Global.MAP_HEIGHT;
const { HALF_MAP_WIDTH, HALF_MAP_HEIGHT } = Global;
for (const food of foods) {
if (!food.isValid || !food.active) continue;
......@@ -814,8 +806,8 @@ export class AISnake extends Snake {
const distance = Vec3.distance(myPos, foodPos);
// 检查食物是否靠近墙体
if (foodPos.x < -mapWidth / 2 + boundaryBuffer || foodPos.x > mapWidth / 2 - boundaryBuffer ||
foodPos.y < -mapHeight / 2 + boundaryBuffer || foodPos.y > mapHeight / 2 - boundaryBuffer) {
if (foodPos.x < -HALF_MAP_WIDTH + boundaryBuffer || foodPos.x > HALF_MAP_WIDTH - boundaryBuffer ||
foodPos.y < -HALF_MAP_HEIGHT + boundaryBuffer || foodPos.y > HALF_MAP_HEIGHT - boundaryBuffer) {
continue; // 跳过靠近墙体的食物
}
......
......@@ -10,17 +10,19 @@ export class Global {
/** 地图宽度 */
static MAP_WIDTH: number = 6000;
static HALF_MAP_WIDTH: number = Global.MAP_WIDTH * 0.5;
/** 地图高度 */
static MAP_HEIGHT: number = 4200;
static HALF_MAP_HEIGHT: number = Global.MAP_HEIGHT * 0.5;
/** 道具CD */
static PROP_CD: number = 15;
static getRandomPosition(padding: number = 0): IVec2Like {
const maxW = Global.MAP_WIDTH / 2 - padding;
const maxH = Global.MAP_HEIGHT / 2 - padding;
const maxW = Global.HALF_MAP_WIDTH - padding;
const maxH = Global.HALF_MAP_HEIGHT - padding;
const x = math.randomRange(-maxW, maxW);
const y = math.randomRange(-maxH, maxH);
......
......@@ -86,12 +86,8 @@ export class LuckyBagManager extends Component {
add(num: number = 1) {
if (!num || num <= 0) return;
const maxW = Global.MAP_WIDTH / 2 - 100;
const maxH = Global.MAP_HEIGHT / 2 - 100;
for (let i = 1; i <= num; i++) {
const x = math.randomRange(-maxW, maxW);
const y = math.randomRange(-maxH, maxH);
const {x, y} = Global.getRandomPosition(100);
const node = instantiate(this.luckyBagPrefab);
// 设置食物属性
......
......@@ -436,13 +436,13 @@ export class Snake extends Component {
}
// 边界检查
const mapHalfWidth = Global.MAP_WIDTH / 2;
const mapHalfHeight = Global.MAP_HEIGHT / 2;
const mapHalfWidth = Global.HALF_MAP_WIDTH;
const mapHalfHeight = Global.HALF_MAP_HEIGHT;
if (
newHeadPos.x <= -mapHalfWidth
|| newHeadPos.x >= mapHalfWidth
|| newHeadPos.y <= -mapHalfHeight
|| newHeadPos.y >= mapHalfHeight
newHeadPos.x < -mapHalfWidth
|| newHeadPos.x > mapHalfWidth
|| newHeadPos.y < -mapHalfHeight
|| newHeadPos.y > mapHalfHeight
) {
this.death();
}
......
const now = performance.now();
let a = 1e10;
for (let i = 0; i < 1e9; i++) {
// a /= 2;
a *= 0.5;
}
console.log(performance.now() - now);
\ 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