TileMap.hpp
上传用户:zhj2929
上传日期:2022-07-23
资源大小:28772k
文件大小:8k
- #ifndef _TILEMAP_H_
- #define _TILEMAP_H_
- #include "../../JGE/include/JGE.h"
- #define TILE_WIDTH 32
- #define TILE_HEIGHT 32
- #define TILE_SHIFT 5
- #define TILE_SOLID 1
- #define TILE_MONEY 2
- #define TILE_POWERUP 3
- #define TILE_TUNNEL 4
- typedef JQuad* JQUAD_PTR;
- class TileMap
- {
- private:
- JGE* mEngine;
- GameStatePlay* mApp;
- JTexture* mTileTex;
-
- short* mTilemap;
- u8* mTilemapCollision;
- JQUAD_PTR* mTiles;
-
- int mTileWidth;
- int mTileHeight;
-
- int mCols;
- int mRows;
- int mColsPerScreen;
- int mRowsPerScreen;
- float mMapX;
- float mMapY;
- float mTargetX;
- float mTargetY;
- bool banana;
- float mAnimateCounter;
- public:
-
- TileMap(GameStatePlay* app, JTexture* tex);
- ~TileMap();
- void Load(const char* mapDat, const char* mapCollision);
- void SetTarget(float x, float y);
- void Update(float dt);
- void Render(float x, float y);
- bool BBoxCollide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
- int CheckHoriz(int x1, int x2, int y, bool fromLeft, int* col, int* row);
- int CheckVert(int x, int y1, int y2, int* col, int* row);
- short GetTile(int col, int row);
- u8 GetTileInfo(int col, int row);
- void SetTile(int col, int row, short tile);
- void SetTileInfo(int col, int row, u8 n);
- void GetPosition(float* x, float* y) { *x = mMapX; *y = mMapY; }
- bool GetBanana(){ return banana;}
- };
- TileMap::TileMap(GameStatePlay* app, JTexture* tex): mApp(app)
- {
- mEngine = mApp->GetJGE();
- mTileTex = tex;
-
- mTilemap = NULL;
- mTilemapCollision = NULL;
- int col = tex->mWidth/TILE_WIDTH;
- int row = tex->mHeight/TILE_HEIGHT;
- mTiles = new JQUAD_PTR[col*row];
-
- float x = 0.0f;
- float y = 0.0f;
- int n = 0;
- for (int i=0;i<row;i++)
- {
- x = 0.0f;
- for (int j=0;j<col;j++)
- {
- mTiles[n++] = new JQuad(tex, x, y, TILE_WIDTH, TILE_HEIGHT);
- x += TILE_WIDTH;
- }
- y += TILE_HEIGHT;
- }
- mMapX = 0.0f;
- mMapY = VIRTUAL_HEIGHT - SCREEN_HEIGHT_F;
-
- mAnimateCounter = 0;
- }
- TileMap::~TileMap()
- {
- if (mTilemap)
- delete mTilemap;
- if (mTilemapCollision != NULL)
- delete mTilemapCollision;
- for (int i=0;i<32;i++)
- delete mTiles[i];
- delete[] mTiles;
- }
- void TileMap::Load(const char* mapDat, const char* mapCollision)
- {
- if (mTilemap != NULL)
- delete mTilemap;
- if (mTilemapCollision != NULL)
- delete mTilemapCollision;
- FILE* f = fopen(mapDat, "rb");
- if (f != NULL)
- {
- short cols;
- short rows;
- fread(&cols, 1, sizeof(short), f);
- fread(&rows, 1, sizeof(short), f);
- mCols = (int)cols;
- mRows = (int)rows;
- mTilemap = new short[cols*rows];
- fread(mTilemap, cols*rows, sizeof(short), f);
- fclose(f);
- }
- f = fopen(mapCollision, "rb");
- if (f != NULL)
- {
- short cols;
- short rows;
- fread(&cols, 1, sizeof(short), f);
- fread(&rows, 1, sizeof(short), f);
- mTilemapCollision = new u8[cols*rows];
- fread(mTilemapCollision, cols*rows, sizeof(u8), f);
- fclose(f);
- }
- }
- void TileMap::Render(float x, float y)
- {
- int mapX = (int) mMapX;
- int mapY = (int) mMapY;
- int col = mapX/TILE_WIDTH;
- float xoffset = -(float)(mapX%TILE_WIDTH);
- int row = mapY/TILE_HEIGHT;
- float yy = -(float)(mapY%TILE_HEIGHT);
- int start = mCols * row + col;
-
- float xx=0;
- int currCol;
- int n, index;
- while (yy < SCREEN_HEIGHT_F && row < mRows)
- {
- xx = xoffset;
- currCol = col;
- index = start;
- while (xx < SCREEN_WIDTH_F && currCol< mCols)
- {
- n = mTilemap[index++];
- if (n > 0)
- {
- mEngine->RenderQuad(mTiles[n], xx, yy);
- }
- xx += TILE_WIDTH;
- currCol++;
-
- }
- yy += TILE_HEIGHT;
- start += mCols;
- row++;
- }
- }
- bool BBoxCollide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
- {
- if (x1 - x2 < -w1) return false;
- if (x1 - x2 > w2) return false;
- if (y1 - y2 < -h1) return false;
- if (y1 - y2 > h2) return false;
-
- return true; // collision!!!
- }
- int TileMap::CheckHoriz(int x1, int x2, int y, bool fromLeft, int* col, int* row)
- {
- int tile;
-
- int colStart;
- int colEnd;
- int incr;
- if (fromLeft)
- {
- colStart = x1>>TILE_SHIFT;
- colEnd = x2>>TILE_SHIFT;
- incr = 1;
- }
- else
- {
- colStart = x2>>TILE_SHIFT;
- colEnd = x1>>TILE_SHIFT;
- incr = -1;
- }
- *row = y>>TILE_SHIFT;
- *col = colStart;
- for (int currCol=colStart;; currCol+=incr)
- {
- if (currCol >= 0 && currCol < mCols && *row >= 0 && *row <= mRows)
- {
- tile = mTilemapCollision[(*row) * mCols + currCol];
- if (tile != 0)
- {
- *col = currCol;
- return tile;
- }
- }
- if (currCol==colEnd) break;
- }
- return 0;
- }
- int TileMap::CheckVert(int x, int y1, int y2, int* col, int* row)
- {
- int tile;
- int rowStart = y1>>TILE_SHIFT;
- int rowEnd = y2>>TILE_SHIFT;
-
- *col = x>>TILE_SHIFT;
- *row = rowStart;
- for (int currRow = rowStart;currRow<=rowEnd;currRow++)
- {
- if (*col >= 0 && *col < mCols && currRow >= 0 && currRow <= mRows)
- {
- tile = mTilemapCollision[currRow * mCols + (*col)];
- if (tile != 0)
- {
- *row = currRow;
- return tile;
- }
- }
- }
-
- return 0;
- }
- short TileMap::GetTile(int col, int row)
- {
- return mTilemap[row*mCols + col];
- }
- u8 TileMap::GetTileInfo(int col, int row)
- {
- return mTilemapCollision[row*mCols + col];
- }
- void TileMap::SetTile(int col, int row, short tile)
- {
- mTilemap[row*mCols + col] = tile;
- }
- void TileMap::SetTileInfo(int col, int row, u8 n)
- {
- mTilemapCollision[row*mCols + col] = n;
- }
- void TileMap::SetTarget(float x, float y)
- {
- mTargetX = x;
- mTargetY = y;
-
- if (mTargetX < 0.0f)
- mTargetX = 0.0f;
- if (mTargetX + SCREEN_WIDTH_F > VIRTUAL_WIDTH)
- mTargetX = VIRTUAL_WIDTH - SCREEN_WIDTH_F;
- if (mTargetY < 0.0f)
- mTargetY = 0.0f;
- if (mTargetY + SCREEN_HEIGHT_F > VIRTUAL_HEIGHT)
- mTargetY = VIRTUAL_HEIGHT - SCREEN_HEIGHT_F;
- }
- void TileMap::Update(float dt)
- {
-
- float speed = DEFAULT_WALK_SPEED;
- if (mMapX < mTargetX)
- {
- mMapX += speed*dt;
- if (mMapX > mTargetX)
- mMapX = mTargetX;
- }
- else if (mMapX > mTargetX)
- {
- mMapX -= speed*dt;
- if (mMapX < mTargetX)
- mMapX = mTargetX;
- }
- speed = 0.2f;
- if (mMapY < mTargetY)
- {
- mMapY += speed*dt;
- if (mMapY > mTargetY)
- mMapY = mTargetY;
- }
- else if (mMapY > mTargetY)
- {
- mMapY -= speed*dt;
- if (mMapY < mTargetY)
- mMapY = mTargetY;
- }
- mAnimateCounter += dt;
- if (mAnimateCounter > 300.0f)
- {
- mAnimateCounter = 0.0f;
- int col = (((int)mMapX)>>TILE_SHIFT)-1;
- int row = (((int)mMapY)>>TILE_SHIFT)-1;
- if (col<0) col = 0;
- if (row<0) row = 0;
- short tile;
- u8 tileInfo;
- int currCol, currRow;
- for (int i=0;i<16+2;i++)
- {
- for (int j=0;j<16+2;j++)
- {
- currCol = col+j;
- currRow = row+i;
- if (currCol >=0 && currCol < mCols && currRow >= 0 && currRow < mRows)
- {
-
- tile = GetTile(currCol, currRow);
- if (tile == 1)
- SetTile(currCol, currRow, 31);
- else if (tile == 31)
- SetTile(currCol, currRow, 1);
- tileInfo = GetTileInfo(currCol, currRow);
-
- if (tileInfo == BLOCK_TURTLE)
- {
- SetTileInfo(currCol, currRow, 0);
- mApp->SpawnTurtle(currCol, currRow);
- }
- /* else if (tileInfo == BLOCK_FLOWER)
- {
- SetTileInfo(currCol, currRow, 0);
- }*/
- else if (tileInfo == BLOCK_WEASEL)
- {
- SetTileInfo(currCol, currRow, 0);
- mApp->SpawnWeasel(currCol, currRow);
- }
- else if (tileInfo == BLOCK_SNOWMAN)
- {
- SetTileInfo(currCol, currRow, 0);
- mApp->SpawnSnowman(currCol, currRow);
- }
- else if (tileInfo == BLOCK_MONSTER)
- {
- SetTileInfo(currCol, currRow, 0);
- mApp->SpawnMonster(currCol, currRow);
- }
-
- }
- }
- }
- }
- }
- #endif