Scrolling tile view

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 258134bbac
commit 1a2bdd1895
  1. BIN
      C++ProjectTemplate
  2. 4
      assets/maps/map0
  3. 100
      main.cpp
  4. 1
      references.h

Binary file not shown.

@ -1,3 +1,4 @@
0000R00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@ -61,6 +62,3 @@
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
OBJECT17;30;0
OBJECT7;4;0

@ -7,13 +7,19 @@
#include "tiles.h"
#include "references.h"
#define WIDTH 256
#define HEIGHT 224
#define CAMERA_MOVESPD 0.5
using namespace olc;
class Map{
public:
std::string filename;
Map(std::string fname) {
Decal*tileset;
Map(std::string fname,Decal*tileset) {
this->filename=fname;
this->tileset=tileset;
}
};
@ -32,12 +38,12 @@ class Animation{
class Object{
public:
Animation*spr;
vi2d pos;
vd2d pos;
int frameIndex=0;
int frameCount=0;
int animationSpd=12; //How many frames to wait between each frame.
std::string name;
vi2d scale={1,1};
vd2d scale={1,1};
Pixel color=WHITE;
//animationSpd is how long to wait before switching frames.
Object(std::string name,vi2d pos,Animation*spr,vi2d scale={1,1},Pixel color=WHITE,int animationSpd=1) {
@ -67,6 +73,9 @@ public:
int MAP_HEIGHT;
Map*CURRENT_MAP;
Map*MAP_ONETT;
vd2d cameraPos = {0,0};
bool MOUSE_PRESSED_DOWN=false,MOUSE_DOWN=false,MOUSE_RELEASED=false; //TODO Implement Mouse things.
std::vector<std::vector<TILE*>> MAP;
std::map<Reference,Decal*> SPRITES;
@ -80,11 +89,11 @@ public:
ConsoleCaptureStdOut(true);
// Called once at the start, so create things here
MAP_ONETT=new Map("map0");
SetupAnimations();
SetupObjectInfo();
MAP_ONETT=new Map("map0",SPRITES[TILESET1]);
//OBJ_INFO["PLAYER"]=PLAYER_ANIMATION;
LoadMap(MAP_ONETT);
@ -111,11 +120,34 @@ public:
obj->frameIndex++;
}
}
};
if (UpHeld()) {
cameraPos.y-=CAMERA_MOVESPD;
}
if (RightHeld()) {
cameraPos.x+=CAMERA_MOVESPD;
}
if (LeftHeld()) {
cameraPos.x-=CAMERA_MOVESPD;
}
if (DownHeld()) {
cameraPos.y+=CAMERA_MOVESPD;
}
}
void drawGame(){
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);
}
for (int y=-1;y<HEIGHT/32+2;y++) {
for (int x=-1;x<WIDTH/32+2;x++) {
int yTileOffset = cameraPos.y/32;
int xTileOffset = cameraPos.x/32;
if (x+xTileOffset>=0&&x+xTileOffset<MAP_WIDTH&&y+yTileOffset>=0&&y+yTileOffset<MAP_HEIGHT) {
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES[TILESET1],{MAP[y+yTileOffset][x+xTileOffset]->tileX*32,MAP[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
}
}
};
void LoadMap(Map*map) {
@ -159,27 +191,79 @@ public:
OBJECTS.push_back(newObj);
//printf("Object: %s %s %s\n",split1.c_str(),split2.c_str(),split3.c_str());
} else {
std::vector<TILE*> tiles;
for (int i=0;i<data.length();i++) {
unsigned char tileX,tileY;
tileX=data[i]&0x0F;
tileY=(data[i]&0xF0)>>4;
tiles.push_back(new TILE(tileX,tileY));
}
MAP.push_back(tiles);
MAP_HEIGHT++;
}
}
printf("Loaded map %s.\n",map->filename.c_str());
}
Decal*CreateSprite(std::string spriteName) {
return new Decal(new Sprite("assets/"+spriteName));
}
void SetupAnimations() {
SPRITES[PLAYER] = new Decal(new Sprite("assets/player.png"));
SPRITES[PLAYER] = CreateSprite("player.png");
ANIMATIONS[PLAYER] = new Animation(SPRITES[PLAYER],32);
SPRITES[TILESET1] = CreateSprite("terrainmap.png");
}
void SetupObjectInfo() {
OBJ_INFO[PLAYER]=new Object("player",{0,0},ANIMATIONS[PLAYER]);
}
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;
}
bool DownPressed(){
return GetKey(S).bPressed||GetKey(DOWN).bPressed||GetKey(NP5).bPressed||GetKey(NP2).bPressed||MOUSE_PRESSED_DOWN&&GetMouseY()<=HEIGHT&&GetMouseY()>=HEIGHT-32&&GetMouseX()<=HEIGHT-128;
}
bool LeftPressed(){
return GetKey(A).bPressed||GetKey(LEFT).bPressed||GetKey(NP4).bPressed||MOUSE_PRESSED_DOWN&&GetMouseX()<=32&&GetMouseX()>=0&&GetMouseY()>=HEIGHT-128;
}
bool RightPressed(){
return GetKey(D).bPressed||GetKey(RIGHT).bPressed||GetKey(NP6).bPressed||MOUSE_PRESSED_DOWN&&GetMouseX()<=128&&GetMouseX()>=96&&GetMouseY()>=HEIGHT-128;
}
bool UpHeld(){
return GetKey(W).bHeld||GetKey(UP).bHeld||GetKey(NP8).bHeld||MOUSE_DOWN&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128;
}
bool DownHeld(){
return GetKey(S).bHeld||GetKey(DOWN).bHeld||GetKey(NP5).bHeld||GetKey(NP2).bHeld||MOUSE_DOWN&&GetMouseY()<=HEIGHT&&GetMouseY()>=HEIGHT-32&&GetMouseX()<=HEIGHT-128;
}
bool LeftHeld(){
return GetKey(A).bHeld||GetKey(LEFT).bHeld||GetKey(NP4).bHeld||MOUSE_DOWN&&GetMouseX()<=32&&GetMouseX()>=0&&GetMouseY()>=HEIGHT-128;
}
bool RightHeld(){
return GetKey(D).bHeld||GetKey(RIGHT).bHeld||GetKey(NP6).bHeld||MOUSE_DOWN&&GetMouseX()<=128&&GetMouseX()>=96&&GetMouseY()>=HEIGHT-128;
}
bool UpReleased(){
return GetKey(W).bReleased||GetKey(UP).bReleased||GetKey(NP8).bReleased||MOUSE_RELEASED&&GetMouseY()<=HEIGHT-128+32&&GetMouseY()>=HEIGHT-128&&GetMouseX()<=HEIGHT-128;
}
bool DownReleased(){
return GetKey(S).bReleased||GetKey(DOWN).bReleased||GetKey(NP5).bReleased||GetKey(NP2).bReleased||MOUSE_RELEASED&&GetMouseY()<=HEIGHT&&GetMouseY()>=HEIGHT-32&&GetMouseX()<=HEIGHT-128;
}
bool LeftReleased(){
return GetKey(A).bReleased||GetKey(LEFT).bReleased||GetKey(NP4).bReleased||MOUSE_RELEASED&&GetMouseX()<=32&&GetMouseX()>=0&&GetMouseY()>=HEIGHT-128;
}
bool RightReleased(){
return GetKey(D).bReleased||GetKey(RIGHT).bReleased||GetKey(NP6).bReleased||MOUSE_RELEASED&&GetMouseX()<=128&&GetMouseX()>=96&&GetMouseY()>=HEIGHT-128;
}
};
int main()
{
SeasonI demo;
if (demo.Construct(256, 224, 4, 4))
if (demo.Construct(WIDTH, HEIGHT, 4, 4))
demo.Start();
return 0;

@ -1,3 +1,4 @@
enum Reference{
PLAYER,
TILESET1,
};
Loading…
Cancel
Save