Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c3349a7f51 | |||
| a496a5e59a | |||
| 0b304a3747 | |||
| bdad7ec589 | |||
| d21606a3b5 | |||
| 7a7d0ad6d7 | |||
| 9ed0561040 | |||
| 41d9622ff6 | |||
| bc3771e119 | |||
| eb65e3658d |
Binary file not shown.
|
After Width: | Height: | Size: 109 B |
+53
-27
@@ -2,6 +2,10 @@
|
|||||||
#include "entities/entitybase.hpp"
|
#include "entities/entitybase.hpp"
|
||||||
#include "render/textures.hpp"
|
#include "render/textures.hpp"
|
||||||
#include "tiles/tilebase.hpp"
|
#include "tiles/tilebase.hpp"
|
||||||
|
#include "saving/saving.hpp"
|
||||||
|
#include "settings/hardcode.hpp"
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
void PlayerController(Entity &e, float deltatime) {
|
void PlayerController(Entity &e, float deltatime) {
|
||||||
raylib::Vector2 acceleration = {0,0};
|
raylib::Vector2 acceleration = {0,0};
|
||||||
@@ -17,13 +21,21 @@ void PlayerController(Entity &e, float deltatime) {
|
|||||||
e.rect.y += acceleration.y * 100 * deltatime;
|
e.rect.y += acceleration.y * 100 * deltatime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Tiles {
|
||||||
|
int chunkx;
|
||||||
|
int chunky;
|
||||||
|
int tileindex;
|
||||||
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int screenWidth = 1920;
|
int screenWidth = 1980 / 2;
|
||||||
int screenHeight = 1080;
|
int screenHeight = 1080 / 2;
|
||||||
|
|
||||||
|
|
||||||
raylib::Window window(screenWidth, screenHeight, "raylib-cpp - basic window");
|
raylib::Window window(screenWidth, screenHeight, "raylib-cpp - basic window");
|
||||||
|
|
||||||
|
window.SetFullscreen(true);
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
GenDefaultTexture();
|
GenDefaultTexture();
|
||||||
@@ -37,17 +49,27 @@ int main() {
|
|||||||
player.spriteid = 0;
|
player.spriteid = 0;
|
||||||
|
|
||||||
entityTextures.LoadImage("./src/assets/playersprite.png");
|
entityTextures.LoadImage("./src/assets/playersprite.png");
|
||||||
|
std::cout << "kms\n";
|
||||||
|
tileTextures.LoadImage("./src/assets/transparent.png");
|
||||||
tileTextures.LoadImage("./src/assets/BasicTile.png");
|
tileTextures.LoadImage("./src/assets/BasicTile.png");
|
||||||
raylib::Texture placeholder("./src/assets/rock1.png");
|
tileTextures.LoadImage("./src/assets/rock1.png");
|
||||||
|
|
||||||
Camera2D camera = { 0 };
|
Camera2D camera = { 0 };
|
||||||
camera.offset = { screenWidth/2.0f, screenHeight/2.0f };
|
camera.offset = { screenWidth/2.0f, screenHeight/2.0f };
|
||||||
camera.rotation = 0.0f;
|
camera.rotation = 0.0f;
|
||||||
camera.zoom = 1.0f;
|
camera.zoom = 1.0f;
|
||||||
|
|
||||||
|
Vector2 storedPosition = { 0, 0 };
|
||||||
|
bool shouldDrawPlaceholder = false;
|
||||||
|
|
||||||
while (!window.ShouldClose())
|
float zoom = 1;
|
||||||
{
|
|
||||||
|
int chunkx = 0;
|
||||||
|
int chunky = 0;
|
||||||
|
|
||||||
|
std::cout << "textures and includes loaded, window stuff now\n";
|
||||||
|
|
||||||
|
while (!window.ShouldClose()) {
|
||||||
float deltatime = GetFrameTime();
|
float deltatime = GetFrameTime();
|
||||||
|
|
||||||
player.Update(deltatime);
|
player.Update(deltatime);
|
||||||
@@ -57,38 +79,31 @@ int main() {
|
|||||||
|
|
||||||
BeginMode2D(camera);
|
BeginMode2D(camera);
|
||||||
|
|
||||||
|
|
||||||
camera.target = { player.rect.x + player.rect.width / 2.0f, player.rect.y + player.rect.height / 2.0f};
|
camera.target = { player.rect.x + player.rect.width / 2.0f, player.rect.y + player.rect.height / 2.0f};
|
||||||
|
|
||||||
if (IsKeyDown(KEY_Q)) camera.zoom += 0.1f;
|
if (IsKeyDown(KEY_Q)) zoom += 0.1f;
|
||||||
|
|
||||||
DrawText("Middle", 190, 200, 20, WHITE);
|
DrawText("Middle", 190, 200, 20, WHITE);
|
||||||
|
|
||||||
Texture bg = tileTextures.GetTexture(0);
|
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
|
||||||
|
Vector2 mousepos = GetMousePosition();
|
||||||
|
Vector2 worldpos;
|
||||||
|
worldpos.x = mousepos.x + camera.target.x;
|
||||||
|
worldpos.y = mousepos.y + camera.target.y;
|
||||||
|
|
||||||
// Infinite Background
|
Coordinate chunkpos;
|
||||||
int bh = bg.height;
|
chunkpos.x = worldpos.x / CHUNKWIDTH;
|
||||||
int bw = bg.width;
|
chunkpos.y = worldpos.y / CHUNKWIDTH;
|
||||||
int offsetX = ((int)camera.target.x % bg.width);
|
|
||||||
int offsetY = ((int)camera.target.y % bg.height);
|
|
||||||
|
|
||||||
//for (int i = -1; i <= GetScreenWidth() / bw + 1; i++) {
|
}
|
||||||
// for (int j = -1; j <= GetScreenHeight() / bh + 1; j++) {
|
|
||||||
// DrawTexture(
|
|
||||||
// bg,
|
|
||||||
// camera.target.x - offsetX - screenWidth / 2.0f + i * bw,
|
|
||||||
// camera.target.y - offsetY - screenHeight / 2.0f + j * bh,
|
|
||||||
// WHITE
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
//if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) placeholder.Draw(GetMousePosition().x, GetMousePosition().y, WHITE);
|
|
||||||
|
|
||||||
raylib::Rectangle fov{
|
raylib::Rectangle fov{
|
||||||
camera.target.x - screenWidth / 2 * camera.zoom,
|
camera.target.x - screenWidth / 2 / zoom,
|
||||||
camera.target.y - screenHeight / 2 * camera.zoom,
|
camera.target.y - screenHeight / 2 / zoom,
|
||||||
screenWidth * camera.zoom,
|
screenWidth / zoom,
|
||||||
screenHeight * camera.zoom,
|
screenHeight / zoom,
|
||||||
};
|
};
|
||||||
|
|
||||||
gamemap.Draw(fov);
|
gamemap.Draw(fov);
|
||||||
@@ -101,5 +116,16 @@ int main() {
|
|||||||
|
|
||||||
// UnloadTexture() and CloseWindow() are called automatically.
|
// UnloadTexture() and CloseWindow() are called automatically.
|
||||||
|
|
||||||
|
std::cout << "Saving Data...\n";
|
||||||
|
|
||||||
|
std::cout << "Player is at: " << player.rect.x << ", " << player.rect.y << "\n";
|
||||||
|
std::cout << "chunkx: " << chunkx << "\n";
|
||||||
|
std::cout << "chunky: " << chunky << "\n";
|
||||||
|
// std::cout << "tileindex: " << tileindex << "\n";
|
||||||
|
|
||||||
|
SaveGame(player.rect.x, player.rect.y);
|
||||||
|
|
||||||
|
std::cout << "Data Saved!\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void SaveGame(int x, int y) {
|
||||||
|
ofstream outfile("save.json");
|
||||||
|
|
||||||
|
outfile << x << " " << y << std::endl;
|
||||||
|
|
||||||
|
outfile.close();
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef SAVES_HPP
|
||||||
|
#define SAVES_HPP
|
||||||
|
|
||||||
|
void SaveGame(int x, int y);
|
||||||
|
void LoadGame(int &x, int &y);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef HARDCODE
|
||||||
|
#define HARDCODE
|
||||||
|
|
||||||
|
const int CHUNKSIZE = 8;
|
||||||
|
const int TILEWIDTH = 64;
|
||||||
|
const int CHUNKWIDTH = CHUNKSIZE * TILEWIDTH;
|
||||||
|
const int CHUNKTILES = CHUNKSIZE * CHUNKSIZE;
|
||||||
|
|
||||||
|
#endif // !HARDCODE
|
||||||
+44
-21
@@ -2,6 +2,16 @@
|
|||||||
#include "tilebase.hpp"
|
#include "tilebase.hpp"
|
||||||
#include "iostream"
|
#include "iostream"
|
||||||
|
|
||||||
|
Coordinate::Coordinate() {
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Coordinate::Coordinate(int X, int Y) {
|
||||||
|
x = X;
|
||||||
|
y = Y;
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------- Map -----------------
|
// ----------------- Map -----------------
|
||||||
Map::Map()
|
Map::Map()
|
||||||
: worldtime(0), DEBUG(false) {
|
: worldtime(0), DEBUG(false) {
|
||||||
@@ -18,39 +28,46 @@ Layer::Layer()
|
|||||||
: id(0), DEBUG(false), owner(nullptr) {}
|
: id(0), DEBUG(false), owner(nullptr) {}
|
||||||
|
|
||||||
// ----------------- Chunk -----------------
|
// ----------------- Chunk -----------------
|
||||||
Chunk::Chunk(Vector2 pos, Layer* Owner)
|
Chunk::Chunk(Coordinate pos, Layer* Owner)
|
||||||
: position(pos), owner(Owner) {
|
: position(pos), owner(Owner) {
|
||||||
|
|
||||||
|
tiles.reserve(64);
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
tiles[i].owner = this;
|
int spriteid = owner->id > 0;
|
||||||
tiles[i].id = i;
|
tiles.emplace_back(i, spriteid, this);
|
||||||
tiles[i].spriteid = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::Chunk()
|
Chunk::Chunk()
|
||||||
: position(0, 0), owner(nullptr) {
|
: position(0, 0), owner(nullptr) {
|
||||||
|
tiles.reserve(64);
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
tiles[i].owner = this;
|
int spriteid = owner->id > 0;
|
||||||
tiles[i].id = i;
|
tiles.emplace_back(i, spriteid, this);
|
||||||
tiles[i].spriteid = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------- Tile -----------------
|
// ----------------- Tile -----------------
|
||||||
Tile::Tile(ID Id, ID Spriteid, Chunk* Owner)
|
Tile::Tile(ID Id, ID Spriteid, Chunk* Owner)
|
||||||
: id(Id), spriteid(Spriteid), animationframe(0), owner(Owner) {}
|
: id(Id), spriteid(Spriteid), animationframe(0), owner(Owner) {
|
||||||
|
|
||||||
|
int posx = owner->position.x * 64 * 8 + id % 8 * 64;
|
||||||
|
int posy = owner->position.y * 64 * 8 + id / 8 * 64;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Tile::Tile()
|
Tile::Tile()
|
||||||
: id(0), spriteid(1), animationframe(0), owner(nullptr) {}
|
: id(0), spriteid(1), animationframe(0), owner(nullptr) {}
|
||||||
|
|
||||||
// ----------------- Vector2Hash / Eq -----------------
|
// ----------------- Vector2Hash / Eq -----------------
|
||||||
std::size_t Vector2Hash::operator()(const Vector2& v) const noexcept {
|
std::size_t CoordinateHash::operator()(const Coordinate& c) const noexcept {
|
||||||
std::size_t h1 = std::hash<int>{}((int)v.x);
|
std::size_t h1 = std::hash<int>{}(c.x);
|
||||||
std::size_t h2 = std::hash<int>{}((int)v.y);
|
std::size_t h2 = std::hash<int>{}(c.y);
|
||||||
return h1 ^ (h2 << 1);
|
return h1 ^ (h2 << 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Vector2Eq::operator()(const Vector2& a, const Vector2& b) const noexcept {
|
bool CoordinateEq::operator()(const Coordinate& a, const Coordinate& b) const noexcept {
|
||||||
return ((int)a.x == (int)b.x && (int)a.y == (int)b.y);
|
return ((int)a.x == (int)b.x && (int)a.y == (int)b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +75,8 @@ void Tile::Draw() {
|
|||||||
int posx = owner->position.x * 64 * 8 + id % 8 * 64;
|
int posx = owner->position.x * 64 * 8 + id % 8 * 64;
|
||||||
int posy = owner->position.y * 64 * 8 + id / 8 * 64;
|
int posy = owner->position.y * 64 * 8 + id / 8 * 64;
|
||||||
|
|
||||||
std::cout << "Tile at "<< posx << ", " << posy << "\n";
|
//std::cout << "Tile at "<< posx << ", " << posy << "\n";
|
||||||
std::cout << "Owner at "<< owner->position.x << ", " << owner->position.y << "\n";
|
//std::cout << "Owner at "<< owner->position.x << ", " << owner->position.y << "\n";
|
||||||
|
|
||||||
DrawTextureEx(tileTextures.GetTexture(spriteid), Vector2({(float)posx,(float)posy}), 0, 1, WHITE);
|
DrawTextureEx(tileTextures.GetTexture(spriteid), Vector2({(float)posx,(float)posy}), 0, 1, WHITE);
|
||||||
|
|
||||||
@@ -69,21 +86,21 @@ void Chunk::Draw(raylib::Rectangle aera) {
|
|||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
int x = i % 8;
|
int x = i % 8;
|
||||||
int y = i / 8;
|
int y = i / 8;
|
||||||
raylib::Rectangle box{x * 64 + position.x * 8 * 64, y * 64 + position.y * 8 * 64, 64, 64};
|
raylib::Rectangle box{x * 64 + (float)position.x * 8 * 64, y * 64 + (float)position.y * 8 * 64, 64, 64};
|
||||||
|
|
||||||
//if (box.CheckCollision(aera)) {
|
if (box.CheckCollision(aera)) {
|
||||||
tiles[i].Draw();
|
tiles[i].Draw();
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
;}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Layer::Draw(raylib::Rectangle aera) {
|
void Layer::Draw(raylib::Rectangle aera) {
|
||||||
for (auto it = chunks.begin(); it != chunks.end(); ++it) {
|
for (auto it = chunks.begin(); it != chunks.end(); ++it) {
|
||||||
raylib::Rectangle box{it->first.x * 64 * 8, it->first.y * 64 * 8, 64 * 8, 64 * 8};
|
raylib::Rectangle box{(float)(it->first.x * 64 * 8), (float)(it->first.y * 64 * 8), 64 * 8, 64 * 8};
|
||||||
|
|
||||||
//if (box.CheckCollision(aera)) {
|
if (box.CheckCollision(aera)) {
|
||||||
it->second.Draw(aera);
|
it->second.Draw(aera);
|
||||||
//}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,9 +111,15 @@ void Map::Draw(raylib::Rectangle aera) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Map::Init() {
|
void Map::Init() {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
for (int x = -1; x < 2; x++) {
|
for (int x = -1; x < 2; x++) {
|
||||||
for (int y = -1; y < 2; y++) {
|
for (int y = -1; y < 2; y++) {
|
||||||
layers[0].chunks[Vector2{(float)x,(float)y}] = Chunk(Vector2{ (float)x, (float)y }, &layers[0]);
|
layers[i].chunks.emplace(
|
||||||
|
std::piecewise_construct,
|
||||||
|
std::forward_as_tuple(Coordinate{x, y}), // key
|
||||||
|
std::forward_as_tuple(Coordinate{x, y}, &layers[i])
|
||||||
|
);// value: construct Chunk here
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-9
@@ -12,6 +12,13 @@ struct Chunk;
|
|||||||
struct Layer;
|
struct Layer;
|
||||||
struct Map;
|
struct Map;
|
||||||
|
|
||||||
|
struct Coordinate {
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
Coordinate();
|
||||||
|
Coordinate(int X, int Y);
|
||||||
|
};
|
||||||
|
|
||||||
struct Tile {
|
struct Tile {
|
||||||
ID id;
|
ID id;
|
||||||
ID spriteid;
|
ID spriteid;
|
||||||
@@ -26,26 +33,26 @@ struct Tile {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Chunk {
|
struct Chunk {
|
||||||
raylib::Vector2 position;
|
Coordinate position;
|
||||||
Tile tiles[64]; // dynamic instead of fixed array
|
std::vector<Tile> tiles; // dynamic instead of fixed array
|
||||||
Layer* owner;
|
Layer* owner;
|
||||||
|
|
||||||
void Draw(raylib::Rectangle aera);
|
void Draw(raylib::Rectangle aera);
|
||||||
|
|
||||||
Chunk(Vector2 pos, Layer* Owner);
|
Chunk(Coordinate pos, Layer* Owner);
|
||||||
Chunk(); // default constructor
|
Chunk(); // default constructor
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Vector2Hash {
|
struct CoordinateHash {
|
||||||
std::size_t operator()(const Vector2& v) const noexcept;
|
std::size_t operator()(const Coordinate& c) const noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Vector2Eq {
|
struct CoordinateEq {
|
||||||
bool operator()(const Vector2& a, const Vector2& b) const noexcept;
|
bool operator()(const Coordinate& a, const Coordinate& b) const noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Layer {
|
struct Layer {
|
||||||
std::unordered_map<Vector2, Chunk, Vector2Hash, Vector2Eq> chunks;
|
std::unordered_map<Coordinate, Chunk, CoordinateHash, CoordinateEq> chunks;
|
||||||
ID id;
|
ID id;
|
||||||
bool DEBUG;
|
bool DEBUG;
|
||||||
Map* owner;
|
Map* owner;
|
||||||
@@ -67,7 +74,7 @@ enum Actions{
|
|||||||
|
|
||||||
struct packet {
|
struct packet {
|
||||||
Actions action;
|
Actions action;
|
||||||
Vector2 position;
|
Coordinate position;
|
||||||
int data;
|
int data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user