forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityPlacer.cpp
More file actions
151 lines (129 loc) · 4.35 KB
/
EntityPlacer.cpp
File metadata and controls
151 lines (129 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "EntityPlacer.hpp"
#include "GUI.hpp"
EntityPlacer::EntityPlacer(EntitySystem& ents, GridSystem& grid, Ogre::SceneManager& mgr)
: entities_{ents}, grid_{grid}, curr_position_{0, 0, 0},
placing_node_{mgr.getRootSceneNode()->createChildSceneNode()}, visible_{false}, table_name_{},
half_height_{}, placing_structure_{false}, structure_radius_{0}, mgr_{mgr},
ent_{nullptr}, price_{0}
{ /* DUMMY BODY */ }
EntityPlacer::~EntityPlacer()
{
if(placing_node_)
mgr_.destroySceneNode(placing_node_);
if(ent_)
mgr_.destroyEntity(ent_);
}
void EntityPlacer::set_current_entity_table(const std::string& table_name, bool use_price)
{
if(entities_.get_registered_entities().count(table_name) == 0)
return;
auto& script = lpp::Script::get_singleton();
if(script.is_nil(table_name + ".GraphicsComponent"))
return;
table_name_ = table_name;
std::string mesh = script.get<std::string>(table_name + ".GraphicsComponent.mesh");
std::string mat = script.get<std::string>(table_name + ".GraphicsComponent.material");
bool man_scaling = script.get<bool>(table_name + ".GraphicsComponent.manual_scaling");
if(ent_)
{ // Recreation of the entity is required by Ogre.
mgr_.destroyEntity(ent_);
placing_node_->detachAllObjects();
placing_node_->setScale(1.f, 1.f, 1.f);
price_ = 0;
}
ent_ = mgr_.createEntity(mesh);
ent_->setQueryFlags(0);
placing_node_->attachObject(ent_);
if(mat != "NO_MAT")
ent_->setMaterialName(mat);
if(!script.is_nil(table_name + ".StructureComponent"))
{
placing_structure_ = true;
structure_radius_ = script.get<std::size_t>(table_name + ".StructureComponent.radius");
}
else
placing_structure_ = false;
if(use_price && !script.is_nil(table_name + ".PriceComponent"))
price_ = script.get<std::size_t>(table_name + ".PriceComponent.price");
if(man_scaling)
{
Ogre::Real x, y, z;
x = script.get<Ogre::Real>(table_name + ".GraphicsComponent.scale_x");
y = script.get<Ogre::Real>(table_name + ".GraphicsComponent.scale_y");
z = script.get<Ogre::Real>(table_name + ".GraphicsComponent.scale_z");
placing_node_->setScale(x, y, z);
half_height_ = y;
}
else
half_height_ = ent_->getWorldBoundingBox(true).getHalfSize().y;
}
void EntityPlacer::update_position(const Ogre::Vector3& pos)
{
if(placing_structure_)
{
auto node_id = Grid::instance().get_node_from_position(pos.x, pos.z);
if(node_id == Component::NO_ENTITY)
return;
// Snap to grid.
auto comp = entities_.get_component<PhysicsComponent>(node_id);
if(comp)
{
auto& node_pos = comp->position;
curr_position_ = Ogre::Vector3{node_pos.x, half_height_, node_pos.z};
}
}
else
curr_position_ = Ogre::Vector3{pos.x, half_height_, pos.z};
placing_node_->setPosition(curr_position_);
}
std::size_t EntityPlacer::place(Console& console)
{
if(!Player::instance().sub_gold(price_))
{
GUI::instance().get_log().print("\\[ERROR\\] Not enough gold to place that.");
return Component::NO_ENTITY;
}
std::size_t node_id = Grid::instance().get_node_from_position(curr_position_.x, curr_position_.z);
if(placing_structure_ && !GridNodeHelper::area_free(entities_, node_id, structure_radius_))
return Component::NO_ENTITY; // This will prohibit of wall/building stacking.
std::size_t id = entities_.create_entity(table_name_);
auto comp = entities_.get_component<PhysicsComponent>(id);
if(comp)
{
comp->position = Ogre::Vector3{curr_position_.x,
comp->half_height,
curr_position_.z};
auto graph_comp = entities_.get_component<GraphicsComponent>(id);
if(graph_comp)
{
graph_comp->node->setPosition(curr_position_.x, comp->half_height,
curr_position_.z);
}
}
if(placing_structure_)
grid_.place_structure(id, node_id, structure_radius_);
console.print_text("Placed entity #" + std::to_string(id) + " at (" + std::to_string(curr_position_.x)
+ ", " + std::to_string(curr_position_.z) + ")\nTable: " + table_name_);
console.scroll_down();
return id;
}
void EntityPlacer::set_visible(bool on_off)
{
visible_ = on_off;
if(placing_node_)
placing_node_->setVisible(visible_);
if(placing_structure_ && !on_off)
placing_structure_ = false;
}
bool EntityPlacer::is_visible() const
{
return visible_;
}
void EntityPlacer::toggle_placing_when_game_paused()
{
can_place_when_game_paused_ = !can_place_when_game_paused_;
}
bool EntityPlacer::can_place_when_game_paused() const
{
return can_place_when_game_paused_;
}