-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderSystem.cpp
More file actions
218 lines (193 loc) · 10.6 KB
/
Copy pathRenderSystem.cpp
File metadata and controls
218 lines (193 loc) · 10.6 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
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
#include "RenderSystem.h"
void RenderSystem::Update(){
//clears the screen and then draws all the necessary things on screen
ak_->ClearScreen();
ak_->DrawScaledBitmap(Sprite::SPRT_BACKGROUND,0,0,BACKGROUND_WIDTH,BACKGROUND_HEIGHT,0,0,SCREEN_WIDTH, SCREEN_HEIGHT);
EntityStream es = engine_->GetEntityStream();
std::set<Entity*> entities;
entities = es.WithTag(Component::SPRITE);
for(Entity* entity: entities){
Component* component = entity->GetComponent(Component::SPRITE);
SpriteComponent* sc = dynamic_cast<SpriteComponent*>(component);
RenderSprite(sc);
}
entities = es.WithTag(Component::BOX);
for(Entity* entity: entities){
LevelElementComponent* lec = dynamic_cast<LevelElementComponent*>(entity->GetComponent(Component::LEVELELEMENT));
PositionComponent* pc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
RenderBox(pc,lec);
}
entities = es.WithTag(Component::STONE);
for(Entity* entity: entities){
LevelElementComponent* lec = dynamic_cast<LevelElementComponent*>(entity->GetComponent(Component::LEVELELEMENT));
PositionComponent* pc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
RenderStone(pc,lec);
}
entities = es.WithTag(Component::TARGET);
for(Entity* entity: entities){
LevelElementComponent* lec = dynamic_cast<LevelElementComponent*>(entity->GetComponent(Component::LEVELELEMENT));
PositionComponent* pc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
TargetComponent* tc = dynamic_cast<TargetComponent*>(entity->GetComponent(Component::TARGET));
RenderTarget(pc,lec,tc);
}
entities = es.WithTag(Component::MISSILE1);
for(Entity* entity: entities){
Component* component = entity->GetComponent(Component::POSITION);
PositionComponent* pc = dynamic_cast<PositionComponent*>(component);
RenderMissile1(pc);
}
entities = es.WithTag(Component::MISSILE2);
for(Entity* entity: entities){
Component* component = entity->GetComponent(Component::POSITION);
PositionComponent* pc = dynamic_cast<PositionComponent*>(component);
Missile2Component* mc = dynamic_cast<Missile2Component*>(entity->GetComponent(Component::MISSILE2));
RenderMissile2(pc,mc);
}
entities = es.WithTag(Component::MISSILE3);
for(Entity* entity: entities){
Component* component = entity->GetComponent(Component::POSITION);
PositionComponent* pc = dynamic_cast<PositionComponent*>(component);
RenderMissile3(pc);
}
entities = es.WithTag(Component::EXPLOSIONEFFECT);
for(Entity* entity: entities){
ExplosionEffectComponent* eec = dynamic_cast<ExplosionEffectComponent*>(entity->GetComponent(Component::EXPLOSIONEFFECT));
if(RenderExplosion(eec)){
engine_->RemoveEntity(entity);
delete entity;
}
}
if(DEBUG_MODE==true){
std::set<Component::Tag> tags = {Component::BOX,Component::STONE,Component::TARGET};
entities = es.WithTagsOR(tags);
for(Entity* entity:entities){
PositionComponent* bpc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
std::vector<Point> boxPoly = {Point(bpc->position.x_+1,SCREEN_HEIGHT-(bpc->position.y_-1)),Point(bpc->position.x_+1,SCREEN_HEIGHT-(bpc->position.y_-MISSILE_DST_HEIGHT+1)),Point(bpc->position.x_+MISSILE_DST_WIDTH-1,SCREEN_HEIGHT-(bpc->position.y_-MISSILE_DST_HEIGHT+1)),Point(bpc->position.x_+MISSILE_DST_WIDTH-1,SCREEN_HEIGHT-(bpc->position.y_-1))};
ak_->DrawPoly(boxPoly);
}
entities = es.WithTag(Component::MISSILE1);
for(Entity* entity:entities){
PositionComponent* pc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
std::vector<Point> poly = {Point(pc->position.x_,SCREEN_HEIGHT-pc->position.y_),Point(pc->position.x_,SCREEN_HEIGHT-pc->position.y_+MISSILE_DST_HEIGHT),Point(pc->position.x_+MISSILE_DST_WIDTH,SCREEN_HEIGHT-pc->position.y_+MISSILE_DST_HEIGHT),Point(pc->position.x_+MISSILE_DST_WIDTH,SCREEN_HEIGHT-pc->position.y_)};
ak_->DrawPoly(poly);
}
entities = es.WithTag(Component::MISSILE2);
for(Entity* entity:entities){
PositionComponent* pc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
std::vector<Point> poly = {Point(pc->position.x_,SCREEN_HEIGHT-pc->position.y_+8),Point(pc->position.x_,SCREEN_HEIGHT-pc->position.y_-4+MISSILE_DST_HEIGHT),Point(pc->position.x_+MISSILE_DST_WIDTH,SCREEN_HEIGHT-pc->position.y_-4+MISSILE_DST_HEIGHT),Point(pc->position.x_+MISSILE_DST_WIDTH,SCREEN_HEIGHT-pc->position.y_+8)};
ak_->DrawPoly(poly);
}
entities = es.WithTag(Component::MISSILE3);
for(Entity* entity:entities){
PositionComponent* pc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
std::vector<Point> poly = {Point(pc->position.x_+MISSILE_DST_WIDTH/2,SCREEN_HEIGHT-pc->position.y_),Point(pc->position.x_,SCREEN_HEIGHT-pc->position.y_+MISSILE_DST_HEIGHT),Point(pc->position.x_+MISSILE_DST_WIDTH,SCREEN_HEIGHT-pc->position.y_+MISSILE_DST_HEIGHT)};
ak_->DrawPoly(poly);
}
}
entities = es.WithTag(Component::MISSILEQUEUE);
bool controlflag = false;
for (Entity* entity:entities){
MissileQueueComponent* mqc = dynamic_cast<MissileQueueComponent*>(entity->GetComponent(Component::MISSILEQUEUE));
if (mqc->queuenumber==0){
PositionComponent* pc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
Render_elastic_attached(pc);
controlflag = true;
break;
}
}
if (controlflag==false){
Render_elastic_not_attached();
}
Point p(10,10);
Color c(10,10,10);
ak_->DrawString(std::string("Time: ") += (std::to_string(engine_->GetContext().timer/60)),p,c,Allkit::ALIGN_LEFT,true);
ak_->DrawOnScreen();
}
//draws the launcher
void RenderSystem::RenderSprite(SpriteComponent* sc){
ak_->DrawScaledBitmap(sc->sprite,0,0,LAUNCHER_SRC_WIDTH,LAUNCHER_SRC_HEIGHT,sc->position.x_,SCREEN_HEIGHT-sc->position.y_,LAUNCHER_DST_WIDTH,LAUNCHER_DST_HEIGHT);
}
//draws a box on screen
void RenderSystem::RenderBox(PositionComponent* pc,LevelElementComponent* lec){
Sprite s;
lec->IsHit ? s=SPRT_CSTONE_HIT : s = SPRT_CSTONE;
ak_->DrawScaledBitmap(s,0,0,MISSILE_SRC_WIDTH,MISSILE_SRC_HEIGHT,pc->position.x_,SCREEN_HEIGHT-pc->position.y_,MISSILE_DST_WIDTH,MISSILE_DST_HEIGHT);
}
//draws a stone
void RenderSystem::RenderStone(PositionComponent* pc,LevelElementComponent* lec){
Sprite s;
lec->IsHit ? s=SPRT_OBSIDIAN_HIT : s = SPRT_OBSIDIAN;
ak_->DrawScaledBitmap(s,0,0,MISSILE_SRC_WIDTH,MISSILE_SRC_HEIGHT,pc->position.x_,SCREEN_HEIGHT-pc->position.y_,MISSILE_DST_WIDTH,MISSILE_DST_HEIGHT);
}
//draws a target
void RenderSystem::RenderTarget(PositionComponent* pc,LevelElementComponent* lec,TargetComponent* tc){
Sprite s;
lec->IsHit ? s=SPRT_SLIME_HIT : s = tc->TargetAnim[tc->animStage];
ak_->DrawScaledBitmap(s,0,0,MISSILE_SRC_WIDTH,MISSILE_SRC_HEIGHT,pc->position.x_,SCREEN_HEIGHT-pc->position.y_,MISSILE_DST_WIDTH,MISSILE_DST_HEIGHT);
if(tc->animTiming>=TARGETANIMTIMING){
tc->animStage = (tc->animStage+1)%(tc->TargetAnim.size());
tc->animTiming = 0;
}
else{
tc->animTiming += 1;
}
}
//draws a missile from type 1
void RenderSystem::RenderMissile1(PositionComponent* pc){
ak_->DrawScaledBitmap(SPRT_FIRECHARGE,0,0,MISSILE_SRC_WIDTH,MISSILE_SRC_HEIGHT,pc->position.x_,SCREEN_HEIGHT-pc->position.y_,MISSILE_DST_WIDTH,MISSILE_DST_HEIGHT);
}
//draws a missile from type 2
void RenderSystem::RenderMissile2(PositionComponent* pc,Missile2Component* mc){
ak_->DrawScaledBitmap(mc->Missile2Anim[mc->animStage],0,0,MISSILE_SRC_WIDTH,MISSILE_SRC_HEIGHT,pc->position.x_,SCREEN_HEIGHT-pc->position.y_,MISSILE_DST_WIDTH,MISSILE_DST_HEIGHT);
if(mc->animTiming>=MISSILE2ANIMTIMING){
mc->animStage = (mc->animStage+1)%(mc->Missile2Anim.size());
mc->animTiming = 0; //different animation stages to let the wings of the bee move
}
else{
mc->animTiming += 1;
}
}
//draws a missile from type 3
void RenderSystem::RenderMissile3(PositionComponent* pc){
ak_->DrawScaledBitmap(SPRT_ROCK,0,0,MISSILE_SRC_WIDTH,MISSILE_SRC_HEIGHT,pc->position.x_,SCREEN_HEIGHT-pc->position.y_,MISSILE_DST_WIDTH,MISSILE_DST_HEIGHT);
}
//draws the explosion graphics when missile1 hits a levelelement
bool RenderSystem::RenderExplosion(ExplosionEffectComponent* eec){
ak_->DrawScaledBitmap(eec->ExplosionAnim[eec->animStage],0,0,50,50,eec->position.x_-17,SCREEN_HEIGHT-eec->position.y_-17,70,70);
if(eec->animTiming>=EXPLOSIONANIMTIMING){
eec->animStage = (eec->animStage+1);
eec->animTiming = 0;
if((eec->animStage == eec->ExplosionAnim.size())){
return true;
}
}
else{
eec->animTiming += 1;
}
return false;
}
//draws the elastics of the launcher, when there is no missile in the launcher
void RenderSystem::Render_elastic_not_attached(void){
Point a = Point(116, 220); //a= Point at the top of the left branch of the launcher
Point b = Point(125,257); //b = Point lower than point a but also on the left branch of the launcher
Point c = Point(199,250); //c= Point at the top of the right branch of the launcher
Point d = Point(196,270); //d=Point lower than point but also on the right branch of the launcher
ak_->DrawLine(a,c); //connects the two highest Points
ak_->DrawLine(b,d); //connects the two lowest Points
}
//draws the elastics of the launcher, when there is a missile in the launcher
void RenderSystem::Render_elastic_attached(PositionComponent* pc){
Point a = Point(116, 220); //a= Point at the top of the left branch of the launcher
Point b = Point(125,257); //b = Point lower than point a but also on the left branch of the launcher
Point c = Point(199,250); //c= Point at the top of the right branch of the launcher
Point d = Point(196,270); //d=Point lower than point but also on the right branch of the launcher
int xco = pc->position.x_+MISSILE_DST_WIDTH/2 ;
int yco_boven = SCREEN_HEIGHT-pc->position.y_;
int yco_beneden = SCREEN_HEIGHT-pc->position.y_+MISSILE_DST_HEIGHT;
Point p1 = Point(xco, yco_boven);
Point p2 = Point(xco, yco_beneden);
ak_->DrawLine(a, p1);
ak_->DrawLine(c, p1);
ak_->DrawLine(b, p2);
ak_->DrawLine(d, p2);
}