forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntitySystem.hpp
997 lines (872 loc) · 33.3 KB
/
EntitySystem.hpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
#pragma once
#include <Ogre.h>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
#include <set>
#include <cstdlib>
#include <array>
#include "System.hpp"
#include "Components.hpp"
#include "lppscript/LppScript.hpp"
#include "Helpers.hpp"
#include "Util.hpp"
#include "Player.hpp"
/**
* The EntitySystem class handles everything related to entities, like addition and removal of components,
* testing if an entity has a component or retrieval of components belonging to particular entities.
*/
class EntitySystem : public System
{
friend class util::EntityDestroyer;
typedef void (EntitySystem::*LoaderFuncPtr)(std::size_t, const std::string&);
typedef void (EntitySystem::*AdderFuncPtr)(std::size_t);
typedef void (EntitySystem::*DeleterFuncPtr)(std::size_t);
typedef void (EntitySystem::*ImmediateDeleterFuncPtr)(std::size_t);
public:
/**
* Brief: Constructor.
* Param: Reference to the game's scene manager used to create nodes and entities.
*/
EntitySystem(Ogre::SceneManager&);
/**
* Brief: Destructor.
*/
~EntitySystem() { /* DUMMY BODY */ }
/**
* Brief: Checks for entities with no components and if any are found, deletes
* them.
* Param: Time since the last frame.
*/
void update(Ogre::Real);
/**
* Brief: Returns first available entity id.
*/
std::size_t get_new_id();
/**
* Brief: Removes all entities that have no components and individual components marked
* for deletion from their entities (this is used so that the Lua code does not
* delete an entity/a component from a container while C++ iterates over it).
*/
void cleanup();
/**
* Brief: Creates a new entity from a blueprint.
* Param: Name of the Lua table containing the entity blueprint.
*/
std::size_t create_entity(std::string = "");
/**
* Breif: Returns const reference to the component list, so that it can
* be used to iterate over all entities.
*/
const std::map<std::size_t, std::bitset<Component::count>>& get_component_list() const;
/**
* Brief: Tests whether a given entity has a component specialized by the
* template argument.
* Param: ID of the entity being checked.
* Note: VS2015RC does not let me use variadic templates with recursion to check multiple
* components for some reason, investigate!
*/
template<typename COMP>
bool has_component(std::size_t id)
{
return get_component_container<COMP>().find(id) !=
get_component_container<COMP>().end();
}
/**
* Brief: Tests whether a given entity has a component of a given type (used from Lua as
* it cannot use templates).
* Param: ID of the entity.
* Param: Type of the component.
*/
bool has_component(std::size_t, std::size_t) const;
/**
* Brief: Returns a bool-component pointer pair, in which the first bool member determines if the
* component was found and the second is a pointer to the component.
* Param: ID of the entity whose component we ask for.
*/
template<typename COMP>
COMP* get_component(std::size_t id)
{
auto it = get_component_container<COMP>().find(id);
if(it != get_component_container<COMP>().end())
return &it->second;
else
return nullptr;
}
/**
* Brief: Changes a component (type specified by template argument) of and entity or assigns a new
* component it that entity didn't have it.
* Param: ID of the entity.
* Param: Component to be assigned.
*/
template<typename COMP>
void set_component(std::size_t id, COMP comp)
{
auto it = get_component_container<COMP>().find(id);
if(it != get_component_container<COMP>().end())
it->second = comp;
else
{
get_component_container<COMP>().emplace(std::make_pair(id, std::move(comp)));
entities_[id].set(COMP::type); // Notify of the presence of this new component.
}
}
/**
* Brief: Returns the map associated with the component specified by the template argument.
*/
template<typename COMP>
std::map<std::size_t, COMP>& get_component_container();
/**
* Brief: Adds a components to the given enetity using it's default constructor (all values have
* to be set afterwards).
* Param: ID of the entity.
*/
template<typename COMP>
void add_component(std::size_t id)
{
auto res = get_component_container<COMP>().emplace(std::make_pair(id, COMP{}));
// Set the flag.
auto it = entities_.find(id);
if(it != entities_.end())
it->second.set(COMP::type, true);
}
/**
* Brief: Allows to add a component based on it's ID.
* Param: ID of the entity.
* Param: ID of the component.
*/
void add_component(std::size_t, int);
/**
* Brief: Marks a component (specified by template argument) for given entity for deletion.
* Param: ID of the entity.
*/
template<typename COMP>
void delete_component(std::size_t id)
{
components_to_be_removed_.push_back(std::make_pair(id, COMP::type));
}
/**
* Brief: Allows to enqueue a component for deletion based on it's ID.
* Param: ID of the entity.
* Param: ID of the component.
*/
void delete_component(std::size_t, int);
/**
* Brief: Registers an entity that has been loaded from a Lua script.
* (If it has been registered previously, the register ignores it.)
* Param: Name of the table containing the info about the entity.
*/
void register_entity(const std::string&);
/**
* Brief: Returns a reference to the set containing all entity tables registered
* during the game's runtime.
*/
std::set<std::string>& get_registered_entities();
/**
* Brief: Checks if a given entity exists and returns true if it does, false otherwise.
* Param: ID of the entity.
*/
bool exists(std::size_t) const;
/**
* Brief: Returns a reference to the scene manager all entities of this system are
* attached to (if they have a graphics component).
*/
Ogre::SceneManager& get_scene_manager() { return scene_; }
/**
* Brief: Deletes all entities int the game, used before loading a new game.
*/
void delete_entities();
/**
* Used in helpers when no component exists and we still need to return
* the blueprint name (in this case the ERROR blueprint) by reference.
*/
std::string NO_BLUEPRINT{"ERROR"};
/**
* Used in when translating the faction enum to a string in the FactionHelper.
*/
std::array<std::string, 3> FACTION_NAME{"FRIENDLY", "ENEMY", "NEUTRAL"};
private:
/**
* Brief: Loads a component from a Lua script.
* Param: ID of the entity.
* Param: Name of the table containing the component.
*/
template<typename COMP>
void load_component(std::size_t id, const std::string& table_name);
/**
* Brief: Removes an entity from the system, thus killing/destroying it.
* Param: ID of the entity.
*/
void destroy_entity(std::size_t);
/**
* Brief: Deletes a component.
* Param: ID of the entity.
*/
template<typename COMP>
void delete_component_now(std::size_t id)
{
auto ent = entities_.find(id);
if(ent != entities_.end())
ent->second.set(COMP::type, false);
clean_up_component<COMP>(id);
get_component_container<COMP>().erase(id);
}
/**
* Brief: Deletes a component.
* Param: ID of the entity.
* Param: ID of the component.
*/
void delete_component_now(std::size_t, int);
/**
* Brief: Initializes all arrays holding pointers to the component
* manipulating methods.
*/
void init_function_arrays();
/**
* Brief: Deletes all necessary data when destroying a component (like Ogre
* related objects, other entities, tasks etc.).
* Param: ID of the entity.
*/
template<typename COMP>
void clean_up_component(std::size_t)
{ /* DUMMY BODY */ }
/**
* Contains bitsets describing component availability.
*/
std::map<std::size_t, std::bitset<Component::count>> entities_;
/**
* Used to mark components or entire entities for removal.
*/
std::vector<std::size_t> to_be_destroyed_;
std::vector<std::pair<std::size_t, int>> components_to_be_removed_;
/**
* Contain components specified by the entity ID.
* Initialized here to avoid a long initializing list in the constructor.
*/
std::map<std::size_t, PhysicsComponent> physics_{};
std::map<std::size_t, HealthComponent> health_{};
std::map<std::size_t, AIComponent> ai_{};
std::map<std::size_t, GraphicsComponent> graphics_{};
std::map<std::size_t, MovementComponent> movement_{};
std::map<std::size_t, CombatComponent> combat_{};
std::map<std::size_t, EventComponent> event_{};
std::map<std::size_t, InputComponent> input_{};
std::map<std::size_t, TimeComponent> time_{};
std::map<std::size_t, ManaComponent> mana_{};
std::map<std::size_t, SpellComponent> spell_{};
std::map<std::size_t, ProductionComponent> production_{};
std::map<std::size_t, GridNodeComponent> grid_node_{};
std::map<std::size_t, ProductComponent> product_{};
std::map<std::size_t, PathfindingComponent> pathfinding_{};
std::map<std::size_t, TaskComponent> task_{};
std::map<std::size_t, TaskHandlerComponent> task_handler_{};
std::map<std::size_t, StructureComponent> structure_{};
std::map<std::size_t, HomingComponent> homing_{};
std::map<std::size_t, EventHandlerComponent> event_handler_{};
std::map<std::size_t, DestructorComponent> destructor_{};
std::map<std::size_t, GoldComponent> gold_{};
std::map<std::size_t, FactionComponent> faction_{};
std::map<std::size_t, PriceComponent> price_{};
std::map<std::size_t, AlignComponent> align_{};
std::map<std::size_t, MineComponent> mine_{};
std::map<std::size_t, ManaCrystalComponent> mana_crystal_{};
std::map<std::size_t, OnHitComponent> on_hit_{};
std::map<std::size_t, ConstructorComponent> constructor_{};
std::map<std::size_t, TriggerComponent> trigger_{};
std::map<std::size_t, UpgradeComponent> upgrade_{};
std::map<std::size_t, NotificationComponent> notification_{};
std::map<std::size_t, ExplosionComponent> explosion_{};
std::map<std::size_t, LimitedLifeSpanComponent> limited_life_span_{};
std::map<std::size_t, NameComponent> name_{};
std::map<std::size_t, ExperienceValueComponent> exp_value_{};
/**
* Reference to the game's scene manager used to create nodes and entities.
*/
Ogre::SceneManager& scene_;
/**
* Contains the names of all loaded entity tables.
*/
std::set<std::string> entity_register_;
/**
* These arrays contain pointers to the component managment methods for easier
* use when Lua interacts with C++, since Lua doesn't know anything about C++
* types and templates.
*/
std::array<LoaderFuncPtr, Component::count> loaders_{};
std::array<AdderFuncPtr, Component::count> adders_{};
std::array<DeleterFuncPtr, Component::count> deleters_{};
std::array<ImmediateDeleterFuncPtr, Component::count> immediate_deleters_{};
/**
* Keeps track of the highest ID given to an entity.
*/
std::size_t curr_id_{};
};
/**
* Specializations of the EntitySystem::get_component_container method.
*/
template<>
inline std::map<std::size_t, PhysicsComponent>& EntitySystem::get_component_container<PhysicsComponent>()
{
return physics_;
}
template<>
inline std::map<std::size_t, HealthComponent>& EntitySystem::get_component_container<HealthComponent>()
{
return health_;
}
template<>
inline std::map<std::size_t, AIComponent>& EntitySystem::get_component_container<AIComponent>()
{
return ai_;
}
template<>
inline std::map<std::size_t, GraphicsComponent>& EntitySystem::get_component_container<GraphicsComponent>()
{
return graphics_;
}
template<>
inline std::map<std::size_t, MovementComponent>& EntitySystem::get_component_container<MovementComponent>()
{
return movement_;
}
template<>
inline std::map<std::size_t, CombatComponent>& EntitySystem::get_component_container<CombatComponent>()
{
return combat_;
}
template<>
inline std::map<std::size_t, EventComponent>& EntitySystem::get_component_container<EventComponent>()
{
return event_;
}
template<>
inline std::map<std::size_t, InputComponent>& EntitySystem::get_component_container<InputComponent>()
{
return input_;
}
template<>
inline std::map<std::size_t, TimeComponent>& EntitySystem::get_component_container<TimeComponent>()
{
return time_;
}
template<>
inline std::map<std::size_t, ManaComponent>& EntitySystem::get_component_container<ManaComponent>()
{
return mana_;
}
template<>
inline std::map<std::size_t, SpellComponent>& EntitySystem::get_component_container<SpellComponent>()
{
return spell_;
}
template<>
inline std::map<std::size_t, ProductionComponent>& EntitySystem::get_component_container<ProductionComponent>()
{
return production_;
}
template<>
inline std::map<std::size_t, GridNodeComponent>& EntitySystem::get_component_container<GridNodeComponent>()
{
return grid_node_;
}
template<>
inline std::map<std::size_t, ProductComponent>& EntitySystem::get_component_container<ProductComponent>()
{
return product_;
}
template<>
inline std::map<std::size_t, PathfindingComponent>& EntitySystem::get_component_container<PathfindingComponent>()
{
return pathfinding_;
}
template<>
inline std::map<std::size_t, TaskComponent>& EntitySystem::get_component_container<TaskComponent>()
{
return task_;
}
template<>
inline std::map<std::size_t, TaskHandlerComponent>& EntitySystem::get_component_container<TaskHandlerComponent>()
{
return task_handler_;
}
template<>
inline std::map<std::size_t, StructureComponent>& EntitySystem::get_component_container<StructureComponent>()
{
return structure_;
}
template<>
inline std::map<std::size_t, HomingComponent>& EntitySystem::get_component_container<HomingComponent>()
{
return homing_;
}
template<>
inline std::map<std::size_t, EventHandlerComponent>& EntitySystem::get_component_container<EventHandlerComponent>()
{
return event_handler_;
}
template<>
inline std::map<std::size_t, DestructorComponent>& EntitySystem::get_component_container<DestructorComponent>()
{
return destructor_;
}
template<>
inline std::map<std::size_t, GoldComponent>& EntitySystem::get_component_container<GoldComponent>()
{
return gold_;
}
template<>
inline std::map<std::size_t, FactionComponent>& EntitySystem::get_component_container<FactionComponent>()
{
return faction_;
}
template<>
inline std::map<std::size_t, PriceComponent>& EntitySystem::get_component_container<PriceComponent>()
{
return price_;
}
template<>
inline std::map<std::size_t, AlignComponent>& EntitySystem::get_component_container<AlignComponent>()
{
return align_;
}
template<>
inline std::map<std::size_t, MineComponent>& EntitySystem::get_component_container<MineComponent>()
{
return mine_;
}
template<>
inline std::map<std::size_t, ManaCrystalComponent>& EntitySystem::get_component_container<ManaCrystalComponent>()
{
return mana_crystal_;
}
template<>
inline std::map<std::size_t, OnHitComponent>& EntitySystem::get_component_container<OnHitComponent>()
{
return on_hit_;
}
template<>
inline std::map<std::size_t, ConstructorComponent>& EntitySystem::get_component_container<ConstructorComponent>()
{
return constructor_;
}
template<>
inline std::map<std::size_t, TriggerComponent>& EntitySystem::get_component_container<TriggerComponent>()
{
return trigger_;
}
template<>
inline std::map<std::size_t, UpgradeComponent>& EntitySystem::get_component_container<UpgradeComponent>()
{
return upgrade_;
}
template<>
inline std::map<std::size_t, NotificationComponent>& EntitySystem::get_component_container<NotificationComponent>()
{
return notification_;
}
template<>
inline std::map<std::size_t, ExplosionComponent>& EntitySystem::get_component_container<ExplosionComponent>()
{
return explosion_;
}
template<>
inline std::map<std::size_t, LimitedLifeSpanComponent>& EntitySystem::get_component_container<LimitedLifeSpanComponent>()
{
return limited_life_span_;
}
template<>
inline std::map<std::size_t, NameComponent>& EntitySystem::get_component_container<NameComponent>()
{
return name_;
}
template<>
inline std::map<std::size_t, ExperienceValueComponent>& EntitySystem::get_component_container<ExperienceValueComponent>()
{
return exp_value_;
}
/**
* Specializations of the EntitySystem::load_component method.
* Note: Following components can only be created manually and thus don't have load_component specialization.
* GridNodeComponent (created by GridSystem::add_node)
* ProductComponent (production id is assigned during runtime)
* TaskComponent (tasks are specified by their types and are added through the TaskHelper)
*/
template<>
inline void EntitySystem::load_component<PhysicsComponent>(std::size_t id, const std::string& table_name)
{
lpp::Script& script = lpp::Script::get_singleton();
bool solid = script.get<bool>(table_name + ".PhysicsComponent.solid");
physics_.emplace(id, PhysicsComponent{solid});
}
template<>
inline void EntitySystem::load_component<HealthComponent>(std::size_t id, const std::string& table_name)
{
lpp::Script& script = lpp::Script::get_singleton();
int max = script.get<int>(table_name + ".HealthComponent.max_hp");
int reg = script.get<int>(table_name + ".HealthComponent.regen");
int def = script.get<int>(table_name + ".HealthComponent.defense");
health_.emplace(id, HealthComponent(max, reg, def));
}
template<>
inline void EntitySystem::load_component<AIComponent>(std::size_t id, const std::string& table_name)
{
lpp::Script& script = lpp::Script::get_singleton();
std::string blueprint = script.get<std::string>(table_name + ".AIComponent.blueprint");
ai_.emplace(id, AIComponent{std::move(blueprint)});
}
template<>
inline void EntitySystem::load_component<GraphicsComponent>(std::size_t id, const std::string& table_name)
{ // TODO: Improve this ...
lpp::Script& script = lpp::Script::get_singleton();
std::string mesh = script.get<std::string>(table_name + ".GraphicsComponent.mesh");
std::string material = script.get<std::string>(table_name + ".GraphicsComponent.material");
auto res = graphics_.emplace(id, GraphicsComponent{std::move(mesh), std::move(material)});
// Ogre init of the entity and scene node.
auto& comp = res.first->second;
comp.node = scene_.getRootSceneNode()->createChildSceneNode("entity_" + std::to_string(id));
comp.entity = scene_.createEntity(comp.mesh);
comp.node->attachObject(comp.entity);
comp.entity->setQueryFlags(1);
if(!script.get<bool>(table_name + ".GraphicsComponent.visible"))
{
comp.visible = false;
comp.node->setVisible(false);
}
if(script.get<bool>(table_name + ".GraphicsComponent.manual_scaling"))
{
comp.manual_scaling = true;
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");
comp.scale = Ogre::Vector3{x, y, z};
comp.node->setScale(comp.scale); // TODO: Apply to entity placer!
}
if(comp.material != "NO_MAT")
comp.entity->setMaterialName(comp.material);
// Make the entity stand on ground.
auto half_height = comp.entity->getWorldBoundingBox(true).getHalfSize().y;
auto phys_comp = get_component<PhysicsComponent>(id);
if(phys_comp)
{
phys_comp->half_height = half_height;
phys_comp->position = Ogre::Vector3{phys_comp->position.x, half_height, phys_comp->position.z};
comp.node->setPosition(phys_comp->position);
}
// This will allow specific querying.
if(!script.is_nil(table_name + ".GraphicsComponent.query_flags"))
comp.entity->setQueryFlags(script.get<int>(table_name + ".GraphicsComponent.query_flags"));
}
template<>
inline void EntitySystem::load_component<MovementComponent>(std::size_t id, const std::string& table_name)
{
lpp::Script& script = lpp::Script::get_singleton();
float speed = script.get<float>(table_name + ".MovementComponent.speed_modifier");
movement_.emplace(id, MovementComponent{speed});
}
template<>
inline void EntitySystem::load_component<CombatComponent>(std::size_t id, const std::string& table_name)
{
lpp::Script& script = lpp::Script::get_singleton();
Ogre::Real range = script.get<Ogre::Real>(table_name + ".CombatComponent.range");
Ogre::Real cd = script.get<Ogre::Real>(table_name + ".CombatComponent.cooldown");
std::size_t min = script.get<std::size_t>(table_name + ".CombatComponent.min_dmg");
std::size_t max = script.get<std::size_t>(table_name + ".CombatComponent.max_dmg");
bool pursue = script.get<bool>(table_name + ".CombatComponent.pursue");
int type = script.get<int>(table_name + ".CombatComponent.type");
combat_.emplace(id, CombatComponent(Component::NO_ENTITY, min, max, cd, range, type, pursue));
}
template<>
inline void EntitySystem::load_component<EventComponent>(std::size_t id, const std::string& table_name)
{
lpp::Script& script = lpp::Script::get_singleton();
EVENT_TYPE type = (EVENT_TYPE)script.get<int>(table_name + ".EventComponent.type");
std::size_t target = script.get<std::size_t>(table_name + ".EventComponent.target");
Ogre::Real radius = script.get<Ogre::Real>(table_name + ".EventComponent.radius");
bool active = script.get<bool>(table_name + ".EventComponent.active");
event_.emplace(id, EventComponent{type, target, radius, active});
}
template<>
inline void EntitySystem::load_component<InputComponent>(std::size_t id, const std::string& table_name)
{
std::string handler = lpp::Script::get_singleton().get<std::string>(table_name + ".InputComponent.input_handler");
input_.emplace(id, InputComponent{std::move(handler)});
}
template<>
inline void EntitySystem::load_component<TimeComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
int type = script.get<int>(table_name + ".TimeComponent.type");
Ogre::Real time_limit = script.get<Ogre::Real>(table_name + ".TimeComponent.time_limit");
std::size_t target = script.get<std::size_t>(table_name + ".TimeComponent.target");
time_.emplace(id, TimeComponent{(TIME_EVENT)type, time_limit, target});
}
template<>
inline void EntitySystem::load_component<ProductionComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::string blueprint = script.get<std::string>(table_name + ".ProductionComponent.blueprint");
std::size_t limit = script.get<std::size_t>(table_name + ".ProductionComponent.limit");
Ogre::Real cd = script.get<Ogre::Real>(table_name + ".ProductionComponent.cooldown");
production_.emplace(id, ProductionComponent{std::move(blueprint), limit, cd});
if(!script.is_nil(table_name + ".FactionComponent") &&
script.get<std::size_t>(table_name + ".FactionComponent.faction") == (std::size_t)FACTION::FRIENDLY)
Player::instance().add_max_unit(limit);
}
template<>
inline void EntitySystem::load_component<PathfindingComponent>(std::size_t id, const std::string& table_name)
{
std::string blueprint = lpp::Script::get_singleton().get<std::string>(table_name + ".PathfindingComponent.blueprint");
pathfinding_.emplace(id, PathfindingComponent{std::move(blueprint)});
}
template<>
inline void EntitySystem::load_component<TaskHandlerComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::vector<int> possible_tasks = script.get_vector<int>(table_name + ".TaskHandlerComponent.possible_tasks");
std::string blueprint = script.get<std::string>(table_name + ".TaskHandlerComponent.blueprint");
auto res = task_handler_.emplace(id, TaskHandlerComponent{std::move(blueprint)});
// Init possible tasks.
auto& tasks = res.first->second.possible_tasks;
for(auto task_type : possible_tasks)
tasks.set(task_type);
}
template<>
inline void EntitySystem::load_component<StructureComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::size_t radius = script.get<std::size_t>(table_name + ".StructureComponent.radius");
bool walk_through{false};
if(!script.is_nil(table_name + ".StructureComponent.walk_through"))
walk_through = script.get<bool>(table_name + ".StructureComponent.walk_through");
structure_.emplace(id, StructureComponent{radius, walk_through});
}
template<>
inline void EntitySystem::load_component<HomingComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::size_t source = script.get<std::size_t>(table_name + ".HomingComponent.source");
std::size_t target = script.get<std::size_t>(table_name + ".HomingComponent.target");
std::size_t dmg = script.get<std::size_t>(table_name + ".HomingComponent.damage");
homing_.emplace(id, HomingComponent{source, target, dmg});
}
template<>
inline void EntitySystem::load_component<EventHandlerComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::string handler = script.get<std::string>(table_name + ".EventHandlerComponent.handler");
auto res = event_handler_.emplace(id, EventHandlerComponent{std::move(handler)});
if(!res.second)
return; // TODO: Notify.
auto& comp = res.first->second;
auto possible_events = script.get_vector<int>(table_name + ".EventHandlerComponent.possible_events");
for(const auto& evt : possible_events)
comp.possible_events.set(evt);
}
template<>
inline void EntitySystem::load_component<DestructorComponent>(std::size_t id, const std::string& table_name)
{
std::string blueprint = lpp::Script::get_singleton().get<std::string>(table_name + ".DestructorComponent.blueprint");
destructor_.emplace(id, DestructorComponent{blueprint});
}
template<>
inline void EntitySystem::load_component<GoldComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::size_t curr = script.get<std::size_t>(table_name + ".GoldComponent.curr");
std::size_t max = script.get<std::size_t>(table_name + ".GoldComponent.max");
gold_.emplace(id, GoldComponent{max, curr});
}
template<>
inline void EntitySystem::load_component<FactionComponent>(std::size_t id, const std::string& table_name)
{
FACTION fac = (FACTION)lpp::Script::get_singleton().get<int>(table_name + ".FactionComponent.faction");
faction_.emplace(id, FactionComponent{fac});
}
template<>
inline void EntitySystem::load_component<PriceComponent>(std::size_t id, const std::string& table_name)
{
std::size_t price = lpp::Script::get_singleton().get<std::size_t>(table_name + ".PriceComponent.price");
price_.emplace(id, PriceComponent{price});
}
template<>
inline void EntitySystem::load_component<AlignComponent>(std::size_t id, const std::string& table_name)
{
auto res = align_.emplace(id, AlignComponent{});
if(!res.second)
return; // TODO: Notify.
auto& comp = res.first->second;
std::string state_table{};
auto& script = lpp::Script::get_singleton();
for(std::size_t i = 0; i < AlignComponent::state_count; ++i)
{
state_table = table_name + ".AlignComponent.state_" + std::to_string(i);
comp.states[i].material = script.get<std::string>(state_table + ".material");
comp.states[i].mesh = script.get<std::string>(state_table + ".mesh");;
comp.states[i].position_offset.x = script.get<Ogre::Real>(state_table + ".position_offset_x");;
comp.states[i].position_offset.y = script.get<Ogre::Real>(state_table + ".position_offset_y");;
comp.states[i].position_offset.z = script.get<Ogre::Real>(state_table + ".position_offset_z");;
comp.states[i].scale.x = script.get<Ogre::Real>(state_table + ".scale_x");;
comp.states[i].scale.y = script.get<Ogre::Real>(state_table + ".scale_y");;
comp.states[i].scale.z = script.get<Ogre::Real>(state_table + ".scale_z");;
}
}
template<>
inline void EntitySystem::load_component<MineComponent>(std::size_t id, const std::string& table_name)
{
mine_.emplace(id, MineComponent{});
}
template<>
inline void EntitySystem::load_component<ManaCrystalComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::size_t cap = script.get<std::size_t>(table_name + ".ManaCrystalComponent.cap_increase");
std::size_t regen = script.get<std::size_t>(table_name + ".ManaCrystalComponent.regen_increase");
mana_crystal_.emplace(id, ManaCrystalComponent{cap, regen});
Player::instance().add_max_mana(cap);
Player::instance().add_mana_regen(regen);
}
template<>
inline void EntitySystem::load_component<OnHitComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::string blueprint = script.get<std::string>(table_name + ".OnHitComponent.blueprint");
Ogre::Real cd = script.get<Ogre::Real>(table_name + ".OnHitComponent.cooldown");
on_hit_.emplace(id, OnHitComponent{std::move(blueprint), cd});
}
template<>
inline void EntitySystem::load_component<ConstructorComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::string blueprint = script.get<std::string>(table_name + ".ConstructorComponent.blueprint");
constructor_.emplace(id, ConstructorComponent{std::move(blueprint)});
}
template<>
inline void EntitySystem::load_component<TriggerComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::string blueprint = script.get<std::string>(table_name + ".TriggerComponent.blueprint");
Ogre::Real cd = script.get<Ogre::Real>(table_name + ".TriggerComponent.cooldown");
Ogre::Real radius = script.get<Ogre::Real>(table_name + ".TriggerComponent.radius");
trigger_.emplace(id, TriggerComponent{std::move(blueprint), cd, radius});
}
template<>
inline void EntitySystem::load_component<UpgradeComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::string blueprint = script.get<std::string>(table_name + ".UpgradeComponent.blueprint");
std::size_t exp = script.get<std::size_t>(table_name + ".UpgradeComponent.exp_needed");
std::size_t cap = script.get<std::size_t>(table_name + ".UpgradeComponent.level_cap");
upgrade_.emplace(id, UpgradeComponent{std::move(blueprint), exp, cap});
}
template<>
inline void EntitySystem::load_component<NotificationComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
Ogre::Real cd = script.get<Ogre::Real>(table_name + ".NotificationComponent.cooldown");
notification_.emplace(id, NotificationComponent{cd});
}
template<>
inline void EntitySystem::load_component<ExplosionComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
Ogre::Real delta = script.get<Ogre::Real>(table_name + ".ExplosionComponent.delta");
Ogre::Real radius = script.get<Ogre::Real>(table_name + ".ExplosionComponent.radius");
explosion_.emplace(id, ExplosionComponent{delta, radius});
}
template<>
inline void EntitySystem::load_component<LimitedLifeSpanComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
Ogre::Real max = script.get<Ogre::Real>(table_name + ".LimitedLifeSpanComponent.max_time");
limited_life_span_.emplace(id, LimitedLifeSpanComponent{max});
}
template<>
inline void EntitySystem::load_component<NameComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::string name = script.get<std::string>(table_name + ".NameComponent.name");
name_.emplace(id, NameComponent{std::move(name)});
}
template<>
inline void EntitySystem::load_component<ExperienceValueComponent>(std::size_t id, const std::string& table_name)
{
auto& script = lpp::Script::get_singleton();
std::size_t val = script.get<std::size_t>(table_name + ".ExperienceValueComponent.value");
exp_value_.emplace(id, ExperienceValueComponent{val});
}
/**
* Specializations of the EntitySystem::clean_up_component method.
*/
template<>
inline void EntitySystem::clean_up_component<GraphicsComponent>(std::size_t id)
{
auto graph_comp = get_component<GraphicsComponent>(id);
if(graph_comp && graph_comp->node && graph_comp->entity)
{
graph_comp->node->detachObject(graph_comp->entity);
scene_.destroyEntity(graph_comp->entity);
if(graph_comp->node->numChildren() != 0)
graph_comp->node->removeAndDestroyAllChildren();
scene_.destroySceneNode(graph_comp->node);
}
}
template<>
inline void EntitySystem::clean_up_component<TaskHandlerComponent>(std::size_t id)
{
auto comp = get_component<TaskHandlerComponent>(id);
if(comp)
{ // Destroy all assigned tasks.
if(comp->curr_task != Component::NO_ENTITY)
to_be_destroyed_.push_back(comp->curr_task);
for(auto task : comp->task_queue)
to_be_destroyed_.push_back(task);
}
}
template<>
inline void EntitySystem::clean_up_component<StructureComponent>(std::size_t id)
{
auto comp = get_component<StructureComponent>(id);
if(comp)
{ // Free all obstructed nodes.
for(auto& residence : comp->residences)
{
auto node = get_component<GridNodeComponent>(residence);
if(node && node->resident == id)
GridNodeHelper::set_free(*this, residence, true);
}
}
}
template<>
inline void EntitySystem::clean_up_component<ProductComponent>(std::size_t id)
{
auto comp = get_component<ProductComponent>(id);
if(comp)
{
auto producent = get_component<ProductionComponent>(comp->producer);
if(producent)
--producent->curr_produced;
}
auto fac = get_component<FactionComponent>(id);
if(fac && fac->faction == FACTION::FRIENDLY)
Player::instance().sub_curr_unit(1);
}
template<>
inline void EntitySystem::clean_up_component<ProductionComponent>(std::size_t id)
{
auto comp = get_component<ProductionComponent>(id);
auto fac = get_component<FactionComponent>(id);
if(comp && fac && fac->faction == FACTION::FRIENDLY)
Player::instance().sub_max_unit(comp->max_produced);
}
template<>
inline void EntitySystem::clean_up_component<ManaCrystalComponent>(std::size_t id)
{
auto comp = get_component<ManaCrystalComponent>(id);
if(comp)
{
Player::instance().sub_max_mana(comp->cap_increase);
Player::instance().sub_mana_regen(comp->regen_increase);
}
}