diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-03 16:10:44 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-03 16:10:44 -0500 |
commit | 8ffb27ab09ff567a159e5be5a243fd3967084977 (patch) | |
tree | c7c1be31a4074a0d58191b9cc5ed880271c65b91 /src/simulation.cpp | |
download | dispatcher-8ffb27ab09ff567a159e5be5a243fd3967084977.tar.gz dispatcher-8ffb27ab09ff567a159e5be5a243fd3967084977.tar.bz2 dispatcher-8ffb27ab09ff567a159e5be5a243fd3967084977.zip |
Very basic ECS
Diffstat (limited to 'src/simulation.cpp')
-rw-r--r-- | src/simulation.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/simulation.cpp b/src/simulation.cpp new file mode 100644 index 0000000..3079f56 --- /dev/null +++ b/src/simulation.cpp | |||
@@ -0,0 +1,115 @@ | |||
1 | #include "simulation.h" | ||
2 | |||
3 | #include "consts.h" | ||
4 | #include "level.h" | ||
5 | |||
6 | void Simulation::tick( | ||
7 | double dt, | ||
8 | const Uint8* keystate) | ||
9 | { | ||
10 | for (id_type id : active_) | ||
11 | { | ||
12 | Entity& entity = entities_.at(id); | ||
13 | |||
14 | // Control | ||
15 | if (entity.player && | ||
16 | !entity.moving) | ||
17 | { | ||
18 | if (keystate[SDL_SCANCODE_LEFT] && | ||
19 | entity.gridPos.x() > 0 && | ||
20 | level_.getTileset().canPlayerMoveTo( | ||
21 | level_.at(entity.gridPos - vec2s { 1, 0 }))) | ||
22 | { | ||
23 | entity.moving = true; | ||
24 | entity.destPos = entity.gridPos - vec2s { 1, 0 }; | ||
25 | } | ||
26 | else if (keystate[SDL_SCANCODE_UP] && | ||
27 | entity.gridPos.y() > 0 && | ||
28 | level_.getTileset().canPlayerMoveTo( | ||
29 | level_.at(entity.gridPos - vec2s { 0, 1 }))) | ||
30 | { | ||
31 | entity.moving = true; | ||
32 | entity.destPos = entity.gridPos - vec2s { 0, 1 }; | ||
33 | } else if (keystate[SDL_SCANCODE_RIGHT] && | ||
34 | entity.gridPos.x() < (level_.getSize().w() - 1) && | ||
35 | level_.getTileset().canPlayerMoveTo( | ||
36 | level_.at(entity.gridPos + vec2s { 1, 0 }))) | ||
37 | { | ||
38 | entity.moving = true; | ||
39 | entity.destPos = entity.gridPos + vec2s { 1, 0 }; | ||
40 | } | ||
41 | else if (keystate[SDL_SCANCODE_DOWN] && | ||
42 | entity.gridPos.y() < (level_.getSize().h() - 1) && | ||
43 | level_.getTileset().canPlayerMoveTo( | ||
44 | level_.at(entity.gridPos + vec2s { 0, 1 }))) | ||
45 | { | ||
46 | entity.moving = true; | ||
47 | entity.destPos = entity.gridPos + vec2s { 0, 1 }; | ||
48 | } | ||
49 | |||
50 | if (entity.moving) | ||
51 | { | ||
52 | entity.movementTween = 0.0; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | |||
57 | |||
58 | |||
59 | // Collision | ||
60 | |||
61 | |||
62 | |||
63 | |||
64 | // Movement | ||
65 | if (entity.moving) | ||
66 | { | ||
67 | entity.movementTween += entity.speed * dt; | ||
68 | |||
69 | if (entity.movementTween >= 1.0) | ||
70 | { | ||
71 | entity.moving = false; | ||
72 | entity.gridPos = entity.destPos; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | if (entity.moving) | ||
77 | { | ||
78 | entity.pos.x() = | ||
79 | TILE_SIZE.x() * entity.destPos.x() * entity.movementTween + | ||
80 | TILE_SIZE.x() * entity.gridPos.x() * (1.0 - entity.movementTween); | ||
81 | |||
82 | entity.pos.y() = | ||
83 | TILE_SIZE.y() * entity.destPos.y() * entity.movementTween + | ||
84 | TILE_SIZE.y() * entity.gridPos.y() * (1.0 - entity.movementTween); | ||
85 | } else { | ||
86 | entity.pos = TILE_SIZE * entity.gridPos; | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | Simulation::id_type Simulation::emplaceEntity() | ||
92 | { | ||
93 | id_type nextId; | ||
94 | |||
95 | if (!available_.empty()) | ||
96 | { | ||
97 | nextId = available_.front(); | ||
98 | available_.pop_front(); | ||
99 | |||
100 | entities_.at(nextId) = Entity(); | ||
101 | } else { | ||
102 | nextId = entities_.size(); | ||
103 | entities_.emplace_back(); | ||
104 | } | ||
105 | |||
106 | active_.insert(nextId); | ||
107 | |||
108 | return nextId; | ||
109 | } | ||
110 | |||
111 | void Simulation::deleteEntity(id_type id) | ||
112 | { | ||
113 | available_.push_back(id); | ||
114 | active_.erase(id); | ||
115 | } | ||