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 |
+72
-46
@@ -2,6 +2,10 @@
|
||||
#include "entities/entitybase.hpp"
|
||||
#include "render/textures.hpp"
|
||||
#include "tiles/tilebase.hpp"
|
||||
#include "saving/saving.hpp"
|
||||
#include "settings/hardcode.hpp"
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
void PlayerController(Entity &e, float deltatime) {
|
||||
raylib::Vector2 acceleration = {0,0};
|
||||
@@ -17,37 +21,55 @@ void PlayerController(Entity &e, float deltatime) {
|
||||
e.rect.y += acceleration.y * 100 * deltatime;
|
||||
}
|
||||
|
||||
struct Tiles {
|
||||
int chunkx;
|
||||
int chunky;
|
||||
int tileindex;
|
||||
};
|
||||
|
||||
int main() {
|
||||
int screenWidth = 1920;
|
||||
int screenHeight = 1080;
|
||||
int screenWidth = 1980 / 2;
|
||||
int screenHeight = 1080 / 2;
|
||||
|
||||
|
||||
raylib::Window window(screenWidth, screenHeight, "raylib-cpp - basic window");
|
||||
|
||||
window.SetFullscreen(true);
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
||||
GenDefaultTexture();
|
||||
GenDefaultTexture();
|
||||
|
||||
Map gamemap;
|
||||
gamemap.Init();
|
||||
Map gamemap;
|
||||
gamemap.Init();
|
||||
|
||||
raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64};
|
||||
Entity player{1, playerrect, "MainPlayer", 100, 100};
|
||||
player.controller = PlayerController;
|
||||
player.spriteid = 0;
|
||||
raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64};
|
||||
Entity player{1, playerrect, "MainPlayer", 100, 100};
|
||||
player.controller = PlayerController;
|
||||
player.spriteid = 0;
|
||||
|
||||
entityTextures.LoadImage("./src/assets/playersprite.png");
|
||||
tileTextures.LoadImage("./src/assets/BasicTile.png");
|
||||
raylib::Texture placeholder("./src/assets/rock1.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/rock1.png");
|
||||
|
||||
Camera2D camera = { 0 };
|
||||
camera.offset = { screenWidth/2.0f, screenHeight/2.0f };
|
||||
camera.rotation = 0.0f;
|
||||
camera.zoom = 1.0f;
|
||||
Camera2D camera = { 0 };
|
||||
camera.offset = { screenWidth/2.0f, screenHeight/2.0f };
|
||||
camera.rotation = 0.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();
|
||||
|
||||
player.Update(deltatime);
|
||||
@@ -57,49 +79,53 @@ int main() {
|
||||
|
||||
BeginMode2D(camera);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
int bh = bg.height;
|
||||
int bw = bg.width;
|
||||
int offsetX = ((int)camera.target.x % bg.width);
|
||||
int offsetY = ((int)camera.target.y % bg.height);
|
||||
Coordinate chunkpos;
|
||||
chunkpos.x = worldpos.x / CHUNKWIDTH;
|
||||
chunkpos.y = worldpos.y / CHUNKWIDTH;
|
||||
|
||||
//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{
|
||||
camera.target.x - screenWidth / 2 * camera.zoom,
|
||||
camera.target.y - screenHeight / 2 * camera.zoom,
|
||||
screenWidth * camera.zoom,
|
||||
screenHeight * camera.zoom,
|
||||
};
|
||||
raylib::Rectangle fov{
|
||||
camera.target.x - screenWidth / 2 / zoom,
|
||||
camera.target.y - screenHeight / 2 / zoom,
|
||||
screenWidth / zoom,
|
||||
screenHeight / zoom,
|
||||
};
|
||||
|
||||
gamemap.Draw(fov);
|
||||
gamemap.Draw(fov);
|
||||
|
||||
player.Draw();
|
||||
player.Draw();
|
||||
|
||||
EndMode2D();
|
||||
EndDrawing();
|
||||
EndMode2D();
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
+47
-24
@@ -2,6 +2,16 @@
|
||||
#include "tilebase.hpp"
|
||||
#include "iostream"
|
||||
|
||||
Coordinate::Coordinate() {
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
Coordinate::Coordinate(int X, int Y) {
|
||||
x = X;
|
||||
y = Y;
|
||||
}
|
||||
|
||||
// ----------------- Map -----------------
|
||||
Map::Map()
|
||||
: worldtime(0), DEBUG(false) {
|
||||
@@ -18,39 +28,46 @@ Layer::Layer()
|
||||
: id(0), DEBUG(false), owner(nullptr) {}
|
||||
|
||||
// ----------------- Chunk -----------------
|
||||
Chunk::Chunk(Vector2 pos, Layer* Owner)
|
||||
Chunk::Chunk(Coordinate pos, Layer* Owner)
|
||||
: position(pos), owner(Owner) {
|
||||
|
||||
tiles.reserve(64);
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
tiles[i].owner = this;
|
||||
tiles[i].id = i;
|
||||
tiles[i].spriteid = 0;
|
||||
int spriteid = owner->id > 0;
|
||||
tiles.emplace_back(i, spriteid, this);
|
||||
}
|
||||
}
|
||||
|
||||
Chunk::Chunk()
|
||||
: position(0, 0), owner(nullptr) {
|
||||
tiles.reserve(64);
|
||||
for (int i = 0; i < 64; i++) {
|
||||
tiles[i].owner = this;
|
||||
tiles[i].id = i;
|
||||
tiles[i].spriteid = 0;
|
||||
int spriteid = owner->id > 0;
|
||||
tiles.emplace_back(i, spriteid, this);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------- Tile -----------------
|
||||
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()
|
||||
: id(0), spriteid(1), animationframe(0), owner(nullptr) {}
|
||||
|
||||
// ----------------- Vector2Hash / Eq -----------------
|
||||
std::size_t Vector2Hash::operator()(const Vector2& v) const noexcept {
|
||||
std::size_t h1 = std::hash<int>{}((int)v.x);
|
||||
std::size_t h2 = std::hash<int>{}((int)v.y);
|
||||
std::size_t CoordinateHash::operator()(const Coordinate& c) const noexcept {
|
||||
std::size_t h1 = std::hash<int>{}(c.x);
|
||||
std::size_t h2 = std::hash<int>{}(c.y);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -58,8 +75,8 @@ void Tile::Draw() {
|
||||
int posx = owner->position.x * 64 * 8 + id % 8 * 64;
|
||||
int posy = owner->position.y * 64 * 8 + id / 8 * 64;
|
||||
|
||||
std::cout << "Tile at "<< posx << ", " << posy << "\n";
|
||||
std::cout << "Owner at "<< owner->position.x << ", " << owner->position.y << "\n";
|
||||
//std::cout << "Tile at "<< posx << ", " << posy << "\n";
|
||||
//std::cout << "Owner at "<< owner->position.x << ", " << owner->position.y << "\n";
|
||||
|
||||
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++) {
|
||||
int x = 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();
|
||||
//}
|
||||
}
|
||||
}
|
||||
;}
|
||||
}
|
||||
|
||||
void Layer::Draw(raylib::Rectangle aera) {
|
||||
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);
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,9 +111,15 @@ void Map::Draw(raylib::Rectangle aera) {
|
||||
}
|
||||
|
||||
void Map::Init() {
|
||||
for (int x = -1; x < 2; x++) {
|
||||
for (int y = -1; y < 2; y++) {
|
||||
layers[0].chunks[Vector2{(float)x,(float)y}] = Chunk(Vector2{ (float)x, (float)y }, &layers[0]);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int x = -1; x < 2; x++) {
|
||||
for (int y = -1; y < 2; y++) {
|
||||
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 Map;
|
||||
|
||||
struct Coordinate {
|
||||
int x, y;
|
||||
|
||||
Coordinate();
|
||||
Coordinate(int X, int Y);
|
||||
};
|
||||
|
||||
struct Tile {
|
||||
ID id;
|
||||
ID spriteid;
|
||||
@@ -26,26 +33,26 @@ struct Tile {
|
||||
};
|
||||
|
||||
struct Chunk {
|
||||
raylib::Vector2 position;
|
||||
Tile tiles[64]; // dynamic instead of fixed array
|
||||
Coordinate position;
|
||||
std::vector<Tile> tiles; // dynamic instead of fixed array
|
||||
Layer* owner;
|
||||
|
||||
void Draw(raylib::Rectangle aera);
|
||||
|
||||
Chunk(Vector2 pos, Layer* Owner);
|
||||
Chunk(Coordinate pos, Layer* Owner);
|
||||
Chunk(); // default constructor
|
||||
};
|
||||
|
||||
struct Vector2Hash {
|
||||
std::size_t operator()(const Vector2& v) const noexcept;
|
||||
struct CoordinateHash {
|
||||
std::size_t operator()(const Coordinate& c) const noexcept;
|
||||
};
|
||||
|
||||
struct Vector2Eq {
|
||||
bool operator()(const Vector2& a, const Vector2& b) const noexcept;
|
||||
struct CoordinateEq {
|
||||
bool operator()(const Coordinate& a, const Coordinate& b) const noexcept;
|
||||
};
|
||||
|
||||
struct Layer {
|
||||
std::unordered_map<Vector2, Chunk, Vector2Hash, Vector2Eq> chunks;
|
||||
std::unordered_map<Coordinate, Chunk, CoordinateHash, CoordinateEq> chunks;
|
||||
ID id;
|
||||
bool DEBUG;
|
||||
Map* owner;
|
||||
@@ -67,7 +74,7 @@ enum Actions{
|
||||
|
||||
struct packet {
|
||||
Actions action;
|
||||
Vector2 position;
|
||||
Coordinate position;
|
||||
int data;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user