Skip to content

Commit

Permalink
cursor class
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Apr 13, 2024
1 parent 0bd9dff commit d7485d3
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 37 deletions.
46 changes: 46 additions & 0 deletions include/Cursor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by 盧威任 on 4/13/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_CURSOR_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_CURSOR_HPP
#include "DrawOverlays.hpp"
#include "Map/Map.hpp"
#include "Map/MapUtility.hpp"
#include "Structure/Structure.hpp"
class CursorClass {
public:
CursorClass() {
m_testdraw.Start(std::vector({glm::vec2(0.F, 0.F)}),
DrawOverlays::OverlayShapes::R_CROSS);
}
~CursorClass() {}
void Update(std::shared_ptr<TileClass> tile) {
DrawWalkable(tile->getWalkable());

Util::Transform trans2;
trans2.translation = Structure::PositionStickToGrid(
MapUtil::ScreenToGlobalCoord(Util::Input::GetCursorPosition()));
m_testdraw.DrawUsingCamera(trans2, 1);
}

void DrawWalkable(bool tilewalkable) {
if (tilewalkable) {
m_testdraw.setDrawMode(DrawOverlays::OverlayShapes::B_BOXES);
} else {
m_testdraw.setDrawMode(DrawOverlays::OverlayShapes::R_CROSS);
}
}

void DrawBuildable(bool tilebuildable) {
if (tilebuildable) {
m_testdraw.setDrawMode(DrawOverlays::OverlayShapes::B_SLATED);
} else {
m_testdraw.setDrawMode(DrawOverlays::OverlayShapes::R_SLATED);
}
}

private:
DrawOverlays m_testdraw;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_CURSOR_HPP
1 change: 1 addition & 0 deletions include/DrawOverlays.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_DRAWOVERLAYS_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_DRAWOVERLAYS_HPP
#include "Core/Drawable.hpp"
#include "Util/Image.hpp"
#include "Util/TransformUtils.hpp"
#include "pch.hpp"
class DrawOverlays : public Core::Drawable {
Expand Down
20 changes: 0 additions & 20 deletions include/Map/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,6 @@ class TileClass {
}

void clearSelectableObjects() { m_SelectableObjects.clear(); }

/*
bool operator==(const TileClass &tile) const {
if (tile.m_Name == m_Name && m_Walkable == tile.m_Walkable &&
m_Buildable == tile.m_Walkable) {
return true;
} else {
return false;
}
}
bool operator<(const TileClass &tile) const {
if (tile.m_Name < m_Name || m_Walkable < tile.m_Walkable ||
m_Buildable == tile.m_Walkable) {
return true;
} else {
return false;
}
}
*/
TileClass &operator=(const TileClass &tile) {
this->m_Walkable = tile.m_Walkable;
this->m_Clickable = tile.m_Clickable;
Expand Down
3 changes: 2 additions & 1 deletion include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_DEFAULTSCENE_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_DEFAULTSCENE_HPP

#include "Cursor.hpp"
#include "Scene.hpp"
#include "Unit/Avatar.hpp"
#include "Unit/Hunter.hpp"
Expand All @@ -20,7 +21,7 @@ class DefaultScene : public Scene {

private:
SpriteSheet m_SpriteSheet;
DrawOverlays m_testdraw;
CursorClass m_Cursor;
Grid testGrid;

Avatar m_dummy;
Expand Down
2 changes: 1 addition & 1 deletion include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Structure : public Util::GameObject,
glm::vec2 GetDrawLocation() { return DrawLocation; };
void SetID(GameObjectID id) { m_ID = id; };

static glm::vec2 ChangeToCell(glm::vec2 location);
static glm::vec2 PositionStickToGrid(glm::vec2 location);
void onSelected() { this->SetAttachVisible(getSelected()); };
virtual void attachmentUpdate();
bool getBuilt() {
Expand Down
15 changes: 3 additions & 12 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ void DefaultScene::Start() {
m_Map->getTileByCellPosition(glm::vec2(8, 5))->setWalkable(0);
m_Map->getTileByCellPosition(glm::vec2(9, 5))->setWalkable(0);
m_UI.Start();
m_testdraw.Start(std::vector({glm::vec2(0.F, 0.F)}),
DrawOverlays::OverlayShapes::R_CROSS);
// m_GameObjectManager.Start();

// m_dummy.Start({5, 5}, m_Map);
Expand Down Expand Up @@ -48,20 +46,13 @@ void DefaultScene::Update() {
m_Renderer.Update();
m_UI.Update();

Util::Transform trans2;
trans2.translation = Structure::ChangeToCell(
MapUtil::ScreenToGlobalCoord(Util::Input::GetCursorPosition()));
m_Cursor.Update(
m_Map->getTileByCellPosition(MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(Util::Input::GetCursorPosition()))));

auto tile = m_Map->getTileByCellPosition(MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(Util::Input::GetCursorPosition())));

if (tile->getWalkable()) {
m_testdraw.setDrawMode(DrawOverlays::OverlayShapes::B_BOXES);
} else {
m_testdraw.setDrawMode(DrawOverlays::OverlayShapes::R_CROSS);
}

m_testdraw.DrawUsingCamera(trans2, 1);
// m_GameObjectManager.Update();

if (m_UI.getIfAnyBuildingReadyToBuild()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Structure/Structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ void Structure::updateMoveable() {
}
}

glm::vec2 Structure::ChangeToCell(glm::vec2 location) {
glm::vec2 Structure::PositionStickToGrid(glm::vec2 location) {
int _x = location.x / CELL_SIZE.x;
int _y = location.y / CELL_SIZE.y;
return {_x * CELL_SIZE.x, _y * CELL_SIZE.y};
}
void Structure::SetObjectLocation(glm::vec2 location) {
location = ChangeToCell(location);
location = PositionStickToGrid(location);
ObjectLocation = location;
DrawLocation = {location.x + 0.5 * CELL_SIZE.x,
location.y + 0.5 * CELL_SIZE.y};
Expand Down
2 changes: 1 addition & 1 deletion src/Structure/WayPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void WayPoint::Update([[maybe_unused]] const Util::Transform &transsform) {
m_Transform.scale = {0.5, 0.5};
}
void WayPoint::SetObjectLocation(glm::vec2 location) {
Structure::ChangeToCell(location);
Structure::PositionStickToGrid(location);
ObjectLocation = location;
m_Transform.translation = location;
}

0 comments on commit d7485d3

Please sign in to comment.