3Dc++枪战

· · 个人记录

版本:v_1_9_6

地图:

  在小地图上(右上角有小地图,看来作者还是挺良心的),浅橙色的是武器(可以捡的),红色的是医疗包,深橙色的是小小怪(无限刷的),蓝色的是小怪(有四个),黄色的是BOSS。


  在3d图像中,绿色有两只白色的眼睛但很小的是小小怪,体型中等的是小怪,黄色的是BOSS。

操作:

    鼠标控制方向,wasd控制移动,按1~7切换武器(没捡到的不能切换)
切换的武器 对应要按的按键
手枪 1
机枪 2
散弹枪 3
加特零 4
激光枪 5
狙击枪 6
火箭筒 7

提示:

运行时要等10s及以下才能加载好。

打死BOSS才算赢!!!

运行前点开编译器工具里的编译选项,点下面(有两个框,要点下面那个)那个大框框,输入“-lgdi32 -luser32 -static-libgcc”,把小框勾上,就可以运行了!!!

#include <windows.h>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
//-lgdi32 -luser32 -static-libgcc
const int WIDTH = 800;
const int HEIGHT = 600;
const double CAMERA_PLANE_LENGTH = 0.66;
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,1,0,1},
    {1,1,1,0,1,0,1,0,1,0,0,1},
    {1,0,1,0,1,0,1,0,1,0,0,1},
    {1,0,1,0,0,0,0,0,1,0,0,1},
    {1,0,1,1,1,1,1,0,1,1,0,1},
    {1,0,0,0,0,0,1,0,1,0,0,1},
    {1,0,1,0,1,0,1,0,0,0,0,1},
    {1,0,0,1,1,1,1,0,1,1,0,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, MAZE_H=12;
// ── 玩家 ──
double px=1.5, py=1.5, dirX=-1, dirY=0, planeX=0, planeY=CAMERA_PLANE_LENGTH;
bool gameStarted=false,isDead=false,isWin=false,hasFocus=false;
const float MOUSE_SENS=0.003f;
const double MAX_PITCH=0.42;
float playerHP=200.0f;
const float playerMaxHP=200.0f;
const float DAMAGE_RANGE=0.6f;
const float DAMAGE_FLASH_DURATION=0.22f;
float damageFlashTimer=0.0f,damageFlashStrength=0.0f;
double pz=0.0, vz=0.0, pitch=0.0;
const double GRAVITY=22.0, JUMP_VEL=6.5;
const double PITCH_PROJECTION_DISTANCE=WIDTH/(2.0*CAMERA_PLANE_LENGTH), JUMP_PIXEL_SCALE=50.0;
// ── 武器系统 (7种) ──
enum WeaponType { WEAPON_PISTOL, WEAPON_MACHINEGUN, WEAPON_SHOTGUN, WEAPON_GATLING,
                  WEAPON_LASER, WEAPON_SNIPER, WEAPON_ROCKET, WEAPON_COUNT };
WeaponType currentWeapon=WEAPON_PISTOL;
float fireCooldown=0.0f;
float weaponHudPulse[WEAPON_COUNT]={};
bool weaponUnlocked[WEAPON_COUNT]={true,false,false,false,false,false,false};
struct WeaponInfo { float fireRate; int damage; int pellets; float spread; bool isRocket; float blastR; };
WeaponInfo weaponStats[WEAPON_COUNT]={
    {0.40f,25, 1,0.03f,false,0}, // 手枪
    {0.12f,12, 1,0.10f,false,0}, // 机枪
    {0.60f,18,10,0.35f,false,0}, // 散弹
    {0.03f, 8, 1,0.15f,false,0}, // 加特林
    {0.50f,50, 1,0.00f,false,0}, // 激光
    {1.20f,150,1,0.00f,false,0}, // 狙击
    {0.80f,50, 1,0.05f,true,2.0f},// 火箭(范围)
};
const char* weaponNames[WEAPON_COUNT]={"手枪","机枪","散弹","加特林","激光","狙击","火箭"};
const unsigned int weaponColors[WEAPON_COUNT]={
    0xFFFFAA33,0xFF0088FF,0xFFFF44AA,0xFF2222CC,0xFFFFCC00,0xFFAA4422,0xFF0055FF
};
// ── 激光 & 爆炸特效 ──
bool laserBeamActive=false; double laserEndX=0,laserEndY=0; float laserTimer=0.0f;
struct Explosion { double x,y; float timer; bool active; };
const int MAX_EXPLOSIONS=30;
Explosion explosions[MAX_EXPLOSIONS];
// ── 子弹系统 ──
struct Bullet { double x,y,dx,dy; float life; int damage; bool active,isRocket; float blastR; };
const int MAX_BULLETS=150;
Bullet bullets[MAX_BULLETS];
const float BULLET_SPEED=18.0f, ROCKET_SPEED=10.0f;
// ── 敌人 (血量 + BFS缓存) ──
const int MAX_ENEMIES=25, STATIC_ENEMIES=5, HP_COUNT=3, WEAPON_COUNT_PICKUP=6, BOSS_INDEX=3;
float enemyHP[MAX_ENEMIES]={600.0f,600.0f,600.0f,2500.0f,600.0f};
float enemyMaxHPArr[MAX_ENEMIES]={600.0f,600.0f,600.0f,2500.0f,600.0f};
float enemyDamage[MAX_ENEMIES]={12.0f,12.0f,12.0f,25.0f,12.0f};
int eWPX[MAX_ENEMIES], eWPY[MAX_ENEMIES];
int eLastPX[MAX_ENEMIES], eLastPY[MAX_ENEMIES];
struct EnemyDeathEffect { double x,y; float timer; bool active,boss; };
const float ENEMY_DEATH_DURATION=0.48f;
EnemyDeathEffect enemyDeathEffects[MAX_ENEMIES];
float spawnTimer=10.0f;
const float MINION_HP=150.0f, MINION_DAMAGE=8.0f, SPAWN_INTERVAL=10.0f;
// ── 窗口 ──
HWND win; HDC bufDC; HBITMAP bufBmp; BITMAPINFO bmpInfo;
unsigned int* frameBuf; bool keyState[256]={false}; bool mouseDown=false;
// ── 纹理 ──
const int TEX_SIZE=64;
unsigned int texture[TEX_SIZE*TEX_SIZE], enemyTex[TEX_SIZE*TEX_SIZE], bossTex[TEX_SIZE*TEX_SIZE];
unsigned int hpTex[TEX_SIZE*TEX_SIZE], weaponTex[WEAPON_COUNT][TEX_SIZE*TEX_SIZE];
double zBuffer[WIDTH];
// ── 精灵 ──
struct Sprite { double x,y; unsigned int* tex; };
Sprite sprites[]={
    {3.5,3.5,enemyTex},{7.5,5.5,enemyTex},{5.5,8.5,enemyTex}, // 敌0-2
    {1.5,7.5,bossTex},{9.5,3.5,enemyTex},                      // Boss3 敌4
    {-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex}, // 小怪5-9
    {-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex}, // 10-14
    {-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex}, // 15-19
    {-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex},{-99,-99,enemyTex}, // 20-24
    {9.5,1.5,hpTex},{5.5,5.5,hpTex},{1.5,9.5,hpTex},           // 血25-27
    {7.5,1.5,weaponTex[WEAPON_MACHINEGUN]},{5.5,7.5,weaponTex[WEAPON_SHOTGUN]}, // 机枪28 散弹29
    {1.5,5.5,weaponTex[WEAPON_GATLING]},{9.5,5.5,weaponTex[WEAPON_LASER]},      // 加特林30 激光31
    {5.5,3.5,weaponTex[WEAPON_SNIPER]},{7.5,6.5,weaponTex[WEAPON_ROCKET]},      // 狙击32 火箭33
};
const int SPRITE_COUNT=34;
const double enemyStartX[STATIC_ENEMIES]={3.5,7.5,5.5,1.5,9.5};
const double enemyStartY[STATIC_ENEMIES]={3.5,5.5,8.5,7.5,3.5};
bool hpActive[3]={true,true,true}, weaponActive[6]={true,true,true,true,true,true};
int g_spriteOrder[SPRITE_COUNT];
WeaponType weaponPickupType[6]={WEAPON_MACHINEGUN,WEAPON_SHOTGUN,WEAPON_GATLING,
                                WEAPON_LASER,WEAPON_SNIPER,WEAPON_ROCKET};
// ── 声明 ──
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
void CreateBuffer(); void DeleteBuffer();
void Render(); void Update(float dt);
bool IsBlock(double x,double y);
int CameraCenterY();
void GenerateTexture(); void GenerateEnemyTexture(); void GenerateHPTex();
void GenerateWeaponTex(WeaponType type,unsigned int c);
void SelectWeapon(WeaponType type);
void ResetGame();
void UpdatePlayerInput(float dt); void UpdateEffects(float dt); void UpdateJump(float dt);
void UpdateWeapon(float dt); void UpdateBullets(float dt); void UpdateEnemies(float dt); void UpdatePickups();
void SpawnMinion();
void ExplodeRocket(const Bullet& bullet);
void DamagePlayer(float amount);
void DamageEnemy(int enemyIndex,float amount);
void DrawWorldBackground(); void DrawWalls(); void SortSprites(); void DrawWorldSprites(); void DrawProjectiles();
void ApplyGameStateTint();
void DrawMinimap(); void DrawHPBar(); void DrawWeaponHUD(); void DrawEnemyHPBars(); void DrawLaserBeam(); void DrawExplosions();
void DrawDamageEffect(); void DrawEnemyDeathEffects();
void DrawStartScreen(); void DrawEndScreen();
// ═══════════════════ MAIN ═══════════════════
int main(){
    GenerateTexture(); GenerateEnemyTexture(); GenerateHPTex();
    for(int i=0;i<WEAPON_COUNT;i++)GenerateWeaponTex((WeaponType)i,weaponColors[i]);
    const char cn[]="RayCastMaze";
    WNDCLASSEX wc={0}; wc.cbSize=sizeof(WNDCLASSEX);
    wc.lpfnWndProc=WinProc; wc.hInstance=GetModuleHandle(NULL); wc.lpszClassName=cn;
    RegisterClassEx(&wc);
    // framebuffer 是固定 800x600;窗口外框必须额外计入标题栏和边框尺寸。
    const DWORD windowStyle=WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;
    RECT windowRect={0,0,WIDTH,HEIGHT};
    if(!AdjustWindowRectEx(&windowRect,windowStyle,FALSE,0))return 1;
    int windowWidth=windowRect.right-windowRect.left,windowHeight=windowRect.bottom-windowRect.top;
    win=CreateWindowEx(0,cn,"3D Shooter Maze",windowStyle,100,100,windowWidth,windowHeight,NULL,NULL,GetModuleHandle(NULL),NULL);
    if(!win)return 1;
    ShowWindow(win,SW_SHOW); UpdateWindow(win); SetForegroundWindow(win); SetFocus(win); CreateBuffer();
    MSG msg; LARGE_INTEGER f,tN,tL; QueryPerformanceFrequency(&f); QueryPerformanceCounter(&tL);
    while(1){
        while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
            if(msg.message==WM_QUIT) goto quit;
            if(msg.message!=WM_MOUSEMOVE){ TranslateMessage(&msg); DispatchMessage(&msg); }
        }
        QueryPerformanceCounter(&tN);
        float dt=(float)((double)(tN.QuadPart-tL.QuadPart)/f.QuadPart);
        if(dt>0.1f)dt=0.1f; tL=tN;
        Update(dt); Render();
        HDC hdc=GetDC(win); BitBlt(hdc,0,0,WIDTH,HEIGHT,bufDC,0,0,SRCCOPY); ReleaseDC(win,hdc);
        LARGE_INTEGER tE; QueryPerformanceCounter(&tE);
        while((double)(tE.QuadPart-tN.QuadPart)/f.QuadPart<0.0069){QueryPerformanceCounter(&tE);}
    }
    quit: DeleteBuffer(); return 0;
}
// ═══════════════════ WINDOW ═══════════════════
LRESULT CALLBACK WinProc(HWND h,UINT m,WPARAM w,LPARAM l){
    switch(m){
    case WM_SETFOCUS: hasFocus=true; ShowCursor(FALSE);
        { RECT r; GetClientRect(h,&r); POINT c={r.right/2,r.bottom/2}; ClientToScreen(h,&c); SetCursorPos(c.x,c.y); }
        break;
    case WM_KILLFOCUS: hasFocus=false; ShowCursor(TRUE); break;
    case WM_LBUTTONDOWN: mouseDown=true; break;
    case WM_LBUTTONUP: mouseDown=false; break;
    case WM_KEYDOWN: keyState[(BYTE)w]=true; break;
    case WM_KEYUP: keyState[(BYTE)w]=false; break;
    case WM_DESTROY: PostQuitMessage(0); break;
    default: return DefWindowProc(h,m,w,l);
    } 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); }
int CameraCenterY(){
    return HEIGHT/2+(int)lround(tan(pitch)*PITCH_PROJECTION_DISTANCE-pz*JUMP_PIXEL_SCALE);
}
bool IsBlock(double x,double y){
    int ix=(int)(x+0.2),iy=(int)(y+0.2),ix2=(int)(x-0.2),iy2=(int)(y-0.2);
    if(ix>=0&&ix<MAZE_W&&iy>=0&&iy<MAZE_H&&maze[iy][ix]==1)return true;
    if(ix2>=0&&ix2<MAZE_W&&iy2>=0&&iy2<MAZE_H&&maze[iy2][ix2]==1)return true;
    return false;
}
// ═══════════════════ TEXTURES ═══════════════════
void GenerateTexture(){
    for(int y=0;y<TEX_SIZE;y++)for(int x=0;x<TEX_SIZE;x++){
        unsigned int c; if(x%16==0||y%8==0)c=0x00555555;
        else{int bx=x;if((y/8)%2==1)bx+=8;c=(bx%16==0)?0x00555555:0x002222AA;}
        texture[y*TEX_SIZE+x]=c;
    }
}
void GenerateEnemyTexture(){
    for(int y=0;y<TEX_SIZE;y++)for(int x=0;x<TEX_SIZE;x++){
        bool bd=(x>10&&x<54&&y>10&&y<54);
        bool ey=(x>18&&x<28&&y>20&&y<30)||(x>36&&x<46&&y>20&&y<30);
        if(ey){enemyTex[y*TEX_SIZE+x]=0xFFFFFFFF;bossTex[y*TEX_SIZE+x]=0xFFFFFFFF;}
        // 精灵纹理沿用原代码的 BGR 通道布局;0x22CCFF 渲染后是亮黄色。
        else if(bd){enemyTex[y*TEX_SIZE+x]=0xFF22AA22;bossTex[y*TEX_SIZE+x]=0xFF22CCFF;}
        else{enemyTex[y*TEX_SIZE+x]=0x00000000;bossTex[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,cy=y-TEX_SIZE/2;
        bool cr=(abs(cx)<7&&abs(cy)<18)||(abs(cy)<7&&abs(cx)<18),bg=(abs(cx)<22&&abs(cy)<22);
        if(cr)hpTex[y*TEX_SIZE+x]=0xFF0000FF;else if(bg)hpTex[y*TEX_SIZE+x]=0xFFFFFFFF;
        else hpTex[y*TEX_SIZE+x]=0x00000000;
    }
}
void GenerateWeaponTex(WeaponType type,unsigned int clr){
    unsigned int dark=0xFF333333,white=0xFFFFFFFF;
    for(int y=0;y<TEX_SIZE;y++)for(int x=0;x<TEX_SIZE;x++){
        int cx=x-TEX_SIZE/2,cy=y-TEX_SIZE/2; bool in=false; unsigned int pc=clr;
        if(type==WEAPON_PISTOL){ // 手枪: 套筒+短枪管+握把
            if(cx>-16&&cx<15&&cy>-7&&cy<3)in=true;
            else if(cx>12&&cx<24&&cy>-5&&cy<1)in=true;
            else if(cx>-8&&cx<5&&cy>1&&cy<20){in=true;pc=dark;}
            else if(cx>0&&cx<10&&cy>2&&cy<8)in=true;
        }else if(type==WEAPON_MACHINEGUN){ // 机枪: 枪身+弹匣+枪托
            if(cx>5&&cx<30&&abs(cy)<4)in=true;                    // 枪管
            else if(cx>-10&&cx<8&&abs(cy)<10)in=true;              // 枪身
            else if(cx>-28&&cx<-8&&cy>-7&&cy<7&&abs(cy)<7-abs(cx+18)/2){in=true;pc=dark;} // 枪托
            else if(cx>-5&&cx<3&&cy>8&&cy<16)in=true;              // 弹匣
            else if(cx>-8&&cx<-2&&cy>8&&cy<18){in=true;pc=dark;}   // 握把
        }else if(type==WEAPON_SHOTGUN){ // 散弹枪: 宽双管+泵动
            if(cx>5&&cx<22&&abs(cy)<3)in=true;                     // 上管
            else if(cx>5&&cx<22&&abs(cy-6)<3)in=true;              // 下管
            else if(cx>-5&&cx<8&&abs(cy)<10)in=true;               // 泵动护木
            else if(cx>-25&&cx<-3&&abs(cy)<8)in=true;              // 枪身
            else if(cx>-12&&cx<-5&&cy>6&&cy<18){in=true;pc=dark;}  // 握把
        }else if(type==WEAPON_GATLING){ // 加特林: 旋转多管
            if(cx*cx+cy*cy<10*10){in=true;pc=dark;}                // 中心体
            for(int b=0;b<6;b++){float a=b*3.14159f*2/6;int bx=(int)(14*cos(a)),by=(int)(14*sin(a));
                if((cx-bx)*(cx-bx)+(cy-by)*(cy-by)<3*3){in=true;pc=clr;}} // 6根枪管
            if(cx*cx+cy*cy>12*12&&cx*cx+cy*cy<15*15)in=true;      // 外环
            if(cx*cx+cy*cy<3*3){in=true;pc=white;}                 // 中心亮点
        }else if(type==WEAPON_LASER){ // 激光枪: 科幻手枪+发光口
            if(cx>-12&&cx<8&&abs(cy)<10)in=true;                   // 枪身
            else if(cx>6&&cx<24&&abs(cy)<3)in=true;                // 发射管
            else if(cx>22&&cx<26&&abs(cy)<5){in=true;pc=white;}    // 发光口
            else if(cx>-8&&cx<0&&cy>8&&cy<20){in=true;pc=dark;}    // 握把
            else if(cx>-10&&cx<-2&&cy>-3&&cy<5){in=true;pc=0xFF0000FF;} // 能量电池
        }else if(type==WEAPON_SNIPER){ // 狙击枪: 长管+瞄准镜+双脚架
            if(cx>-5&&cx<28&&abs(cy)<2)in=true;                    // 枪管
            else if(cx>0&&cx<12&&cy>-10&&cy<-3)in=true;            // 瞄准镜
            else if(cx>10&&cx<13&&cy>-12&&cy<-1){in=true;pc=0xFFFFCC88;} // 镜片
            else if(cx>-15&&cx<5&&abs(cy)<7)in=true;               // 枪身
            else if(cx>-28&&cx<-12&&abs(cy)<5){in=true;pc=dark;}   // 枪托
            else if(cx>10&&cx<16&&cy>1&&cy<12){in=true;pc=dark;}   // 脚架竖
            else if(cx>8&&cx<18&&cy>10&&cy<13){in=true;pc=dark;}   // 脚架横
        }else{ // 火箭筒: 圆筒+弹头+尾翼
            if(cx>-20&&cx<20&&abs(cy)<8)in=true;                   // 筒身
            else if(cx>15&&cx<26&&abs(cy)<(cx-10)*0.6+1)in=true;   // 弹头锥
            else if(cx>-26&&cx<-18&&abs(cy)>5&&abs(cy)<14)in=true; // 尾翼
            else if(cx>-5&&cx<5&&cy>6&&cy<16){in=true;pc=dark;}    // 握把
        }
        weaponTex[type][y*TEX_SIZE+x]=in?pc:0x00000000;
    }
}
// ═══════════════════ HUD ═══════════════════
void PutPixel(int x,int y,unsigned int color){
    if(x>=0&&x<WIDTH&&y>=0&&y<HEIGHT)frameBuf[y*WIDTH+x]=color;
}
void FillRect(int x,int y,int w,int h,unsigned int color){
    int x0=x<0?0:x,y0=y<0?0:y,x1=x+w>WIDTH?WIDTH:x+w,y1=y+h>HEIGHT?HEIGHT:y+h;
    for(int screenY=y0;screenY<y1;screenY++)for(int screenX=x0;screenX<x1;screenX++)frameBuf[screenY*WIDTH+screenX]=color;
}
void DrawRect(int x,int y,int w,int h,unsigned int color,int thickness){
    FillRect(x,y,w,thickness,color);FillRect(x,y+h-thickness,w,thickness,color);
    FillRect(x,y,thickness,h,color);FillRect(x+w-thickness,y,thickness,h,color);
}
void DrawDigit(int digit,int x,int y,unsigned int color,int scale){
    static const unsigned char rows[7][5]={
        {2,6,2,2,7},{7,1,7,4,7},{7,1,7,1,7},{5,5,7,1,1},
        {7,4,7,1,7},{7,4,7,5,7},{7,1,1,1,1}
    };
    if(digit<1||digit>7)return;
    for(int row=0;row<5;row++)for(int col=0;col<3;col++)
        if(rows[digit-1][row]&(1<<(2-col)))FillRect(x+col*scale,y+row*scale,scale,scale,color);
}
const unsigned char* PixelGlyph(char c){
    static const unsigned char glyphs[36][7]={
        {14,17,19,21,25,17,14},{4,12,4,4,4,4,14},{14,17,1,2,4,8,31},{30,1,1,14,1,1,30},{2,6,10,18,31,2,2},
        {31,16,16,30,1,1,30},{14,16,16,30,17,17,14},{31,1,2,4,8,8,8},{14,17,17,14,17,17,14},{14,17,17,15,1,1,14},
        {14,17,17,31,17,17,17},{30,17,17,30,17,17,30},{14,17,16,16,16,17,14},{30,17,17,17,17,17,30},
        {31,16,16,30,16,16,31},{31,16,16,30,16,16,16},{14,17,16,23,17,17,14},{17,17,17,31,17,17,17},
        {14,4,4,4,4,4,14},{7,2,2,2,18,18,12},{17,18,20,24,20,18,17},{16,16,16,16,16,16,31},
        {17,27,21,21,17,17,17},{17,25,21,19,17,17,17},{14,17,17,17,17,17,14},{30,17,17,30,16,16,16},
        {14,17,17,17,21,18,13},{30,17,17,30,20,18,17},{15,16,16,14,1,1,30},{31,4,4,4,4,4,4},
        {17,17,17,17,17,17,14},{17,17,17,17,17,10,4},{17,17,17,21,21,21,10},{17,17,10,4,10,17,17},
        {17,17,10,4,4,4,4},{31,1,2,4,8,16,31}
    };
    static const unsigned char dash[7]={0,0,0,31,0,0,0};
    if(c>='0'&&c<='9')return glyphs[c-'0'];
    if(c>='A'&&c<='Z')return glyphs[10+c-'A'];
    if(c=='-')return dash;
    return NULL;
}
int PixelTextWidth(const char* text,int scale){
    int length=(int)strlen(text);return length>0?(length*6-1)*scale:0;
}
void DrawPixelText(const char* text,int x,int y,unsigned int color,int scale){
    for(int i=0;text[i];i++){
        const unsigned char* glyph=PixelGlyph(text[i]);
        if(glyph)for(int row=0;row<7;row++)for(int col=0;col<5;col++)
            if(glyph[row]&(1<<(4-col)))FillRect(x+col*scale,y+row*scale,scale,scale,color);
        x+=6*scale;
    }
}
void DrawCenteredText(const char* text,int y,unsigned int color,int scale){
    DrawPixelText(text,(WIDTH-PixelTextWidth(text,scale))/2,y,color,scale);
}
void DimFrame(int percent){
    for(int i=0;i<WIDTH*HEIGHT;i++){
        unsigned int p=frameBuf[i];int r=(p>>16)&0xFF,g=(p>>8)&0xFF,b=p&0xFF;
        r=r*percent/100;g=g*percent/100;b=b*percent/100;
        frameBuf[i]=0xFF000000|(r<<16)|(g<<8)|b;
    }
}
void DrawStartScreen(){
    DimFrame(32);
    FillRect(120,75,560,450,0xFF101720);DrawRect(120,75,560,450,0xFF5C7185,3);
    DrawCenteredText("3D MAZE",125,0xFFFFCC33,5);
    DrawCenteredText("SHOOTER",180,0xFFFF7733,4);
    FillRect(250,225,300,2,0xFF5C7185);
    DrawCenteredText("PRESS ENTER",270,0xFFFFFFFF,3);
    DrawCenteredText("TO START",310,0xFF9FB4C7,2);
    DrawCenteredText("WASD MOVE",385,0xFFB8C6D1,2);
    DrawCenteredText("MOUSE LOOK",415,0xFFB8C6D1,2);
    DrawCenteredText("1-7 WEAPONS",445,0xFFB8C6D1,2);
}
void DrawEndScreen(){
    DimFrame(34);
    FillRect(155,145,490,310,0xFF101720);
    unsigned int accent=isWin?0xFF44DD66:0xFFFF5544;
    DrawRect(155,145,490,310,accent,3);
    DrawCenteredText(isWin?"YOU WIN":"GAME OVER",195,accent,5);
    DrawCenteredText(isWin?"ALL ENEMIES DEFEATED":"THE MAZE GOT YOU",275,0xFFD7E1E8,2);
    FillRect(245,325,310,2,0xFF5C7185);
    DrawCenteredText("PRESS R OR ENTER",365,0xFFFFFFFF,2);
    DrawCenteredText("TO RESTART",400,0xFF9FB4C7,2);
}
unsigned int DimWeaponColor(unsigned int color){
    int r=(color>>16)&0xFF,g=(color>>8)&0xFF,b=color&0xFF;
    int gray=(r+g+b)/9+28;
    return 0xFF000000|(gray<<16)|(gray<<8)|gray;
}
void SelectWeapon(WeaponType type){
    if(type<WEAPON_PISTOL||type>=WEAPON_COUNT||!weaponUnlocked[type]||currentWeapon==type)return;
    currentWeapon=type;weaponHudPulse[type]=0.28f;
}
void ResetGame(){
    px=1.5;py=1.5;dirX=-1;dirY=0;planeX=0;planeY=CAMERA_PLANE_LENGTH;
    pz=0.0;vz=0.0;pitch=0.0;playerHP=playerMaxHP;
    damageFlashTimer=0.0f;damageFlashStrength=0.0f;
    gameStarted=true;isDead=false;isWin=false;mouseDown=false;
    currentWeapon=WEAPON_PISTOL;fireCooldown=0;laserBeamActive=false;laserTimer=0;
    for(int i=0;i<WEAPON_COUNT;i++){
        weaponUnlocked[i]=(i==WEAPON_PISTOL);weaponHudPulse[i]=0.0f;
    }
    weaponHudPulse[WEAPON_PISTOL]=0.5f;
    for(int i=0;i<HP_COUNT;i++)hpActive[i]=true;
    for(int i=0;i<WEAPON_COUNT_PICKUP;i++)weaponActive[i]=true;
    for(int i=0;i<STATIC_ENEMIES;i++){
        sprites[i].x=enemyStartX[i];sprites[i].y=enemyStartY[i];enemyHP[i]=enemyMaxHPArr[i];
        eWPX[i]=eWPY[i]=-1;eLastPX[i]=eLastPY[i]=-1;
        enemyDeathEffects[i].active=false;
    }
    for(int i=STATIC_ENEMIES;i<MAX_ENEMIES;i++){
        sprites[i].x=-99;sprites[i].y=-99;enemyHP[i]=0;enemyMaxHPArr[i]=MINION_HP;enemyDamage[i]=MINION_DAMAGE;
        eWPX[i]=eWPY[i]=-1;eLastPX[i]=eLastPY[i]=-1;
        enemyDeathEffects[i].active=false;
    }
    spawnTimer=SPAWN_INTERVAL;
    for(int i=0;i<MAX_BULLETS;i++)bullets[i].active=false;
    for(int i=0;i<MAX_EXPLOSIONS;i++)explosions[i].active=false;
}
void DrawWeaponHUD(){
    const int slotW=48,slotH=50,gap=4,iconSize=34;
    const int totalW=WEAPON_COUNT*slotW+(WEAPON_COUNT-1)*gap;
    const int startX=(WIDTH-totalW)/2,startY=HEIGHT-slotH-8;
    for(int i=0;i<WEAPON_COUNT;i++){
        int sx=startX+i*(slotW+gap),sy=startY;
        bool unlocked=weaponUnlocked[i],selected=(currentWeapon==(WeaponType)i),pulsing=weaponHudPulse[i]>0.0f;
        FillRect(sx,sy,slotW,slotH,unlocked?0xE0182028:0xE0101215);
        unsigned int border=pulsing?0xFFFFFFFF:(selected?0xFFFFCC33:0xFF66717A);
        DrawRect(sx,sy,slotW,slotH,border,selected||pulsing?3:1);
        int iconX=sx+(slotW-iconSize)/2,iconY=sy+8;
        for(int dy=0;dy<iconSize;dy++)for(int dx=0;dx<iconSize;dx++){
            int tx=dx*TEX_SIZE/iconSize,ty=dy*TEX_SIZE/iconSize;
            unsigned int pixel=weaponTex[i][ty*TEX_SIZE+tx];
            if((pixel>>24)==0)continue;
            PutPixel(iconX+dx,iconY+dy,unlocked?pixel:DimWeaponColor(pixel));
        }
        DrawDigit(i+1,sx+4,sy+4,unlocked?0xFFFFFFFF:0xFF666666,1);
        if(!unlocked)for(int d=0;d<28;d++){
            PutPixel(sx+10+d,sy+38-d,0xFF993333);PutPixel(sx+10+d,sy+39-d,0xFF993333);
        }
        if(selected)FillRect(sx+5,sy+slotH-5,slotW-10,2,0xFFFFCC33);
    }
}
void DrawMinimap(){
    const int ms=100,ox=WIDTH-ms-10,oy=10,centerX=ox+ms/2,centerY=oy+ms/2;
    const double pixelsPerCell=9.0;
    // 相机右方向与当前射线平面一致;玩家前方固定映射到小地图上方。
    double rightX=dirY,rightY=-dirX;
    for(int sy=0;sy<ms;sy++)for(int sx=0;sx<ms;sx++){
        double localRight=(sx-ms/2)/pixelsPerCell;
        double localForward=-(sy-ms/2)/pixelsPerCell;
        double worldX=px+rightX*localRight+dirX*localForward;
        double worldY=py+rightY*localRight+dirY*localForward;
        unsigned int color=0xFF090D12;
        if(worldX>=0&&worldX<MAZE_W&&worldY>=0&&worldY<MAZE_H){
            int mapX=(int)floor(worldX),mapY=(int)floor(worldY);
            color=maze[mapY][mapX]==1?0xFFAAAAAA:0xFF1A2026;
        }
        PutPixel(ox+sx,oy+sy,color);
    }
    DrawRect(ox,oy,ms,ms,0xFFFFFFFF,2);
    // 世界物体使用与地图完全相同的玩家相对旋转。
    for(int i=0;i<SPRITE_COUNT;i++){
        int ex=centerX+(int)lround(((sprites[i].x-px)*rightX+(sprites[i].y-py)*rightY)*pixelsPerCell);
        int ey=centerY-(int)lround(((sprites[i].x-px)*dirX+(sprites[i].y-py)*dirY)*pixelsPerCell);
        unsigned int clr;
        if(i<MAX_ENEMIES){if(enemyHP[i]<=0)continue;clr=(i==BOSS_INDEX)?0xFFFFFF22:(i<STATIC_ENEMIES?0xFF0000FF:0xFFFF6622);}
        else if(i<MAX_ENEMIES+HP_COUNT){if(!hpActive[i-MAX_ENEMIES])continue;clr=0x00FF0000;}
        else{if(!weaponActive[i-MAX_ENEMIES-HP_COUNT])continue;clr=0x00FFAA00;}
        for(int dotY=-1;dotY<=1;dotY++)for(int dotX=-1;dotX<=1;dotX++)
            if(ex+dotX>ox+1&&ex+dotX<ox+ms-1&&ey+dotY>oy+1&&ey+dotY<oy+ms-1)PutPixel(ex+dotX,ey+dotY,clr);
    }
    // 玩家箭头固定朝上;地图旋转负责表达方向变化。
    for(int row=0;row<9;row++){
        int half=row/2;
        for(int dx=-half;dx<=half;dx++)PutPixel(centerX+dx,centerY-6+row,0xFF00FF44);
    }
    FillRect(centerX-1,centerY+3,3,5,0xFF00CC33);
}
void DrawHPBar(){
    int bw=300,bh=24,bx=(WIDTH-bw)/2,by=10;
    float r=playerHP/playerMaxHP;if(r<0)r=0;if(r>1)r=1;
    for(int y=by;y<by+bh;y++)for(int x=bx;x<bx+bw;x++)frameBuf[y*WIDTH+x]=0xFF111111;
    int fill=(int)(r*(bw-4));
    unsigned int clr=(r>0.6f)?0xFF00DD00:(r>0.3f)?0xFFDDDD00:0xFFDD0000;
    for(int y=by+2;y<by+bh-2;y++)for(int x=bx+2;x<bx+2+fill;x++)frameBuf[y*WIDTH+x]=clr;
    for(int i=0;i<bw;i++){frameBuf[by*WIDTH+bx+i]=0xFFFFFFFF;frameBuf[(by+bh-1)*WIDTH+bx+i]=0xFFFFFFFF;}
    for(int i=0;i<bh;i++){frameBuf[(by+i)*WIDTH+bx]=0xFFFFFFFF;frameBuf[(by+i)*WIDTH+bx+bw-1]=0xFFFFFFFF;}
}
void DrawEnemyHPBars(){
    double id=1.0/(planeX*dirY-dirX*planeY);
    int cameraCenterY=CameraCenterY();
    for(int si=0;si<SPRITE_COUNT;si++){
        int idx=g_spriteOrder[si]; if(idx>=MAX_ENEMIES||enemyHP[idx]<=0)continue;
        double sx=sprites[idx].x-px,sy=sprites[idx].y-py;
        double tY=id*(-planeY*sx+planeX*sy);if(tY<=0.1)continue;
        double tX=id*(dirY*sx-dirX*sy);
        int scrX=(int)((WIDTH/2)*(1+tX/tY)),projectedWallH=abs((int)(HEIGHT/tY));
        int sprH=projectedWallH*(idx==BOSS_INDEX?82:idx>=STATIC_ENEMIES?48:72)/100;
        // 太远时怪物本体已经很小,继续强制显示血条只会留下悬空色线。
        if(sprH<18)continue;
        int barW=(int)(sprH*0.65);if(barW<14)barW=14;if(barW>72)barW=72;
        int floorY=cameraCenterY+projectedWallH/2;
        int barH=6,barX=scrX-barW/2,barY=floorY-sprH*84/100-10;
        // 怪物头部已经离开画面时直接隐藏,不把血条钉在屏幕边缘。
        if(barY<0||barY+barH>HEIGHT||barX>=WIDTH||barX+barW<=0)continue;
        // 至少有一个血条列与可见的怪物深度一致才绘制,避免隔墙显示。
        bool visible=false;
        for(int x=barX;x<barX+barW;x++)
            if(x>=0&&x<WIDTH&&tY<zBuffer[x]){visible=true;break;}
        if(!visible)continue;
        float r=enemyHP[idx]/enemyMaxHPArr[idx];if(r<0)r=0;if(r>1)r=1;
        int innerW=barW-2,fillW=(int)(r*innerW+0.5f);
        unsigned int hpColor=(r>0.6f)?0xFF00CC00:(r>0.3f)?0xFFCCCC00:0xFFCC0000;
        for(int y=barY;y<barY+barH;y++)for(int x=barX;x<barX+barW;x++){
            if(x<0||x>=WIDTH||tY>=zBuffer[x])continue;
            bool border=(x==barX||x==barX+barW-1||y==barY||y==barY+barH-1);
            if(border)frameBuf[y*WIDTH+x]=0xFF111111;
            else if(x<barX+1+fillW)frameBuf[y*WIDTH+x]=hpColor;
            else frameBuf[y*WIDTH+x]=0xFF442222;
        }
    }
}
void DrawLaserBeam(){
    if(!laserBeamActive)return;
    double id=1.0/(planeX*dirY-dirX*planeY);
    int scrX0=WIDTH/2;
    int scrY0=CameraCenterY();
    double bx=laserEndX-px,by=laserEndY-py;
    double tY=id*(-planeY*bx+planeX*by);if(tY<=0.1)return;
    double tX=id*(dirY*bx-dirX*by);
    int scrX1=(int)((WIDTH/2)*(1+tX/tY));
    int scrY1=CameraCenterY();
    int dx=scrX1-scrX0,dy=scrY1-scrY0,steps=abs(dx)>abs(dy)?abs(dx):abs(dy);if(steps<1)steps=1;
    for(int s=0;s<=steps;s++){
        int sx=scrX0+(int)(dx*(float)s/steps),sy=scrY0+(int)(dy*(float)s/steps);
        for(int dy2=-2;dy2<=2;dy2++)for(int dx2=-2;dx2<=2;dx2++)
            if(sx+dx2>=0&&sx+dx2<WIDTH&&sy+dy2>=0&&sy+dy2<HEIGHT)frameBuf[(sy+dy2)*WIDTH+(sx+dx2)]=0xFFFF3333;
    }
}
void DrawExplosions(){
    double id=1.0/(planeX*dirY-dirX*planeY);
    int cameraCenterY=CameraCenterY();
    for(int i=0;i<MAX_EXPLOSIONS;i++){
        if(!explosions[i].active)continue;
        double ex=explosions[i].x-px,ey=explosions[i].y-py;
        double tY=id*(-planeY*ex+planeX*ey);if(tY<=0.1)continue;
        double tX=id*(dirY*ex-dirX*ey);
        int scrX=(int)((WIDTH/2)*(1+tX/tY));
        float progress=1.0f-explosions[i].timer/0.35f;
        int radius=(int)((20.0f+60.0f*progress)/tY);if(radius<2)radius=2;if(radius>60)radius=60;
        for(int dy=-radius;dy<=radius;dy++){
            int py2=cameraCenterY+dy;if(py2<0||py2>=HEIGHT)continue;
            for(int dx=-radius;dx<=radius;dx++){
                int px2=scrX+dx;if(px2<0||px2>=WIDTH)continue;
                int ds=dx*dx+dy*dy;
                if(ds<=radius*radius&&ds>=(radius-3)*(radius-3)){
                    unsigned int eclr=(progress>0.5f)?0xFFFF6600:0xFF884400;
                    frameBuf[py2*WIDTH+px2]=eclr;
                }
            }
        }
    }
}
void DrawEnemyDeathEffects(){
    double id=1.0/(planeX*dirY-dirX*planeY);
    int cameraCenterY=CameraCenterY();
    for(int i=0;i<MAX_ENEMIES;i++){
        EnemyDeathEffect& effect=enemyDeathEffects[i];
        if(!effect.active)continue;
        double ex=effect.x-px,ey=effect.y-py;
        double tY=id*(-planeY*ex+planeX*ey);if(tY<=0.1)continue;
        double tX=id*(dirY*ex-dirX*ey);
        int scrX=(int)((WIDTH/2)*(1+tX/tY));
        if(scrX<0||scrX>=WIDTH||tY>=zBuffer[scrX])continue;
        int wallH=abs((int)(HEIGHT/tY));
        int enemyH=wallH*(effect.boss?82:72)/100;
        int floorY=cameraCenterY+wallH/2;
        int centerY=floorY-enemyH*42/100;
        float progress=1.0f-effect.timer/ENEMY_DEATH_DURATION;
        if(progress<0.0f)progress=0.0f;if(progress>1.0f)progress=1.0f;
        int spread=(int)(enemyH*(0.08f+0.48f*progress));
        int dotSize=enemyH/22;if(dotSize<1)dotSize=1;if(dotSize>5)dotSize=5;
        float brightness=1.0f-progress*0.75f;
        int r=(int)((effect.boss?255:55)*brightness);
        int g=(int)((effect.boss?210:220)*brightness);
        int b=(int)((effect.boss?45:70)*brightness);
        unsigned int color=0xFF000000|(r<<16)|(g<<8)|b;
        for(int p=0;p<12;p++){
            double angle=p*3.14159265358979323846/6.0+(i%3)*0.18;
            int sx=scrX+(int)(cos(angle)*spread);
            int sy=centerY+(int)(sin(angle)*spread*0.72);
            for(int dy=-dotSize;dy<=dotSize;dy++)for(int dx=-dotSize;dx<=dotSize;dx++)
                if(dx*dx+dy*dy<=dotSize*dotSize&&sx+dx>=0&&sx+dx<WIDTH&&sy+dy>=0&&sy+dy<HEIGHT)
                    frameBuf[(sy+dy)*WIDTH+sx+dx]=color;
        }
    }
}
// ═══════════════════ RENDER ═══════════════════
void DrawWorldBackground(){
    memset(frameBuf,0,WIDTH*HEIGHT*4);
    int cameraCenterY=CameraCenterY();
    int horizon=cameraCenterY;
    if(horizon<0)horizon=0; if(horizon>=HEIGHT)horizon=HEIGHT-1;
    for(int y=0;y<horizon;y++)
        for(int x=0;x<WIDTH;x++)frameBuf[y*WIDTH+x]=0xFF332222;
    for(int y=horizon;y<HEIGHT;y++){
        double rDist=(double)HEIGHT/(2.0*(y-horizon)+1.0);
        double sh=2.0/rDist; if(sh>1.0)sh=1.0; if(sh<0.15)sh=0.15;
        int c=(int)(0x88*sh);
        for(int x=0;x<WIDTH;x++)frameBuf[y*WIDTH+x]=(255<<24)|(c<<16)|(c<<8)|c;
    }
}
void DrawWalls(){
    int cameraCenterY=CameraCenterY();
    for(int x=0;x<WIDTH;x++){
        double cX=2.0*x/WIDTH-1.0;
        double rDX=dirX+planeX*cX,rDY=dirY+planeY*cX;
        if(fabs(rDX)<1e-10)rDX=(rDX>=0)?1e-10:-1e-10;
        if(fabs(rDY)<1e-10)rDY=(rDY>=0)?1e-10:-1e-10;
        int mX=(int)px,mY=(int)py;
        double dX=fabs(1/rDX),dY=fabs(1/rDY),sX,sY;int stX,stY,side=0;bool hit=false;
        if(rDX<0){stX=-1;sX=(px-mX)*dX;}else{stX=1;sX=(mX+1.0-px)*dX;}
        if(rDY<0){stY=-1;sY=(py-mY)*dY;}else{stY=1;sY=(mY+1.0-py)*dY;}
        while(!hit){
            if(sX<sY){sX+=dX;mX+=stX;side=0;}else{sY+=dY;mY+=stY;side=1;}
            if(mX>=0&&mX<MAZE_W&&mY>=0&&mY<MAZE_H){if(maze[mY][mX]>0)hit=true;}else hit=true;
        }
        double pD=(side==0)?(mX-px+(1-stX)/2.0)/rDX:(mY-py+(1-stY)/2.0)/rDY;zBuffer[x]=pD;
        double wX=(side==0)?py+pD*rDY:px+pD*rDX;wX-=floor(wX);
        int tX=(int)(wX*TEX_SIZE);
        if(side==0&&rDX>0)tX=TEX_SIZE-tX-1;if(side==1&&rDY<0)tX=TEX_SIZE-tX-1;
        int lH=(int)(HEIGHT/pD);
        int dT=-lH/2+cameraCenterY;if(dT<0)dT=0;
        int dB=lH/2+cameraCenterY;if(dB>=HEIGHT)dB=HEIGHT-1;
        for(int y=dT;y<=dB;y++){
            int d=(y-cameraCenterY)*256+lH*128,tY=((d*TEX_SIZE)/lH)/256;
            if(tY<0)tY=0;if(tY>=TEX_SIZE)tY=TEX_SIZE-1;
            unsigned int c=texture[TEX_SIZE*tY+tX];
            double sh=4.0/pD;if(sh>1.0)sh=1.0;if(side==1)sh*=0.7;
            int b=(int)(((c>>16)&0xFF)*sh),g=(int)(((c>>8)&0xFF)*sh),r=(int)((c&0xFF)*sh);
            frameBuf[y*WIDTH+x]=(255<<24)|(r<<16)|(g<<8)|b;
        }
    }
}
void SortSprites(){
    double sDist[SPRITE_COUNT];
    for(int i=0;i<SPRITE_COUNT;i++){double dx=sprites[i].x-px,dy=sprites[i].y-py;sDist[i]=dx*dx+dy*dy;g_spriteOrder[i]=i;}
    for(int i=0;i<SPRITE_COUNT-1;i++)for(int j=0;j<SPRITE_COUNT-i-1;j++)
        if(sDist[g_spriteOrder[j]]<sDist[g_spriteOrder[j+1]])std::swap(g_spriteOrder[j],g_spriteOrder[j+1]);
}
void DrawWorldSprites(){
    int cameraCenterY=CameraCenterY();
    double id=1.0/(planeX*dirY-dirX*planeY);
    for(int si=0;si<SPRITE_COUNT;si++){
        int idx=g_spriteOrder[si];
        if(idx<MAX_ENEMIES&&enemyHP[idx]<=0)continue;
        if(idx>=MAX_ENEMIES&&idx<MAX_ENEMIES+HP_COUNT&&!hpActive[idx-MAX_ENEMIES])continue;
        int wIdx=idx-MAX_ENEMIES-HP_COUNT;
        if(wIdx>=0&&!weaponActive[wIdx])continue;
        Sprite& spr=sprites[idx];
        double sX=spr.x-px,sY=spr.y-py;
        double tY=id*(-planeY*sX+planeX*sY);if(tY<=0.1)continue;
        double tX=id*(dirY*sX-dirX*sY);
        int scrX=(int)((WIDTH/2)*(1+tX/tY));
        int projectedWallH=abs((int)(HEIGHT/tY));if(projectedWallH<1)projectedWallH=1;
        bool isPickup=idx>=MAX_ENEMIES;
        bool isBoss=idx==BOSS_INDEX;
        bool isMinion=idx>=STATIC_ENEMIES&&!isBoss;
        int pickupH=projectedWallH*45/100;if(pickupH<8)pickupH=8;
        int enemyH=projectedWallH*(isBoss?82:isMinion?48:72)/100;if(enemyH<4)enemyH=4;
        int sprH=isPickup?pickupH:enemyH;
        int sprW=sprH;
        int dsY,deY;
        if(isPickup){
            int floorY=cameraCenterY+projectedWallH/2;
            deY=floorY+sprH*18/100;dsY=deY-sprH;
        }else{
            // 怪物缩小后仍以脚下地面为锚点,Boss 略大但依然小于旧尺寸。
            int floorY=cameraCenterY+projectedWallH/2;
            deY=floorY+sprH*16/100;dsY=deY-sprH;
        }
        int dsX=-sprW/2+scrX,deX=dsX+sprW;
        for(int st=dsX;st<deX;st++){
            int texX=((st-dsX)*TEX_SIZE)/sprW;
            if(st>=0&&st<WIDTH&&tY<zBuffer[st]){
                for(int y=dsY;y<deY;y++){
                    int texY=(int)(((y-dsY)*TEX_SIZE)/sprH);
                    if(y>=0&&y<HEIGHT&&texY>=0&&texY<TEX_SIZE){
                        unsigned int c=spr.tex[TEX_SIZE*texY+texX];
                        if((c>>24)==0)continue;
                        double sh=4.0/tY;if(sh>1.0)sh=1.0;
                        int b=(int)(((c>>16)&0xFF)*sh),g=(int)(((c>>8)&0xFF)*sh),r=(int)((c&0xFF)*sh);
                        frameBuf[y*WIDTH+st]=(255<<24)|(r<<16)|(g<<8)|b;
                    }
                }
            }
        }
    }
}
void DrawProjectiles(){
    int cameraCenterY=CameraCenterY();
    double id=1.0/(planeX*dirY-dirX*planeY);
    for(int i=0;i<MAX_BULLETS;i++){
        if(!bullets[i].active)continue;
        double bX=bullets[i].x-px,bY=bullets[i].y-py;
        double tY=id*(-planeY*bX+planeX*bY);if(tY<=0.1)continue;
        double tX=id*(dirY*bX-dirX*bY);
        int scrX=(int)((WIDTH/2)*(1+tX/tY));
        int sz=bullets[i].isRocket?(int)(10.0/tY):(int)(6.0/tY);
        if(sz<1)sz=1;if(sz>15)sz=15;
        unsigned int bClr=bullets[i].isRocket?0xFFFF4400:0xFFFFDD00;
        for(int dy=-sz;dy<=sz;dy++){int py2=cameraCenterY+dy;if(py2<0||py2>=HEIGHT)continue;
            for(int dx=-sz;dx<=sz;dx++){int px2=scrX+dx;if(px2<0||px2>=WIDTH)continue;
                if(dx*dx+dy*dy<=sz*sz)frameBuf[py2*WIDTH+px2]=bClr;}}
    }
}
void DrawDamageEffect(){
    if(damageFlashTimer<=0.0f)return;
    float fade=damageFlashTimer/DAMAGE_FLASH_DURATION;
    if(fade>1.0f)fade=1.0f;
    float baseAlpha=damageFlashStrength*fade;
    for(int y=0;y<HEIGHT;y++){
        float ny=fabs((2.0f*y)/(HEIGHT-1)-1.0f);
        for(int x=0;x<WIDTH;x++){
            float nx=fabs((2.0f*x)/(WIDTH-1)-1.0f);
            float edge=nx>ny?nx:ny;
            float vignette=(edge-0.32f)/0.68f;
            if(vignette<=0.0f)continue;
            if(vignette>1.0f)vignette=1.0f;
            float alpha=baseAlpha*vignette*vignette*0.62f;
            unsigned int pixel=frameBuf[y*WIDTH+x];
            int r=(pixel>>16)&0xFF,g=(pixel>>8)&0xFF,b=pixel&0xFF;
            r=(int)(r+(255-r)*alpha);g=(int)(g*(1.0f-alpha));b=(int)(b*(1.0f-alpha));
            frameBuf[y*WIDTH+x]=0xFF000000|(r<<16)|(g<<8)|b;
        }
    }
}
void ApplyGameStateTint(){
    if(isDead){for(int i=0;i<WIDTH*HEIGHT;i++){unsigned int p=frameBuf[i];int r=((p>>16)&0xFF),g=((p>>8)&0xFF),b=p&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 p=frameBuf[i];int r=((p>>16)&0xFF),g=((p>>8)&0xFF),b=p&0xFF;r=(r+255)/2;g=(g+255)/2;b=b/3;frameBuf[i]=(255<<24)|(r<<16)|(g<<8)|b;}}
}
void Render(){
    DrawWorldBackground();
    DrawWalls();
    SortSprites();
    DrawWorldSprites();
    DrawProjectiles();
    DrawLaserBeam();
    DrawExplosions();
    DrawEnemyDeathEffects();
    DrawEnemyHPBars();
    DrawDamageEffect();
    ApplyGameStateTint();
    if(gameStarted){DrawMinimap();DrawHPBar();DrawWeaponHUD();}
    if(!gameStarted)DrawStartScreen();
    else if(isDead||isWin)DrawEndScreen();
}
// ═══════════════════ UPDATE ═══════════════════
void UpdatePlayerInput(float dt){
    float moveSpd=2.5f*dt;
    if(keyState[VK_UP]||keyState['W']){double nx=px+dirX*moveSpd,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,ny=py-dirY*moveSpd;if(!IsBlock(nx,py))px=nx;if(!IsBlock(px,ny))py=ny;}
    if(keyState['A']){double nx=px-planeX*moveSpd,ny=py-planeY*moveSpd;if(!IsBlock(nx,py))px=nx;if(!IsBlock(px,ny))py=ny;}
    if(keyState['D']){double nx=px+planeX*moveSpd,ny=py+planeY*moveSpd;if(!IsBlock(nx,py))px=nx;if(!IsBlock(px,ny))py=ny;}
    // 鼠标(增量追踪,横+纵)
    if(hasFocus){
        static POINT prevCP={-1,-1}; static bool cpInit=false;
        if(!cpInit){POINT ip;GetCursorPos(&ip);ScreenToClient(win,&ip);prevCP=ip;cpInit=true;}
        POINT cp;GetCursorPos(&cp);ScreenToClient(win,&cp);
        int mdx=cp.x-prevCP.x, mdy=cp.y-prevCP.y; prevCP=cp;
        if(abs(mdx)>0&&abs(mdx)<200){float mr=-mdx*MOUSE_SENS;
            double odX=dirX;dirX=dirX*cos(mr)-dirY*sin(mr);dirY=odX*sin(mr)+dirY*cos(mr);
            double opX=planeX;planeX=planeX*cos(mr)-planeY*sin(mr);planeY=opX*sin(mr)+planeY*cos(mr);}
        if(abs(mdy)>0&&abs(mdy)<200){pitch-=mdy*MOUSE_SENS;
            if(pitch>MAX_PITCH)pitch=MAX_PITCH; if(pitch<-MAX_PITCH)pitch=-MAX_PITCH;}
        if(cp.x<40||cp.x>WIDTH-40||cp.y<40||cp.y>HEIGHT-40){
            POINT cc;cc.x=WIDTH/2;cc.y=HEIGHT/2;ClientToScreen(win,&cc);SetCursorPos(cc.x,cc.y);
            prevCP.x=WIDTH/2;prevCP.y=HEIGHT/2;
        }
    }
    // 武器切换 1-7
    for(int i=0;i<WEAPON_COUNT;i++)if(keyState['1'+i])SelectWeapon((WeaponType)i);
}
void UpdateEffects(float dt){
    if(laserBeamActive){laserTimer-=dt;if(laserTimer<=0)laserBeamActive=false;}
    if(damageFlashTimer>0.0f){
        damageFlashTimer-=dt;
        if(damageFlashTimer<=0.0f){damageFlashTimer=0.0f;damageFlashStrength=0.0f;}
    }
    for(int i=0;i<MAX_ENEMIES;i++)if(enemyDeathEffects[i].active){
        enemyDeathEffects[i].timer-=dt;
        if(enemyDeathEffects[i].timer<=0.0f)enemyDeathEffects[i].active=false;
    }
    for(int i=0;i<MAX_EXPLOSIONS;i++){if(explosions[i].active){explosions[i].timer-=dt;if(explosions[i].timer<=0)explosions[i].active=false;}}
    for(int i=0;i<WEAPON_COUNT;i++){if(weaponHudPulse[i]>0){weaponHudPulse[i]-=dt;if(weaponHudPulse[i]<0)weaponHudPulse[i]=0;}}
}
void UpdateJump(float dt){
    if(pz>0.0||vz!=0.0){vz-=GRAVITY*dt; pz+=vz*dt; if(pz<0.0){pz=0.0;vz=0.0;}}
    if(keyState[VK_SPACE]&&pz==0.0){vz=JUMP_VEL; pz=0.01;}
}
void UpdateWeapon(float dt){
    if(fireCooldown>0)fireCooldown-=dt;
    if(mouseDown&&fireCooldown<=0){
        WeaponInfo& wp=weaponStats[currentWeapon];
        fireCooldown=wp.fireRate;
        if(currentWeapon==WEAPON_LASER){
            double rx=px,ry=py,rdx=dirX,rdy=dirY;
            if(fabs(rdx)<1e-10)rdx=(rdx>=0)?1e-10:-1e-10;
            if(fabs(rdy)<1e-10)rdy=(rdy>=0)?1e-10:-1e-10;
            bool hitSth=false;
            for(int step=0;step<80&&!hitSth;step++){
                rx+=rdx*0.25;ry+=rdy*0.25;
                if(IsBlock(rx,ry)){laserEndX=rx;laserEndY=ry;hitSth=true;break;}
                for(int e=0;e<MAX_ENEMIES;e++){if(enemyHP[e]<=0)continue;
                    double edx=rx-sprites[e].x,edy=ry-sprites[e].y;
                    if(sqrt(edx*edx+edy*edy)<0.5){DamageEnemy(e,(float)wp.damage);
                        laserEndX=rx;laserEndY=ry;hitSth=true;break;}
                }
            }
            if(!hitSth){laserEndX=px+dirX*20;laserEndY=py+dirY*20;}
            laserBeamActive=true;laserTimer=0.08f;
        }else{
            for(int p=0;p<wp.pellets;p++){
                int slot=-1;for(int b=0;b<MAX_BULLETS;b++){if(!bullets[b].active){slot=b;break;}}if(slot==-1)break;
                float angle=((float)(rand()%1000)/1000.0f-0.5f)*2.0f*wp.spread;
                double bdx=dirX*cos(angle)-dirY*sin(angle),bdy=dirX*sin(angle)+dirY*cos(angle);
                float spd=wp.isRocket?ROCKET_SPEED:BULLET_SPEED;
                bullets[slot].x=px;bullets[slot].y=py;
                bullets[slot].dx=bdx*spd;bullets[slot].dy=bdy*spd;
                bullets[slot].damage=wp.damage;bullets[slot].life=3.0f;
                bullets[slot].isRocket=wp.isRocket;bullets[slot].blastR=wp.blastR;
                bullets[slot].active=true;
            }
        }
    }
}
void DamagePlayer(float amount){
    if(amount<=0.0f||isDead)return;
    playerHP-=amount;
    float strength=0.35f+amount/30.0f;
    if(strength>1.0f)strength=1.0f;
    if(strength>damageFlashStrength)damageFlashStrength=strength;
    damageFlashTimer=DAMAGE_FLASH_DURATION;
    if(playerHP<=0.0f){playerHP=0.0f;isDead=true;}
}
void DamageEnemy(int enemyIndex,float amount){
    if(enemyIndex<0||enemyIndex>=MAX_ENEMIES||amount<=0.0f||enemyHP[enemyIndex]<=0.0f)return;
    enemyHP[enemyIndex]-=amount;
    if(enemyHP[enemyIndex]>0.0f)return;
    enemyHP[enemyIndex]=0.0f;
    EnemyDeathEffect& effect=enemyDeathEffects[enemyIndex];
    effect.x=sprites[enemyIndex].x;effect.y=sprites[enemyIndex].y;
    effect.timer=ENEMY_DEATH_DURATION;effect.active=true;effect.boss=(enemyIndex==BOSS_INDEX);
}
void ExplodeRocket(const Bullet& bullet){
    for(int e=0;e<MAX_ENEMIES;e++){
        if(enemyHP[e]<=0)continue;
        double dx=bullet.x-sprites[e].x,dy=bullet.y-sprites[e].y;
        double distance=sqrt(dx*dx+dy*dy);
        if(distance<bullet.blastR){
            float damage=bullet.damage*(1.0f-(float)(distance/bullet.blastR));
            DamageEnemy(e,damage);
        }
    }
    double dx=bullet.x-px,dy=bullet.y-py;
    double playerDistance=sqrt(dx*dx+dy*dy);
    if(playerDistance<bullet.blastR){
        float damage=bullet.damage*0.4f*(1.0f-(float)(playerDistance/bullet.blastR));
        DamagePlayer(damage);
    }
    for(int i=0;i<MAX_EXPLOSIONS;i++)if(!explosions[i].active){
        explosions[i].x=bullet.x;explosions[i].y=bullet.y;
        explosions[i].timer=0.35f;explosions[i].active=true;break;
    }
}
void UpdateBullets(float dt){
    for(int i=0;i<MAX_BULLETS;i++){
        if(!bullets[i].active)continue;
        bullets[i].x+=bullets[i].dx*dt;bullets[i].y+=bullets[i].dy*dt;bullets[i].life-=dt;
        bool hitEnemy=false;
        for(int e=0;e<MAX_ENEMIES;e++){if(enemyHP[e]<=0)continue;
            double edx=bullets[i].x-sprites[e].x,edy=bullets[i].y-sprites[e].y;
            if(sqrt(edx*edx+edy*edy)<0.45){
                if(bullets[i].isRocket)ExplodeRocket(bullets[i]);
                else{
                    DamageEnemy(e,(float)bullets[i].damage);
                }
                bullets[i].active=false;hitEnemy=true;break;
            }
        }
        if(hitEnemy)continue;
        if(IsBlock(bullets[i].x,bullets[i].y)){
            if(bullets[i].isRocket)ExplodeRocket(bullets[i]);
            bullets[i].active=false;
        }
        if(bullets[i].life<=0)bullets[i].active=false;
    }
}
void UpdateEnemies(float dt){
    float aiSpd=1.75f*dt;
    for(int i=0;i<MAX_ENEMIES;i++){
        if(enemyHP[i]<=0)continue;
        double dx=px-sprites[i].x,dy=py-sprites[i].y,dist=sqrt(dx*dx+dy*dy);
        if(dist<DAMAGE_RANGE){DamagePlayer(enemyDamage[i]*dt);if(isDead)break;}
        if(dist<=0.25)continue;
        int eGX=(int)sprites[i].x,eGY=(int)sprites[i].y,pGX=(int)px,pGY=(int)py;
        double mvX,mvY;
        if(i<STATIC_ENEMIES){
            // BFS 寻路(仅大怪)
            bool needBFS=(eWPX[i]<0)||(eLastPX[i]!=pGX||eLastPY[i]!=pGY)||(eGX==eWPX[i]&&eGY==eWPY[i]);
            if(needBFS){
                bool visited[MAZE_H][MAZE_W]={};int prevX[MAZE_H][MAZE_W],prevY[MAZE_H][MAZE_W];
                int qx[MAZE_W*MAZE_H],qy[MAZE_W*MAZE_H];int head=0,tail=0;
                qx[tail]=eGX;qy[tail]=eGY;tail++;visited[eGY][eGX]=true;prevX[eGY][eGX]=-1;
                bool found=false;int ncx=eGX,ncy=eGY;
                const int dirs[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
                while(head<tail){
                    int cx2=qx[head],cy2=qy[head];head++;
                    if(cx2==pGX&&cy2==pGY){int tx=cx2,ty=cy2;
                        while(true){int px2=prevX[ty][tx],py2=prevY[ty][tx];if(px2==-1||(px2==eGX&&py2==eGY))break;tx=px2;ty=py2;}
                        ncx=tx;ncy=ty;found=true;break;}
                    for(int d=0;d<4;d++){int nx2=cx2+dirs[d][0],ny2=cy2+dirs[d][1];
                        if(nx2>=0&&nx2<MAZE_W&&ny2>=0&&ny2<MAZE_H&&!visited[ny2][nx2]&&maze[ny2][nx2]==0)
                        {visited[ny2][nx2]=true;prevX[ny2][nx2]=cx2;prevY[ny2][nx2]=cy2;qx[tail]=nx2;qy[tail]=ny2;tail++;}}
                }
                if(found){eWPX[i]=ncx;eWPY[i]=ncy;}else{eWPX[i]=pGX;eWPY[i]=pGY;}
                eLastPX[i]=pGX;eLastPY[i]=pGY;
            }
            double ttx=eWPX[i]+0.5,tty=eWPY[i]+0.5;
            double tdx=ttx-sprites[i].x,tdy=tty-sprites[i].y;
            double td=sqrt(tdx*tdx+tdy*tdy);if(td<0.05)td=0.05;
            mvX=(tdx/td)*aiSpd;mvY=(tdy/td)*aiSpd;
        }else{
            // 小怪:直接追踪,无 BFS
            mvX=(dx/dist)*aiSpd;mvY=(dy/dist)*aiSpd;
        }
        if(!IsBlock(sprites[i].x+mvX,sprites[i].y+mvY)){sprites[i].x+=mvX;sprites[i].y+=mvY;}
        else if(!IsBlock(sprites[i].x+mvX,sprites[i].y))sprites[i].x+=mvX;
        else if(!IsBlock(sprites[i].x,sprites[i].y+mvY))sprites[i].y+=mvY;
    }
    // 胜利:Boss 死亡
    if(enemyHP[BOSS_INDEX]<=0.0f&&!isDead)isWin=true;
}
void UpdatePickups(){
    if(!isDead&&!isWin)for(int i=0;i<HP_COUNT;i++){if(!hpActive[i])continue;
        double hx=px-sprites[MAX_ENEMIES+i].x,hy=py-sprites[MAX_ENEMIES+i].y;
        if(sqrt(hx*hx+hy*hy)<0.5){playerHP+=300;if(playerHP>playerMaxHP)playerHP=playerMaxHP;hpActive[i]=false;}
    }
    if(!isDead&&!isWin)for(int i=0;i<WEAPON_COUNT_PICKUP;i++){if(!weaponActive[i])continue;
        double wx2=px-sprites[MAX_ENEMIES+HP_COUNT+i].x,wy2=py-sprites[MAX_ENEMIES+HP_COUNT+i].y;
        if(sqrt(wx2*wx2+wy2*wy2)<0.5){
            WeaponType picked=weaponPickupType[i];weaponUnlocked[picked]=true;weaponHudPulse[picked]=0.8f;weaponActive[i]=false;
        }
    }
}
void SpawnMinion(){
    if(isDead||isWin)return;
    for(int i=STATIC_ENEMIES;i<MAX_ENEMIES;i++){
        if(enemyHP[i]<=0.0f){
            // 随机找离玩家至少5格的空地
            int tries=0;
            while(tries<50){
                int cx=1+rand()%10, cy=1+rand()%10;
                if(maze[cy][cx]==0){
                    double distToPlayer=sqrt((cx+0.5-px)*(cx+0.5-px)+(cy+0.5-py)*(cy+0.5-py));
                    if(distToPlayer>4.5){
                        sprites[i].x=cx+0.5;sprites[i].y=cy+0.5;
                        enemyHP[i]=MINION_HP;enemyMaxHPArr[i]=MINION_HP;enemyDamage[i]=MINION_DAMAGE;
                        eWPX[i]=eWPY[i]=-1;
                        return;
                    }
                }
                tries++;
            }
            return;
        }
    }
}
void Update(float dt){
    if(!gameStarted){
        if(keyState[VK_RETURN])ResetGame();
        return;
    }
    if(isDead||isWin){
        if(keyState['R']||keyState[VK_RETURN])ResetGame();
        return;
    }
    UpdatePlayerInput(dt);
    UpdateEffects(dt);
    UpdateJump(dt);
    UpdateWeapon(dt);
    UpdateBullets(dt);
    UpdateEnemies(dt);
    // 小怪刷怪计时器
    if(!isDead&&!isWin){
        spawnTimer-=dt;
        if(spawnTimer<=0.0f){spawnTimer=SPAWN_INTERVAL;SpawnMinion();}
    }
    UpdatePickups();
}