Basic foreground layer hiding code implementation.

pull/28/head
sigonasr2 2 years ago
parent b2eda563f2
commit 5ffcf3126a
  1. 29
      Crawler/Crawler.cpp
  2. 6
      Crawler/Crawler.h
  3. 13
      Crawler/Crawler.tiled-project
  4. 13
      Crawler/Map.h
  5. 4
      Crawler/TSXParser.h
  6. 2
      Crawler/Version.h
  7. BIN
      Crawler/assets/maps/Level2.png
  8. 71
      Crawler/assets/maps/grass_tiles_24x24.tsx

@ -92,7 +92,7 @@ bool Crawler::OnUserCreate(){
player.SetClass(WARRIOR);
player.SetPos({4*24,4*24});
LoadLevel(LEVEL2);
LoadLevel(CAMPAIGN_1_1);
return true;
}
@ -609,6 +609,7 @@ void Crawler::HurtEnemies(vf2d pos,float radius,int damage){
void Crawler::RenderWorld(float fElapsedTime){
Clear({100,180,100});
std::vector<TileRenderData>foregroundTiles;
for (int x = view.GetTopLeftTile().x/24-1; x <= view.GetBottomRightTile().x/24; x++){
for (int y = view.GetTopLeftTile().y/24-1; y <= view.GetBottomRightTile().y/24; y++){
if(x>=0&&x<WORLD_SIZE.x&&y>=0&&y<WORLD_SIZE.y){
@ -616,12 +617,16 @@ void Crawler::RenderWorld(float fElapsedTime){
int tileID=layer.tiles[y][x]-1;
if(tileID!=-1){
TilesheetData tileSheet=GetTileSheet(currentLevel,tileID);
int tileSheetWidth=tileSheet.tileset.Sprite()->width/24;
int tileSheetHeight=tileSheet.tileset.Sprite()->height/24;
int tileSheetWidth=tileSheet.tileset.tileset->Sprite()->width/24;
int tileSheetHeight=tileSheet.tileset.tileset->Sprite()->height/24;
int tileSheetIndex=tileID-(tileSheet.firstgid-1);
int tileSheetX=tileSheetIndex%tileSheetWidth;
int tileSheetY=tileSheetIndex/tileSheetWidth;
view.DrawPartialDecal(vi2d{x,y}*24,{24,24},tileSheet.tileset.Decal(),vi2d{tileSheetX,tileSheetY}*24,{24,24});
if(IsForegroundTile(tileSheet,tileSheetIndex)){
foregroundTiles.push_back({tileSheet.tileset.tileset->Decal(),vi2d{x,y}*24,vi2d{tileSheetX,tileSheetY}*24});
} else {
view.DrawPartialDecal(vi2d{x,y}*24,{24,24},tileSheet.tileset.tileset->Decal(),vi2d{tileSheetX,tileSheetY}*24,{24,24});
}
}
}
}
@ -660,6 +665,9 @@ void Crawler::RenderWorld(float fElapsedTime){
for(Bullet&b:PLAYER_BULLET_LIST){
b.Draw();
}
for(TileRenderData&foregroundTile:foregroundTiles){
view.DrawPartialDecal(foregroundTile.pos,{24,24},foregroundTile.tileset,foregroundTile.tileSheetPos,{24,24},{255,255,255,uint8_t(std::clamp(geom2d::line<float>(foregroundTile.pos+vi2d{12,12},player.GetPos()).length()*5,0.f,255.f))});
}
for(std::vector<DamageNumber>::iterator it=DAMAGENUMBER_LIST.begin();it!=DAMAGENUMBER_LIST.end();++it){
DamageNumber&dn=*it;
dn.lifeTime+=fElapsedTime;
@ -781,7 +789,8 @@ void Crawler::InitializeLevel(std::string mapFile,MapName map){
if(MAP_TILESETS.find("assets/maps/"+baseSourceDir)==MAP_TILESETS.end()){
TSXParser tileset(baseDir+tag.data["source"]);
Renderable*r=new Renderable();
MAP_TILESETS["assets/maps/"+baseSourceDir]=r;
MAP_TILESETS["assets/maps/"+baseSourceDir].tileset=r;
MAP_TILESETS["assets/maps/"+baseSourceDir].foregroundTiles=tileset.GetData().ForegroundTileData;
r->Load("assets/maps/"+tileset.GetData().ImageData.data["source"]);
}
}
@ -808,23 +817,27 @@ vi2d Crawler::GetWorldSize(){
return WORLD_SIZE;
}
bool Crawler::IsForegroundTile(TilesheetData sheet,int tileID){
return sheet.tileset.foregroundTiles.find(tileID)!=sheet.tileset.foregroundTiles.end();
}
TilesheetData Crawler::GetTileSheet(MapName map,int tileID){
std::vector<XMLTag>&tileData=MAP_DATA[map].TilesetData;
if(tileData.size()==1){
size_t slashMarkerSourceDir = tileData[0].data["source"].find_last_of('/');
std::string baseSourceDir=tileData[0].data["source"].substr(slashMarkerSourceDir+1);
return {*MAP_TILESETS["assets/maps/"+baseSourceDir],1};
return {MAP_TILESETS["assets/maps/"+baseSourceDir],1};
} else {
for (int i=1;i<tileData.size();i++){
if(tileID<stoi(tileData[i].data["firstgid"])){
size_t slashMarkerSourceDir = tileData[i-1].data["source"].find_last_of('/');
std::string baseSourceDir=tileData[i-1].data["source"].substr(slashMarkerSourceDir+1);
return {*MAP_TILESETS["assets/maps/"+baseSourceDir],stoi(tileData[i-1].data["firstgid"])};
return {MAP_TILESETS["assets/maps/"+baseSourceDir],stoi(tileData[i-1].data["firstgid"])};
}
}
size_t slashMarkerSourceDir = tileData[tileData.size()-1].data["source"].find_last_of('/');
std::string baseSourceDir=tileData[tileData.size()-1].data["source"].substr(slashMarkerSourceDir+1);
return {*MAP_TILESETS["assets/maps/"+baseSourceDir],stoi(tileData[tileData.size()-1].data["firstgid"])};
return {MAP_TILESETS["assets/maps/"+baseSourceDir],stoi(tileData[tileData.size()-1].data["firstgid"])};
}
}

@ -11,7 +11,7 @@
#include "TMXParser.h"
struct TilesheetData{
Renderable&tileset;
TilesetData&tileset;
int firstgid;
};
@ -25,7 +25,7 @@ class Crawler : public olc::PixelGameEngine
GFX_Battlecry_Effect,GFX_Mana,GFX_SonicSlash;
std::vector<Effect>foregroundEffects,backgroundEffects;
std::map<MapName,Map>MAP_DATA;
std::map<std::string,Renderable*>MAP_TILESETS;
std::map<std::string,TilesetData>MAP_TILESETS;
vf2d worldShake={};
float worldShakeTime=0;
float lastWorldShakeAdjust=0;
@ -67,5 +67,7 @@ public:
void SetupWorldShake(float duration);
vi2d GetWorldSize();
//tileID is the tile number from the tilesets.
bool IsForegroundTile(TilesheetData sheet,int tileID);
//tileID is the tile number from the tilesets.
TilesheetData GetTileSheet(MapName map,int tileID);
};

@ -36,6 +36,19 @@
"object"
]
},
{
"color": "#ffd9d929",
"drawFill": true,
"id": 7,
"members": [
],
"name": "ForegroundTile",
"type": "class",
"useAs": [
"property",
"tile"
]
},
{
"color": "#ffaa0000",
"drawFill": false,

@ -1,6 +1,19 @@
#pragma once
#include "TSXParser.h"
enum MapName{
LEVEL1,
LEVEL2,
CAMPAIGN_1_1,
};
struct TilesetData{
Renderable*tileset;
std::map<int,XMLTag>foregroundTiles;
};
struct TileRenderData{
Decal*tileset;
vi2d pos;
vi2d tileSheetPos;
};

@ -7,6 +7,7 @@ using namespace olc;
struct Tileset{
XMLTag ImageData;
std::map<int,XMLTag> ForegroundTileData;
friend std::ostream& operator << (std::ostream& os, Tileset& rhs);
};
@ -78,6 +79,9 @@ class TSXParser{
if (newTag.tag=="image") {
parsedTilesetInfo.ImageData=newTag;
} else
if (newTag.tag=="tile"&&newTag.data["type"]=="ForegroundTile"){
parsedTilesetInfo.ForegroundTileData[stoi(newTag.data["id"])]=newTag;
}
std::cout<<"\n"<<"=============\n";
}

@ -2,7 +2,7 @@
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 0
#define VERSION_BUILD 193
#define VERSION_BUILD 204
#define stringify(a) stringify_(a)
#define stringify_(a) #a

Binary file not shown.

After

Width:  |  Height:  |  Size: 560 KiB

@ -1,4 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.1" name="grass_tiles_24x24" tilewidth="24" tileheight="24" tilecount="783" columns="29">
<image source="grass_tiles_24x24.png" width="696" height="648"/>
<tile id="164" type="ForegroundTile"/>
<tile id="165" type="ForegroundTile"/>
<tile id="166" type="ForegroundTile"/>
<tile id="167" type="ForegroundTile"/>
<tile id="168" type="ForegroundTile"/>
<tile id="169" type="ForegroundTile"/>
<tile id="193" type="ForegroundTile"/>
<tile id="194" type="ForegroundTile"/>
<tile id="195" type="ForegroundTile"/>
<tile id="196" type="ForegroundTile"/>
<tile id="197" type="ForegroundTile"/>
<tile id="198" type="ForegroundTile"/>
<tile id="222" type="ForegroundTile"/>
<tile id="223" type="ForegroundTile"/>
<tile id="224" type="ForegroundTile"/>
<tile id="225" type="ForegroundTile"/>
<tile id="226" type="ForegroundTile"/>
<tile id="227" type="ForegroundTile"/>
<tile id="251" type="ForegroundTile"/>
<tile id="252" type="ForegroundTile"/>
<tile id="253" type="ForegroundTile"/>
<tile id="254" type="ForegroundTile"/>
<tile id="255" type="ForegroundTile"/>
<tile id="256" type="ForegroundTile"/>
<tile id="280" type="ForegroundTile"/>
<tile id="281" type="ForegroundTile"/>
<tile id="282" type="ForegroundTile"/>
<tile id="283" type="ForegroundTile"/>
<tile id="284" type="ForegroundTile"/>
<tile id="285" type="ForegroundTile"/>
<tile id="310">
<objectgroup draworder="index" id="2">
<object id="1" x="6" y="12" width="18" height="12"/>
</objectgroup>
</tile>
<tile id="311">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="24" height="24"/>
<object id="2" x="0" y="0" width="24" height="24"/>
</objectgroup>
</tile>
<tile id="312">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="24" height="24"/>
</objectgroup>
</tile>
<tile id="313">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="9" height="24"/>
</objectgroup>
</tile>
<tile id="339">
<objectgroup draworder="index" id="2">
<object id="1" x="13" y="0" width="11" height="22"/>
</objectgroup>
</tile>
<tile id="340">
<objectgroup draworder="index" id="3">
<object id="7" x="0" y="0" width="24" height="22"/>
</objectgroup>
</tile>
<tile id="341">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="24" height="22"/>
</objectgroup>
</tile>
<tile id="342">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="6" height="22"/>
</objectgroup>
</tile>
</tileset>

Loading…
Cancel
Save