NppClone/src/level.cpp

176 lines
4.9 KiB
C++

#include "level.h"
#include "player.h"
#include <azur/azur.h>
#include <azur/gint/render.h>
#include "utilities.h"
#include <cstdint>
#include <stdlib.h>
#define TILESIZE 16
extern struct Map map_level0;
extern struct Map map_level1;
extern struct Map map_level2;
extern struct Map map_level3;
extern struct Map map_level4;
extern bool drawbackground;
extern bool textbacktile;
extern bool textforetile;
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==0) map_level = &map_level0;
else if(level==1) map_level = &map_level1;
else if(level==2) map_level = &map_level2;
else if(level==3) map_level = &map_level3;
else if(level==4) map_level = &map_level4;
else map_level = &map_level0;
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==31)
{
MyPlayer->currx = (float) (i) + 0.5f;
MyPlayer->curry = (float) (j) + 0.5f;
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!=-1)
{
uint16_t xtile = (currentTile % map_level->tileset_size) * 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 );
if (textbacktile) Azur_draw_text( i*16, j*16, "%d", GetTileBackgroundINT( i, j ) );
if (textforetile) Azur_draw_text( i*16+8, j*16+8, "%d", GetTileForegroundINT( i, j ) );
}
}
}
}
}
void Level::RenderSelected( uint8_t i, uint8_t j )
{
azrp_image_p8( i*16, j*16, &img_selected, DIMAGE_NONE );
Azur_draw_text( i*16+1, j*16+1, "B=%d", GetTileBackgroundINT( i, j ) );
Azur_draw_text( i*16+1, j*16+1, "F=%d", GetTileForegroundINT( i, j ) );
}
void Level::Update( float dt )
{
}
/*RETURN the type of tile located in the background for the point x, y using player coordinates (x=[0..25], y=[0..14]) */
/*the x and y correspond to hte integer part of MyPlayer.x and MyPlayer.y*/
int Level::GetTileBackgroundINT( uint8_t x, uint8_t y )
{
uint16_t index = y * map_level->w + x;
uint16_t currentTile = map_level->layers[0][ index ];
return currentTile;
}
/*RETURN the type of tile located in the foreground for the point x, y using player coordinates (x=[0..25], y=[0..14]) */
/*the x and y correspond to hte integer part of MyPlayer.x and MyPlayer.y*/
int Level::GetTileForegroundINT( uint8_t x, uint8_t y )
{
uint16_t index = y * map_level->w + x;
uint16_t currentTile = map_level->layers[1][ index ];
return currentTile;
}
/*RETURN the type of tile located in the background for the point x, y using screen coordinates (x=[0..396], y=[0..223]) */
int Level::GetTileBackground( uint16_t x, uint16_t y )
{
uint8_t tileX = x >> 4;
uint8_t tileY = y >> 4;
uint16_t index = tileY * map_level->w + tileX;
uint16_t currentTile = map_level->layers[0][ index ];
return currentTile;
}
/*RETURN the type of tile located in the foreground for the point x, y using screen coordinates (x=[0..396], y=[0..223]) */
int Level::GetTileForeground( uint16_t x, uint16_t y )
{
uint8_t tileX = x >> 4;
uint8_t tileY = y >> 4 ;
uint16_t index = tileY * map_level->w + tileX;
uint16_t currentTile = map_level->layers[1][ index ];
return currentTile;
}
/*RETURN true if the player can go in the target position*/
bool Level::CanGo( Player *MyPlayer )
{
uint16_t targetTile = this->GetTileBackgroundINT( (int) MyPlayer->nextx, (int) MyPlayer->nexty );
if (targetTile!=0) return false;
return true;
}
/*RETURN true if the player is above a solid tile*/
/*TO DO : TO BE IMPROVED, THIS IS REALLY DIRTY !!!!*/
bool Level::IsOnGround( Player *MyPlayer )
{
if (this->GetTileBackgroundINT( (uint8_t) MyPlayer->currx, (uint8_t) MyPlayer->curry +1 ) !=0)
{
return true;
}
return false;
}