NppClone/src/level.cpp

172 lines
4.1 KiB
C++

#include "level.h"
#include "player.h"
#include <azur/azur.h>
#include <azur/gint/render.h>
#include <cstdint>
#include <stdlib.h>
#define TILESIZE 16
extern struct Map map_level1;
extern struct Map map_level2;
extern struct Map map_level3;
extern bool drawbackground;
struct Map *map_level;
int tileXmin, tileXmax;
int tileYmin, tileYmax;
int XinTile, YinTile;
extern bopti_image_t img_selected;
Level::Level( )
{
map_level = &map_level1;
}
Level::~Level( )
{
}
void Level::ChangeMap( int level, Player *MyPlayer )
{
if(level==1) map_level = &map_level1;
else if(level==2) map_level = &map_level2;
else if(level==3) map_level = &map_level3;
else map_level = &map_level1;
this->UpdateDataMap( MyPlayer );
}
void Level::UpdateDataMap( Player *MyPlayer )
{
for(int i=0; i<map_level->w; i++)
{
for(int j=0; j<map_level->h; j++)
{
uint16_t index = j * map_level->w + i;
uint16_t currentTile = map_level->layers[1][ index ];
if (currentTile==32)
{
MyPlayer->x = (float) (i*16+8);
MyPlayer->y = (float) (j*16);
MyPlayer->vx = 0.0f;
MyPlayer->vy = 0.0f;
MyPlayer->Update( 0.0f );
}
}
}
}
void Level::Render( void )
{
for(int u=!drawbackground; u<map_level->nblayers;u++)
{
for(int i=0; i<map_level->w; i++)
{
for(int j=0; j<map_level->h; j++)
{
uint16_t index = j * map_level->w + i;
uint16_t currentTile = map_level->layers[u][ index ];
if (currentTile!=0 && currentTile!=32)
{
uint16_t xtile = ((currentTile % map_level->tileset_size)-1) * 16;
uint16_t ytile = (currentTile / map_level->tileset_size) * 16;
azrp_subimage_p8( i*16, j*16, map_level->tileset, xtile, ytile, 16, 16, DIMAGE_NONE );
// TODO :
// the last column of tile is not fully drawn cause 4 pixels are missing
// to upgrade the
}
}
}
}
}
void Level::RenderSelected( void )
{
for(int i=tileXmin; i<=tileXmax; i++)
{
for(int j=tileYmin; j<=tileYmax; j++)
{
azrp_image_p8( i*16, j*16, &img_selected, DIMAGE_NONE );
}
}
}
void Level::Update( float dt )
{
}
int Level::GetTileBackground( uint8_t x, uint8_t y )
{
uint8_t tileX = x / 16;
uint8_t tileY = y / 16;
uint16_t index = tileY * map_level->w + tileX % map_level->w;
uint16_t currentTile = map_level->layers[0][ index ];
return currentTile;
}
int Level::GetTileForeground( uint8_t x, uint8_t y )
{
uint8_t tileX = x / 16;
uint8_t tileY = y / 16;
uint16_t index = tileY * map_level->w + tileX;
uint16_t currentTile = map_level->layers[1][ index ];
return currentTile;
}
bool Level::CanGo( Player *MyPlayer )
{
int tileX, tileY;
int tileIndex, tileValue;
int xmin = (int) MyPlayer->nextx - 8;
int ymin = (int) MyPlayer->nexty - 8;
int xmax = (int) MyPlayer->nextx + 8;
int ymax = (int) MyPlayer->nexty + 8;
/*
tileXmin = xmin / 16;
tileXmax = xmax / 16;
tileYmin = ymin / 16;
tileYmax = ymax / 16;
XinTile = xmin % 16;
YinTile = ymin % 16;
*/
if ((MyPlayer->action == RUN || MyPlayer->action == WALK) && MyPlayer->direction == LEFT)
{
if( GetTileBackground( xmin, ymin ) != 0 || GetTileBackground( xmin, ymax ) != 0) return false;
}
else if ((MyPlayer->action == RUN || MyPlayer->action == WALK) && MyPlayer->direction == RIGHT)
{
if( GetTileBackground( xmax, ymax ) != 0 || GetTileBackground( xmin, ymax ) != 0) return false;
}
else if (MyPlayer->action == JUMP)
{
if( GetTileBackground( xmin, ymin ) != 0 || GetTileBackground( xmax, ymin ) != 0) return false;
}
else if (MyPlayer->action == FALL)
{
if( GetTileBackground( xmin, ymax ) != 0 || GetTileBackground( xmax, ymax ) != 0) return false;
}
return true;
}