版本:V_1.3.7
鼠标控制方向,上下键或wasd控制移动
运行时要等10s及以下才能加载好。
绿色的是怪,白色+蓝色的是医疗包,浅蓝的是出口。(在小地图上,蓝色是怪物,红色是医疗包,黄色的是出口)
运行前点开编译器工具里的编译选项,点最下面那个大框框,输入“-lgdi32 -luser32 -static-libgcc”,把小框勾上,就可以运行了!!!
#include <windows.h>
#include <cmath>
#include <cstring>
#include <algorithm>
//-lgdi32 -luser32 -static-libgcc
const int WIDTH = 800;
const int HEIGHT = 600;
int maze[12][12] = {
{1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,1,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,1,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,1},
{1,0,1,0,1,0,1,0,1,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,1},
{1,0,1,1,0,1,1,0,1,1,0,1},
{1,0,0,0,0,0,1,0,1,0,0,1},
{1,0,1,0,0,0,0,0,0,0,1,1},
{1,1,1,0,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1}
};
const int MAZE_W = 12;
const int MAZE_H = 12;
double px = 1.5, py = 1.5;
double dirX = -1, dirY = 0;
double planeX = 0, planeY = 0.66;
bool isDead = false;
bool isWin = false;
bool hasFocus = false;
const float MOUSE_SENS = 0.003f;
float playerHP = 100.0f;
const float playerMaxHP = 100.0f;
const float DAMAGE_RANGE = 0.6f;
const float DAMAGE_PER_SEC = 25.0f;
HWND win;
HDC bufDC;
HBITMAP bufBmp;
BITMAPINFO bmpInfo;
unsigned int* frameBuf;
bool keyState[256] = {false};
const int TEX_SIZE = 64;
unsigned int texture[TEX_SIZE * TEX_SIZE];
unsigned int enemyTex[TEX_SIZE * TEX_SIZE];
unsigned int exitTex[TEX_SIZE * TEX_SIZE];
unsigned int hpTex[TEX_SIZE * TEX_SIZE];
double zBuffer[WIDTH];
struct Sprite {
double x, y;
unsigned int* tex;
};
Sprite sprites[] = {
{10.5, 10.5, enemyTex},
{10.5, 10.5, enemyTex},
{8.5, 8.5, enemyTex},
{10.5, 10.5, exitTex},
{9.5, 1.5, hpTex},
{5.5, 5.5, hpTex},
{1.5, 7.5, hpTex}
};
const int SPRITE_COUNT = 7;
const int ENEMY_COUNT = 3;
const int HP_COUNT = 3;
bool hpActive[3] = {true, true, true};
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void CreateBuffer();
void DeleteBuffer();
void Render();
void Update(float dt);
bool IsBlock(double x, double y);
void GenerateTexture();
void GenerateEnemyTexture();
void GenerateExitTexture();
void GenerateHPTex();
void DrawMinimap();
void DrawHPBar();
int main()
{
GenerateTexture();
GenerateEnemyTexture();
GenerateExitTexture();
GenerateHPTex();
const char clsName[] = "RayCastMaze";
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WinProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = clsName;
RegisterClassEx(&wc);
win = CreateWindowEx(0, clsName, "3D Raycast Maze + AI & Minimap", WS_OVERLAPPEDWINDOW,
100, 100, WIDTH, HEIGHT, NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(win, SW_SHOW);
UpdateWindow(win);
SetForegroundWindow(win);
SetFocus(win);
CreateBuffer();
MSG msg;
LARGE_INTEGER freq, tNow, tLast;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&tLast);
while (1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
QueryPerformanceCounter(&tNow);
float delta = (float)((double)(tNow.QuadPart - tLast.QuadPart) / freq.QuadPart);
if(delta > 0.1f) delta = 0.1f;
tLast = tNow;
Update(delta);
Render();
HDC hdc = GetDC(win);
BitBlt(hdc, 0, 0, WIDTH, HEIGHT, bufDC, 0, 0, SRCCOPY);
ReleaseDC(win, hdc);
}
DeleteBuffer();
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_SETFOCUS:
hasFocus = true;
ShowCursor(FALSE);
{
RECT rect;
GetClientRect(hWnd, &rect);
POINT center = {rect.right / 2, rect.bottom / 2};
ClientToScreen(hWnd, ¢er);
SetCursorPos(center.x, center.y);
}
break;
case WM_KILLFOCUS:
hasFocus = false;
ShowCursor(TRUE);
break;
case WM_KEYDOWN: keyState[(BYTE)wParam] = true; break;
case WM_KEYUP: keyState[(BYTE)wParam] = false; break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
void CreateBuffer()
{
HDC hdc = GetDC(win);
bufDC = CreateCompatibleDC(hdc);
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = WIDTH;
bmpInfo.bmiHeader.biHeight = -HEIGHT;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bufBmp = CreateDIBSection(bufDC, &bmpInfo, DIB_RGB_COLORS, (void**)&frameBuf, NULL, 0);
SelectObject(bufDC, bufBmp);
ReleaseDC(win, hdc);
}
void DeleteBuffer()
{
DeleteObject(bufBmp);
DeleteDC(bufDC);
}
bool IsBlock(double x, double y)
{
int ix = (int)(x + 0.2); int iy = (int)(y + 0.2);
int ix2 = (int)(x - 0.2); int iy2 = (int)(y - 0.2);
if(ix >=0 && ix < MAZE_W && iy >=0 && iy < MAZE_H)
if(maze[iy][ix]==1) return true;
if(ix2 >=0 && ix2 < MAZE_W && iy2 >=0 && iy2 < MAZE_H)
if(maze[iy2][ix2]==1) return true;
return false;
}
void GenerateTexture() {
for (int y = 0; y < TEX_SIZE; y++) {
for (int x = 0; x < TEX_SIZE; x++) {
unsigned int color;
if (x % 16 == 0 || y % 8 == 0) color = 0x00555555;
else {
int brickX = x;
if ((y / 8) % 2 == 1) brickX += 8;
if (brickX % 16 == 0) color = 0x00555555;
else color = 0x00AA2222;
}
texture[y * TEX_SIZE + x] = color;
}
}
}
void GenerateEnemyTexture() {
for (int y = 0; y < TEX_SIZE; y++) {
for (int x = 0; x < TEX_SIZE; x++) {
bool isBody = (x > 10 && x < 54 && y > 10 && y < 54);
bool isEye = ((x > 18 && x < 28 && y > 20 && y < 30) || (x > 36 && x < 46 && y > 20 && y < 30));
if (isEye) enemyTex[y * TEX_SIZE + x] = 0xFFFFFFFF;
else if (isBody) enemyTex[y * TEX_SIZE + x] = 0xFF22AA22;
else enemyTex[y * TEX_SIZE + x] = 0x00000000;
}
}
}
void GenerateExitTexture() {
for (int y = 0; y < TEX_SIZE; y++) {
for (int x = 0; x < TEX_SIZE; x++) {
int cx = x - TEX_SIZE/2;
int cy = y - TEX_SIZE/2;
int dist = (int)sqrt((double)(cx*cx + cy*cy));
if (dist < TEX_SIZE/2 - 2 && dist > TEX_SIZE/2 - 6)
exitTex[y * TEX_SIZE + x] = 0xFFFFAA00;
else if (dist < 10)
exitTex[y * TEX_SIZE + x] = 0xFFFFDD44;
else
exitTex[y * TEX_SIZE + x] = 0x00000000;
}
}
}
void GenerateHPTex() {
for (int y = 0; y < TEX_SIZE; y++) {
for (int x = 0; x < TEX_SIZE; x++) {
int cx = x - TEX_SIZE/2;
int cy = y - TEX_SIZE/2;
bool isCross = (abs(cx) < 7 && abs(cy) < 18) || (abs(cy) < 7 && abs(cx) < 18);
bool isBg = (abs(cx) < 22 && abs(cy) < 22);
if (isCross)
hpTex[y * TEX_SIZE + x] = 0xFFFF0000;
else if (isBg)
hpTex[y * TEX_SIZE + x] = 0xFFFFFFFF;
else
hpTex[y * TEX_SIZE + x] = 0x00000000;
}
}
}
void DrawMinimap() {
int mapSize = 100;
int cellSize = mapSize / MAZE_W;
int offsetX = WIDTH - mapSize - 10;
int offsetY = 10;
for(int y = 0; y < mapSize; y++)
for(int x = 0; x < mapSize; x++)
frameBuf[(offsetY + y) * WIDTH + (offsetX + x)] = 0x00000000;
for(int y = 0; y < MAZE_H; y++)
for(int x = 0; x < MAZE_W; x++)
if(maze[y][x] == 1)
for(int dy = 0; dy < cellSize; dy++)
for(int dx = 0; dx < cellSize; dx++)
frameBuf[(offsetY + y*cellSize + dy) * WIDTH + (offsetX + x*cellSize + dx)] = 0x00AAAAAA;
for(int i = 0; i < SPRITE_COUNT; i++) {
int ex = offsetX + (int)(sprites[i].x * cellSize);
int ey = offsetY + (int)(sprites[i].y * cellSize);
unsigned int color;
if (i < ENEMY_COUNT)
color = 0x000000FF; // 敌人:蓝
else if (i == ENEMY_COUNT)
color = 0x00FFDD00; // 出口:金
else if (hpActive[i - ENEMY_COUNT - 1])
color = 0x00FF0000; // 血包:红
else
continue; // 已拾取:不显示
for(int dy = -1; dy <= 1; dy++)
for(int dx = -1; dx <= 1; dx++)
if(ex+dx > offsetX && ex+dx < offsetX+mapSize && ey+dy > offsetY && ey+dy < offsetY+mapSize)
frameBuf[(ey+dy) * WIDTH + (ex+dx)] = color;
}
int ppx = offsetX + (int)(px * cellSize);
int ppy = offsetY + (int)(py * cellSize);
for(int dy = -1; dy <= 1; dy++)
for(int dx = -1; dx <= 1; dx++)
if(ppx+dx > offsetX && ppx+dx < offsetX+mapSize && ppy+dy > offsetY && ppy+dy < offsetY+mapSize)
frameBuf[(ppy+dy) * WIDTH + (ppx+dx)] = 0x0000FF00;
}
void DrawHPBar() {
int barW = 300;
int barH = 24;
int barX = (WIDTH - barW) / 2;
int barY = 10;
float ratio = playerHP / playerMaxHP;
if (ratio < 0.0f) ratio = 0.0f;
if (ratio > 1.0f) ratio = 1.0f;
// 暗色底
for (int y = barY; y < barY + barH; y++)
for (int x = barX; x < barX + barW; x++)
frameBuf[y * WIDTH + x] = 0xFF111111;
// 血量填充
int fill = (int)(ratio * (barW - 4));
unsigned int clr;
if (ratio > 0.6f) clr = 0xFF00DD00;
else if (ratio > 0.3f) clr = 0xFFDDDD00;
else clr = 0xFFDD0000;
for (int y = barY + 2; y < barY + barH - 2; y++)
for (int x = barX + 2; x < barX + 2 + fill; x++)
frameBuf[y * WIDTH + x] = clr;
// 白色边框
for (int i = 0; i < barW; i++) {
frameBuf[barY * WIDTH + barX + i] = 0xFFFFFFFF;
frameBuf[(barY + barH - 1) * WIDTH + barX + i] = 0xFFFFFFFF;
}
for (int i = 0; i < barH; i++) {
frameBuf[(barY + i) * WIDTH + barX] = 0xFFFFFFFF;
frameBuf[(barY + i) * WIDTH + barX + barW - 1] = 0xFFFFFFFF;
}
}
void Render()
{
memset(frameBuf, 0, WIDTH * HEIGHT * 4);
for (int x = 0; x < WIDTH; x++)
{
double camX = 2.0 * x / WIDTH - 1.0;
double rDirX = dirX + planeX * camX;
double rDirY = dirY + planeY * camX;
int mapX = (int)px; int mapY = (int)py;
double deltaX = fabs(1 / rDirX); double deltaY = fabs(1 / rDirY);
double sideX, sideY; int stepX, stepY, sideHit; bool hit = false;
if (rDirX < 0) { stepX = -1; sideX = (px - mapX) * deltaX; }
else { stepX = 1; sideX = (mapX + 1.0 - px) * deltaX; }
if (rDirY < 0) { stepY = -1; sideY = (py - mapY) * deltaY; }
else { stepY = 1; sideY = (mapY + 1.0 - py) * deltaY; }
while (!hit) {
if (sideX < sideY) { sideX += deltaX; mapX += stepX; sideHit = 0; }
else { sideY += deltaY; mapY += stepY; sideHit = 1; }
if (mapX >= 0 && mapX < MAZE_W && mapY >= 0 && mapY < MAZE_H) {
if (maze[mapY][mapX] > 0) hit = true;
} else hit = true;
}
double perpDist;
if (sideHit == 0) perpDist = (mapX - px + (1 - stepX) / 2.0) / rDirX;
else perpDist = (mapY - py + (1 - stepY) / 2.0) / rDirY;
zBuffer[x] = perpDist;
double wallX;
if (sideHit == 0) wallX = py + perpDist * rDirY;
else wallX = px + perpDist * rDirX;
wallX -= floor(wallX);
int texX = (int)(wallX * TEX_SIZE);
if (sideHit == 0 && rDirX > 0) texX = TEX_SIZE - texX - 1;
if (sideHit == 1 && rDirY < 0) texX = TEX_SIZE - texX - 1;
int lineH = (int)(HEIGHT / perpDist);
int drawTop = -lineH / 2 + HEIGHT / 2; if (drawTop < 0) drawTop = 0;
int drawBot = lineH / 2 + HEIGHT / 2; if (drawBot >= HEIGHT) drawBot = HEIGHT - 1;
for (int y = drawTop; y <= drawBot; y++) {
int d = y * 256 - HEIGHT * 128 + lineH * 128;
int texY = ((d * TEX_SIZE) / lineH) / 256;
unsigned int color = texture[TEX_SIZE * texY + texX];
double shade = 4.0 / perpDist; if (shade > 1.0) shade = 1.0;
if (sideHit == 1) shade *= 0.7;
int b = ((color >> 16) & 0xFF) * shade;
int g = ((color >> 8) & 0xFF) * shade;
int r = (color & 0xFF) * shade;
frameBuf[y * WIDTH + x] = (255 << 24) | (r << 16) | (g << 8) | b;
}
for(int y = 0; y < drawTop; y++) frameBuf[y * WIDTH + x] = 0x00222222;
for(int y = drawBot + 1; y < HEIGHT; y++) frameBuf[y * WIDTH + x] = 0x00777777;
}
double spriteDist[SPRITE_COUNT]; int spriteOrder[SPRITE_COUNT];
for(int i = 0; i < SPRITE_COUNT; i++) {
double dx = sprites[i].x - px; double dy = sprites[i].y - py;
spriteDist[i] = dx*dx + dy*dy; spriteOrder[i] = i;
}
for(int i = 0; i < SPRITE_COUNT - 1; i++)
for(int j = 0; j < SPRITE_COUNT - i - 1; j++)
if(spriteDist[spriteOrder[j]] < spriteDist[spriteOrder[j+1]])
std::swap(spriteOrder[j], spriteOrder[j+1]);
for(int i = 0; i < SPRITE_COUNT; i++) {
int idx = spriteOrder[i];
// 跳过已拾取的血包
if (idx >= ENEMY_COUNT + 1 && !hpActive[idx - ENEMY_COUNT - 1]) continue;
Sprite& spr = sprites[idx];
double spriteX = spr.x - px; double spriteY = spr.y - py;
double invDet = 1.0 / (planeX * dirY - dirX * planeY);
double transformX = invDet * (dirY * spriteX - dirX * spriteY);
double transformY = invDet * (-planeY * spriteX + planeX * spriteY);
if(transformY <= 0.1) continue;
int spriteScreenX = (int)((WIDTH / 2) * (1 + transformX / transformY));
int spriteHeight = abs((int)(HEIGHT / transformY));
int spriteWidth = abs((int)(HEIGHT / transformY));
int drawStartY = -spriteHeight / 2 + HEIGHT / 2;
int drawEndY = spriteHeight / 2 + HEIGHT / 2;
int drawStartX = -spriteWidth / 2 + spriteScreenX;
int drawEndX = spriteWidth / 2 + spriteScreenX;
for(int stripe = drawStartX; stripe < drawEndX; stripe++) {
int texX = (int)(256 * (stripe - (-spriteWidth / 2 + spriteScreenX)) * TEX_SIZE / spriteWidth) / 256;
if(stripe >= 0 && stripe < WIDTH && transformY < zBuffer[stripe]) {
for(int y = drawStartY; y < drawEndY; y++) {
int texY = (int)(((y - (-spriteHeight / 2 + HEIGHT / 2)) * TEX_SIZE) / spriteHeight);
if(y >= 0 && y < HEIGHT && texY >= 0 && texY < TEX_SIZE) {
unsigned int color = spr.tex[TEX_SIZE * texY + texX];
if((color >> 24) == 0) continue;
double shade = 4.0 / transformY; if(shade > 1.0) shade = 1.0;
int b = ((color >> 16) & 0xFF) * shade;
int g = ((color >> 8) & 0xFF) * shade;
int r = (color & 0xFF) * shade;
frameBuf[y * WIDTH + stripe] = (255 << 24) | (r << 16) | (g << 8) | b;
}
}
}
}
}
if (isDead) {
for (int i = 0; i < WIDTH * HEIGHT; i++) {
unsigned int pixel = frameBuf[i];
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
r = (r + 255) / 2;
g = g / 2;
b = b / 2;
frameBuf[i] = (255 << 24) | (r << 16) | (g << 8) | b;
}
}
if (isWin) {
for (int i = 0; i < WIDTH * HEIGHT; i++) {
unsigned int pixel = frameBuf[i];
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
r = (r + 255) / 2;
g = (g + 255) / 2;
b = b / 3;
frameBuf[i] = (255 << 24) | (r << 16) | (g << 8) | b;
}
}
DrawMinimap();
DrawHPBar();
}
void Update(float dt)
{
// 死亡或胜利状态下,只响应 R 键重置
if (isDead || isWin) {
if (keyState['R']) {
px = 1.5; py = 1.5;
dirX = -1; dirY = 0;
planeX = 0; planeY = 0.66;
isDead = false;
isWin = false;
playerHP = playerMaxHP;
for (int i = 0; i < HP_COUNT; i++) hpActive[i] = true;
}
return;
}
float moveSpd = 2.5f * dt;
// 玩家移动控制
if(keyState[VK_UP] || keyState['W']) { double nx = px + dirX * moveSpd; double ny = py + dirY * moveSpd; if(!IsBlock(nx, py)) px = nx; if(!IsBlock(px, ny)) py = ny; }
if(keyState[VK_DOWN] || keyState['S']) { double nx = px - dirX * moveSpd; double ny = py - dirY * moveSpd; if(!IsBlock(nx, py)) px = nx; if(!IsBlock(px, ny)) py = ny; }
if(keyState['A']) { double nx = px - planeX * moveSpd; double ny = py - planeY * moveSpd; if(!IsBlock(nx, py)) px = nx; if(!IsBlock(px, ny)) py = ny; }
if(keyState['D']) { double nx = px + planeX * moveSpd; double ny = py + planeY * moveSpd; if(!IsBlock(nx, py)) px = nx; if(!IsBlock(px, ny)) py = ny; }
// 鼠标视角控制
if (hasFocus) {
POINT cursorPos;
GetCursorPos(&cursorPos);
ScreenToClient(win, &cursorPos);
int centerX = WIDTH / 2;
int centerY = HEIGHT / 2;
int mouseDX = cursorPos.x - centerX;
// 只在鼠标偏移合理范围内响应(防止切窗口时跳转)
if (abs(mouseDX) > 1 && abs(mouseDX) < 150) {
float mouseRot = -mouseDX * MOUSE_SENS;
double oldX = dirX;
dirX = dirX * cos(mouseRot) - dirY * sin(mouseRot);
dirY = oldX * sin(mouseRot) + dirY * cos(mouseRot);
double pOldX = planeX;
planeX = planeX * cos(mouseRot) - planeY * sin(mouseRot);
planeY = pOldX * sin(mouseRot) + planeY * cos(mouseRot);
}
// 归中光标
POINT center;
center.x = centerX; center.y = centerY;
ClientToScreen(win, ¢er);
SetCursorPos(center.x, center.y);
}
// 敌人AI:BFS寻路 + 滑墙机制
float aiSpd = 0.8f * dt;
for(int i = 0; i < ENEMY_COUNT; i++) {
double dx = px - sprites[i].x;
double dy = py - sprites[i].y;
double dist = sqrt(dx*dx + dy*dy);
// 伤害判定:距离 < 0.6 时持续扣血
if(dist < DAMAGE_RANGE) {
playerHP -= DAMAGE_PER_SEC * dt;
if (playerHP <= 0.0f) {
playerHP = 0.0f;
isDead = true;
break;
}
}
// BFS 寻路(距离 > 0.25 时才移动,防止贴脸抖动)
if (dist > 0.25) {
int enemyGX = (int)sprites[i].x;
int enemyGY = (int)sprites[i].y;
int playerGX = (int)px;
int playerGY = (int)py;
// 如果在同一格或相邻且无遮挡,直接朝玩家走(避免不必要的寻路)
bool directPath = (enemyGX == playerGX && enemyGY == playerGY);
bool visited[MAZE_H][MAZE_W] = {};
int prevX[MAZE_H][MAZE_W];
int prevY[MAZE_H][MAZE_W];
int qx[MAZE_W * MAZE_H], qy[MAZE_W * MAZE_H];
int head = 0, tail = 0;
qx[tail] = enemyGX; qy[tail] = enemyGY; tail++;
visited[enemyGY][enemyGX] = true;
prevX[enemyGY][enemyGX] = -1;
bool found = false;
int nextCellX = enemyGX, nextCellY = enemyGY;
const int dirs[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
while(head < tail) {
int cx = qx[head], cy = qy[head]; head++;
if(cx == playerGX && cy == playerGY) {
// 回溯路径,找到敌人应该走的下一步格子
int tx = cx, ty = cy;
while(true) {
int px2 = prevX[ty][tx];
int py2 = prevY[ty][tx];
if(px2 == -1 || (px2 == enemyGX && py2 == enemyGY)) break;
tx = px2; ty = py2;
}
nextCellX = tx; nextCellY = ty;
found = true;
break;
}
for(int d = 0; d < 4; d++) {
int nx = cx + dirs[d][0], ny = cy + dirs[d][1];
if(nx >= 0 && nx < MAZE_W && ny >= 0 && ny < MAZE_H
&& !visited[ny][nx] && maze[ny][nx] == 0) {
visited[ny][nx] = true;
prevX[ny][nx] = cx;
prevY[ny][nx] = cy;
qx[tail] = nx; qy[tail] = ny; tail++;
}
}
}
// 计算移动方向
double moveX, moveY;
if(found && !directPath) {
// 朝目标格子中心移动
double targetX = nextCellX + 0.5;
double targetY = nextCellY + 0.5;
double tdx = targetX - sprites[i].x;
double tdy = targetY - sprites[i].y;
double tdist = sqrt(tdx*tdx + tdy*tdy);
if(tdist < 0.05) tdist = 0.05;
moveX = (tdx / tdist) * aiSpd;
moveY = (tdy / tdist) * aiSpd;
} else {
// 无路可走或同格时,直接朝玩家移动
moveX = (dx / dist) * aiSpd;
moveY = (dy / dist) * aiSpd;
}
// 滑墙移动
if(!IsBlock(sprites[i].x + moveX, sprites[i].y + moveY)) {
sprites[i].x += moveX;
sprites[i].y += moveY;
} else if(!IsBlock(sprites[i].x + moveX, sprites[i].y)) {
sprites[i].x += moveX;
} else if(!IsBlock(sprites[i].x, sprites[i].y + moveY)) {
sprites[i].y += moveY;
}
} // end if (dist > 0.25)
}
// 胜利判定:玩家到达出口
if (!isDead) {
double exDx = px - sprites[ENEMY_COUNT].x;
double exDy = py - sprites[ENEMY_COUNT].y;
if (sqrt(exDx*exDx + exDy*exDy) < 0.5) {
isWin = true;
}
}
// 血包拾取
if (!isDead && !isWin) {
for (int i = ENEMY_COUNT + 1; i < SPRITE_COUNT; i++) {
int hpIdx = i - ENEMY_COUNT - 1;
if (!hpActive[hpIdx]) continue;
double hpx = px - sprites[i].x;
double hpy = py - sprites[i].y;
if (sqrt(hpx*hpx + hpy*hpy) < 0.5) {
playerHP += 30.0f;
if (playerHP > playerMaxHP) playerHP = playerMaxHP;
hpActive[hpIdx] = false;
}
}
}
}