Compare commits

...

2 commits

Author SHA1 Message Date
b3f1071a1d
We have items and a conveyor belt!
Map is now a singleton so nodes can look up blocks that are next to them

Conveyor block/entity now exists and can move an item to the right

Added a wrench item for conveyor belts to move, it has the best texture
ever
2021-08-07 02:06:35 -04:00
9633b155bd
Fixed GL math
I did almost every matrix operation backwards...
2021-08-07 02:02:44 -04:00
4 changed files with 33 additions and 14 deletions

View file

@ -1,7 +1,10 @@
#include "app.h" #include "app.h"
#include "map/map.h" #include "map/map.h"
#include "interfaces/itemsink.h"
#include "items/wrench.h"
#include "tiles/rainbow.h" #include "tiles/rainbow.h"
#include "tiles/conveyor.h"
#include "tiles/dirt.h" #include "tiles/dirt.h"
#include "tiles/grass.h" #include "tiles/grass.h"
#include "util/vector2.h" #include "util/vector2.h"
@ -23,14 +26,16 @@ bool App::OnUserCreate()
bool App::OnUserUpdate(float delta) bool App::OnUserUpdate(float delta)
{ {
auto &map = Map::instance();
Vector2<int> screen = {ScreenWidth(), ScreenHeight()}; Vector2<int> screen = {ScreenWidth(), ScreenHeight()};
Vector2<int> mouse = {GetMouseX(), GetMouseY()}; Vector2<int> mouse = {GetMouseX(), GetMouseY()};
// Save and load keys // Save and load keys
if(GetKey(olc::Key::S).bReleased) if(GetKey(olc::Key::S).bReleased)
m_map.save(); map.save();
if(GetKey(olc::Key::L).bReleased) if(GetKey(olc::Key::L).bReleased)
m_map.load(); map.load();
// Basic block placing // Basic block placing
if(GetMouse(0).bPressed) { if(GetMouse(0).bPressed) {
@ -38,14 +43,21 @@ bool App::OnUserUpdate(float delta)
auto tile_pos = screen_pos / (TILE_SIZE + 1); auto tile_pos = screen_pos / (TILE_SIZE + 1);
if(screen_pos.x() < 0) tile_pos -= {1, 0}; if(screen_pos.x() < 0) tile_pos -= {1, 0};
if(screen_pos.y() < 0) tile_pos -= {0, 1}; if(screen_pos.y() < 0) tile_pos -= {0, 1};
m_map.set_tile(tile_pos, new Rainbow()); map.set_tile(tile_pos, new Conveyor());
} }
if(GetMouse(1).bPressed) { if(GetMouse(1).bPressed) {
auto screen_pos = mouse - m_camera_pos; auto screen_pos = mouse - m_camera_pos;
auto tile_pos = screen_pos / (TILE_SIZE + 1); auto tile_pos = screen_pos / (TILE_SIZE + 1);
if(screen_pos.x() < 0) tile_pos -= {1, 0}; if(screen_pos.x() < 0) tile_pos -= {1, 0};
if(screen_pos.y() < 0) tile_pos -= {0, 1}; if(screen_pos.y() < 0) tile_pos -= {0, 1};
m_map.set_tile(tile_pos, new Dirt()); // map.set_tile(tile_pos, new Dirt());
auto sink = dynamic_cast<ItemSink *>(&map.at(tile_pos).entity());
std::cout << "Insert " << sink << std::endl;
if(sink) {
std::cout << sink->can_accept(*(new Wrench())) << std::endl;
sink->insert(*(new Wrench()));
std::cout << sink->can_accept(*(new Wrench())) << std::endl;
}
} }
// Pan camera with middle mouse // Pan camera with middle mouse
@ -68,7 +80,7 @@ bool App::OnUserUpdate(float delta)
for(int y = min.y() - 1; y < max.y() + 1; y++) { for(int y = min.y() - 1; y < max.y() + 1; y++) {
for(int x = min.x() - 1; x < max.x() + 1; x++) { for(int x = min.x() - 1; x < max.x() + 1; x++) {
auto &node = m_map.at(Vector2<int>(x, y)); auto &node = map.at(Vector2<int>(x, y));
node.update(delta); node.update(delta);
node.render(m_render); node.render(m_render);
} }

View file

@ -17,7 +17,6 @@ public:
private: private:
Render m_render; Render m_render;
Map m_map;
Vector2<int> m_drag_start; Vector2<int> m_drag_start;
Vector2<int> m_camera_pos; Vector2<int> m_camera_pos;

View file

@ -10,6 +10,14 @@ class Map {
public: public:
Map() = default; Map() = default;
Map(Map const &) = delete;
void operator=(const Map &) = delete;
static Map &instance() {
static Map s_instance;
return s_instance;
}
Node &at(Vector2<int> pos); Node &at(Vector2<int> pos);
void set_tile(Vector2<int> pos, Tile *tile); void set_tile(Vector2<int> pos, Tile *tile);

View file

@ -50,10 +50,10 @@ void Render::fill_rectangle_sz(Vector2<int> size, Color color) {
} }
void Render::draw_lines(std::vector<Vector2<float>> points, Color color) { void Render::draw_lines(std::vector<Vector2<float>> points, Color color) {
auto prev_cache = m_matrix * m_view_matrix * Vector3<float>(points[0], 1); auto prev_cache = m_view_matrix * m_matrix * Vector3<float>(points[0], 1);
for(size_t i = 1; i < points.size(); i++) { for(size_t i = 1; i < points.size(); i++) {
auto start = prev_cache; auto start = prev_cache;
auto end = m_matrix * m_view_matrix * Vector3<float>(points[i], 1); auto end = m_view_matrix * m_matrix * Vector3<float>(points[i], 1);
m_pge->DrawLine( m_pge->DrawLine(
start.x(), start.x(),
@ -118,10 +118,10 @@ void Render::pop_matrix() {
void Render::translate(Vector2<float> trans) { void Render::translate(Vector2<float> trans) {
switch(m_matrix_mode) { switch(m_matrix_mode) {
case MODEL: case MODEL:
m_matrix = translation_matrix(trans) * m_matrix; m_matrix *= translation_matrix(trans);
break; break;
case PROJECTION: case PROJECTION:
m_view_matrix = translation_matrix(trans) * m_view_matrix; m_view_matrix *= translation_matrix(trans);
break; break;
} }
} }
@ -129,10 +129,10 @@ void Render::translate(Vector2<float> trans) {
void Render::rotate(float theta) { void Render::rotate(float theta) {
switch(m_matrix_mode) { switch(m_matrix_mode) {
case MODEL: case MODEL:
m_matrix = rotation_matrix(theta) * m_matrix; m_matrix *= rotation_matrix(theta);
break; break;
case PROJECTION: case PROJECTION:
m_view_matrix = rotation_matrix(theta) * m_view_matrix; m_view_matrix *= rotation_matrix(theta);
break; break;
} }
} }
@ -140,10 +140,10 @@ void Render::rotate(float theta) {
void Render::scale(Vector2<float> scale) { void Render::scale(Vector2<float> scale) {
switch(m_matrix_mode) { switch(m_matrix_mode) {
case MODEL: case MODEL:
m_matrix = scale_matrix(scale) * m_matrix; m_matrix *= scale_matrix(scale);
break; break;
case PROJECTION: case PROJECTION:
m_view_matrix = scale_matrix(scale) * m_view_matrix; m_view_matrix *= scale_matrix(scale);
} }
} }