generated from sigonasr2/CPlusPlusProjectTemplate
Basic tile map editor
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
1a2bdd1895
commit
bf437e0317
Binary file not shown.
78
main.cpp
78
main.cpp
@ -6,10 +6,15 @@
|
|||||||
#include "soundwaveEngine.h"
|
#include "soundwaveEngine.h"
|
||||||
#include "tiles.h"
|
#include "tiles.h"
|
||||||
#include "references.h"
|
#include "references.h"
|
||||||
|
#include "states.h"
|
||||||
|
|
||||||
#define WIDTH 256
|
#define WIDTH 256
|
||||||
#define HEIGHT 224
|
#define HEIGHT 224
|
||||||
#define CAMERA_MOVESPD 0.5
|
#define CAMERA_MOVESPD 5
|
||||||
|
#define TILEMAP_SIZE_X 512
|
||||||
|
#define TILEMAP_SIZE_Y 512
|
||||||
|
#define TILEMAP_EDITOR_DRAW_MULT 0.4375
|
||||||
|
#define TILEMAP_EDITOR_TILESIZE (32*TILEMAP_EDITOR_DRAW_MULT)
|
||||||
|
|
||||||
using namespace olc;
|
using namespace olc;
|
||||||
|
|
||||||
@ -74,6 +79,9 @@ public:
|
|||||||
Map*CURRENT_MAP;
|
Map*CURRENT_MAP;
|
||||||
Map*MAP_ONETT;
|
Map*MAP_ONETT;
|
||||||
vd2d cameraPos = {0,0};
|
vd2d cameraPos = {0,0};
|
||||||
|
state::GameState GAME_STATE = state::EDITOR;
|
||||||
|
vi2d SELECTED_TILE={0,0};
|
||||||
|
vi2d HIGHLIGHTED_TILE={0,0};
|
||||||
|
|
||||||
bool MOUSE_PRESSED_DOWN=false,MOUSE_DOWN=false,MOUSE_RELEASED=false; //TODO Implement Mouse things.
|
bool MOUSE_PRESSED_DOWN=false,MOUSE_DOWN=false,MOUSE_RELEASED=false; //TODO Implement Mouse things.
|
||||||
|
|
||||||
@ -108,6 +116,7 @@ public:
|
|||||||
elapsedTime-=TARGET_RATE;
|
elapsedTime-=TARGET_RATE;
|
||||||
updateGame();
|
updateGame();
|
||||||
}
|
}
|
||||||
|
keyUpdates();
|
||||||
drawGame();
|
drawGame();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -121,6 +130,23 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (GAME_STATE) {
|
||||||
|
case state::TILE_SELECT:{
|
||||||
|
if (!TabHeld()) {
|
||||||
|
GAME_STATE=state::EDITOR;
|
||||||
|
}
|
||||||
|
if (GetMouse(0).bHeld) {
|
||||||
|
int selectedTileX=GetMouseX()/TILEMAP_EDITOR_TILESIZE;
|
||||||
|
int selectedTileY=GetMouseY()/TILEMAP_EDITOR_TILESIZE;
|
||||||
|
if (selectedTileX*TILEMAP_EDITOR_DRAW_MULT>=0&&selectedTileX*TILEMAP_EDITOR_DRAW_MULT<TILEMAP_SIZE_X&&selectedTileY*TILEMAP_EDITOR_DRAW_MULT<TILEMAP_SIZE_Y&&selectedTileY*TILEMAP_EDITOR_DRAW_MULT>=0) {
|
||||||
|
SELECTED_TILE={selectedTileX,selectedTileY};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
case state::EDITOR:{
|
||||||
|
if (TabHeld()) {
|
||||||
|
GAME_STATE=state::TILE_SELECT;
|
||||||
|
}
|
||||||
if (UpHeld()) {
|
if (UpHeld()) {
|
||||||
cameraPos.y-=CAMERA_MOVESPD;
|
cameraPos.y-=CAMERA_MOVESPD;
|
||||||
}
|
}
|
||||||
@ -133,9 +159,48 @@ public:
|
|||||||
if (DownHeld()) {
|
if (DownHeld()) {
|
||||||
cameraPos.y+=CAMERA_MOVESPD;
|
cameraPos.y+=CAMERA_MOVESPD;
|
||||||
}
|
}
|
||||||
|
int selectedTileX=(GetMouseX()+cameraPos.x)/32;
|
||||||
|
int selectedTileY=(GetMouseY()+cameraPos.y)/32;
|
||||||
|
if (selectedTileX<MAP_WIDTH&&selectedTileY<MAP_HEIGHT&&selectedTileX>=0&&selectedTileY>=0) {
|
||||||
|
HIGHLIGHTED_TILE={selectedTileX,selectedTileY};
|
||||||
|
}
|
||||||
|
if (GetMouse(0).bHeld) {
|
||||||
|
TILE*tile=MAP[HIGHLIGHTED_TILE.y][HIGHLIGHTED_TILE.x];
|
||||||
|
tile->tileX=SELECTED_TILE.x;
|
||||||
|
tile->tileY=SELECTED_TILE.y;
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void keyUpdates() {
|
||||||
|
switch (GAME_STATE) {
|
||||||
|
case state::TILE_SELECT:{
|
||||||
|
if (UpPressed()) {
|
||||||
|
SELECTED_TILE.y=SELECTED_TILE.y-1;
|
||||||
|
if (SELECTED_TILE.y<0) {
|
||||||
|
SELECTED_TILE.y=TILEMAP_SIZE_Y/32-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (RightPressed()) {
|
||||||
|
SELECTED_TILE.x=(SELECTED_TILE.x+1)%(TILEMAP_SIZE_X/32);
|
||||||
|
}
|
||||||
|
if (LeftPressed()) {
|
||||||
|
SELECTED_TILE.x=SELECTED_TILE.x-1;
|
||||||
|
if (SELECTED_TILE.x<0) {
|
||||||
|
SELECTED_TILE.x=TILEMAP_SIZE_X/32-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (DownPressed()) {
|
||||||
|
SELECTED_TILE.y=(SELECTED_TILE.y+1)%(TILEMAP_SIZE_Y/32);
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawGame(){
|
void drawGame(){
|
||||||
|
switch (GAME_STATE) {
|
||||||
|
case state::EDITOR:{
|
||||||
for (auto&obj:OBJECTS) {
|
for (auto&obj:OBJECTS) {
|
||||||
DrawPartialDecal(obj->pos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->scale,obj->color);
|
DrawPartialDecal(obj->pos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->scale,obj->color);
|
||||||
}
|
}
|
||||||
@ -148,6 +213,14 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DrawRectDecal((HIGHLIGHTED_TILE)*32-cameraPos,{32,32},YELLOW);
|
||||||
|
}break;
|
||||||
|
case state::TILE_SELECT:{
|
||||||
|
//14x14 pixels per tile.
|
||||||
|
DrawDecal({0,0},SPRITES[TILESET1],{TILEMAP_EDITOR_DRAW_MULT,TILEMAP_EDITOR_DRAW_MULT});
|
||||||
|
DrawRectDecal(SELECTED_TILE*(TILEMAP_EDITOR_TILESIZE),{TILEMAP_EDITOR_TILESIZE,TILEMAP_EDITOR_TILESIZE},RED);
|
||||||
|
}break;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void LoadMap(Map*map) {
|
void LoadMap(Map*map) {
|
||||||
@ -221,6 +294,9 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool TabHeld(){
|
||||||
|
return GetKey(TAB).bHeld;
|
||||||
|
}
|
||||||
bool UpPressed(){
|
bool UpPressed(){
|
||||||
return GetKey(W).bPressed||GetKey(UP).bPressed||GetKey(NP8).bPressed||MOUSE_PRESSED_DOWN&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128;
|
return GetKey(W).bPressed||GetKey(UP).bPressed||GetKey(NP8).bPressed||MOUSE_PRESSED_DOWN&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128;
|
||||||
}
|
}
|
||||||
|
@ -1086,6 +1086,7 @@ namespace olc
|
|||||||
|
|
||||||
// Draws a line in Decal Space
|
// Draws a line in Decal Space
|
||||||
void DrawLineDecal(const olc::vf2d& pos1, const olc::vf2d& pos2, Pixel p = olc::WHITE);
|
void DrawLineDecal(const olc::vf2d& pos1, const olc::vf2d& pos2, Pixel p = olc::WHITE);
|
||||||
|
void DrawRectDecal(const olc::vf2d& pos1, const olc::vf2d& pos2, Pixel p = olc::WHITE);
|
||||||
void DrawRotatedStringDecal(const olc::vf2d& pos, const std::string& sText, const float fAngle, const olc::vf2d& center = { 0.0f, 0.0f }, const olc::Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f });
|
void DrawRotatedStringDecal(const olc::vf2d& pos, const std::string& sText, const float fAngle, const olc::vf2d& center = { 0.0f, 0.0f }, const olc::Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f });
|
||||||
void DrawRotatedStringPropDecal(const olc::vf2d& pos, const std::string& sText, const float fAngle, const olc::vf2d& center = { 0.0f, 0.0f }, const olc::Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f });
|
void DrawRotatedStringPropDecal(const olc::vf2d& pos, const std::string& sText, const float fAngle, const olc::vf2d& center = { 0.0f, 0.0f }, const olc::Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f });
|
||||||
// Clears entire draw target to Pixel
|
// Clears entire draw target to Pixel
|
||||||
@ -2686,6 +2687,13 @@ namespace olc
|
|||||||
vLayers[nTargetLayer].vecDecalInstance.push_back(di);
|
vLayers[nTargetLayer].vecDecalInstance.push_back(di);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PixelGameEngine::DrawRectDecal(const olc::vf2d& pos, const olc::vf2d& size, const olc::Pixel col) {
|
||||||
|
DrawLineDecal(pos,{pos.x+size.x,pos.y},col);
|
||||||
|
DrawLineDecal({pos.x,pos.y},{pos.x,pos.y+size.y},col);
|
||||||
|
DrawLineDecal({pos.x,pos.y+size.y},{pos.x+size.x,pos.y+size.y},col);
|
||||||
|
DrawLineDecal({pos.x+size.x,pos.y},{pos.x+size.x,pos.y+size.y},col);
|
||||||
|
}
|
||||||
|
|
||||||
void PixelGameEngine::FillRectDecal(const olc::vf2d& pos, const olc::vf2d& size, const olc::Pixel col)
|
void PixelGameEngine::FillRectDecal(const olc::vf2d& pos, const olc::vf2d& size, const olc::Pixel col)
|
||||||
{
|
{
|
||||||
olc::vf2d vNewSize = (size - olc::vf2d(0.375f, 0.375f)).ceil();
|
olc::vf2d vNewSize = (size - olc::vf2d(0.375f, 0.375f)).ceil();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user