12
12
13
13
using namespace sre ;
14
14
15
+ std::map<std::string, std::shared_ptr<sre::Texture>> Hero::inventoryTexture;
16
+
15
17
Hero::Hero (GameObject *gameObject)
16
18
: Component(gameObject), velocity(0 ) {
17
19
auto heroTexture = sre::Texture::create ()
18
20
.withFile (" assets/Goblin.png" )
19
21
.withFilterSampling (false )
20
22
.build ();
21
23
24
+ inventorySet = {" Antidote" ,
25
+ " Axe" ,
26
+ " BlueMagic" ,
27
+ " Carrot" ,
28
+ " Hat" ,
29
+ " Lemon" ,
30
+ " Necklace" ,
31
+ " Shield" };
32
+
22
33
heroSpriteAtlas = SpriteAtlas::createSingleSprite (heroTexture, " hero" );
23
34
24
35
auto spriteComponent = gameObject->addComponent <SpriteComponent>();
25
- spriteComponent->setSprite (heroSpriteAtlas->get (" hero" ));
36
+ auto sprite = heroSpriteAtlas->get (" hero" );
37
+ heroSize = sprite.getSpriteSize ();
38
+ spriteComponent->setSprite (sprite);
26
39
27
40
gameObject->setPosition ({305 ,200 });
28
41
@@ -50,11 +63,9 @@ bool Hero::onKey(SDL_Event &event) {
50
63
velocity.x = event.type == SDL_KEYDOWN?1 :0 ;
51
64
break ;
52
65
case SDLK_h:
66
+ speachBubbleTimeOut = 5 ;
53
67
message = " Hello world" ;
54
68
break ;
55
- case SDLK_SPACE:
56
- message = " " ;
57
- break ;
58
69
}
59
70
}
60
71
@@ -63,23 +74,29 @@ void Hero::update(float deltaTime) {
63
74
auto newPos = gameObject->getPosition () + velocity*speed*deltaTime;
64
75
65
76
gameObject->setPosition (newPos);
77
+ speachBubbleTimeOut -= deltaTime;
78
+ if (speachBubbleTimeOut < 0 ){
79
+ message = " " ;
80
+ }
81
+
66
82
}
67
83
68
- void Hero::speechBubble (){
84
+ void Hero::guiSpeechBubble (){
69
85
auto r = Renderer::instance;
70
86
auto winsize = r->getWindowSize ();
71
87
72
88
// display info over hero
73
89
auto flags =
74
90
ImGuiWindowFlags_NoTitleBar |
75
91
ImGuiWindowFlags_NoResize |
76
- ImGuiWindowFlags_NoMove;
92
+ ImGuiWindowFlags_NoMove |
93
+ ImGuiWindowFlags_NoScrollbar;
77
94
bool * open = nullptr ;
78
95
79
96
auto heroPos = gameObject->getPosition ();
80
- ImVec2 popupSize (200 , 50 );
97
+ ImVec2 popupSize (300 , 50 );
81
98
82
- ImVec2 pos (heroPos.x / 2 - popupSize.x / 2 , (winsize.y -heroPos.y )/ 2 + popupSize.y * 2 );
99
+ ImVec2 pos (heroPos.x - popupSize.x / 2 , (winsize.y - heroPos.y ) - popupSize.y - heroSize. y );
83
100
auto cond = ImGuiCond_Always;
84
101
ImGui::SetNextWindowPos (pos, cond);
85
102
ImGui::SetNextWindowSize (popupSize, cond);
@@ -91,31 +108,96 @@ void Hero::speechBubble(){
91
108
ImGui::End ();
92
109
}
93
110
94
- void Hero::gameInfo () {
95
- ImVec2 pos = {0 ,0 };
111
+ void Hero::guiGameInfo () {
112
+ auto r = Renderer::instance;
113
+ auto winsize = r->getWindowSize ();
114
+ ImVec2 size = {220 ,60 };
115
+ ImVec2 pos = {winsize.x - size.x ,0 };
96
116
auto cond = ImGuiCond_Always;
97
117
ImVec2 pivot = {0 ,0 };
98
118
ImGui::SetNextWindowPos (pos, cond, pivot);
99
- ImVec2 size = { 100 , 50 };
119
+
100
120
ImGui::SetNextWindowSize (size, cond);
101
121
auto flags =
102
122
ImGuiWindowFlags_NoTitleBar |
103
123
ImGuiWindowFlags_NoResize |
104
- ImGuiWindowFlags_NoMove;
124
+ ImGuiWindowFlags_NoMove |
125
+ ImGuiWindowFlags_NoScrollbar;
105
126
bool * open = nullptr ;
106
- ImGui::Begin (" " , open, flags);
127
+ ImGui::Begin (" #gameinfo " , open, flags);
107
128
ImGui::PushFont (ProggyTiny);
108
- ImGui::Text (" Score" );
109
- ImGui::SameLine ();
129
+
130
+ // draw score
131
+ ImGui::Text (" Score" ); ImGui::SameLine ();
132
+ auto scoreStr = std::to_string (score);
133
+ float windowWidth =
ImGui::GetWindowContentRegionWidth ();
134
+ float width = ImGui::CalcTextSize (scoreStr.c_str ()).x ;
135
+ ImGui::SetCursorPosX (windowWidth - width); // align right
136
+ ImGui::Text (scoreStr.c_str ());
137
+
138
+ // draw health
139
+ ImGui::PushID (1 );
140
+ auto healthStr = std::to_string (health);
141
+ ImGui::Text (" Health" ); ImGui::SameLine ();
142
+ width = ImGui::CalcTextSize (healthStr.c_str ()).x ;
143
+ ImGui::SetCursorPosX (windowWidth - width); // align right
144
+ ImGui::Text (healthStr.c_str ());
145
+ ImGui::PopID ();
110
146
ImGui::PopFont ();
111
147
ImGui::End ();
112
148
}
113
149
114
- void Hero::onGui () {
115
- if (message != " " ){
116
- speechBubble ();
150
+ void Hero::guiInventory () {
151
+ ImVec2 pos = {0 ,0 };
152
+ auto cond = ImGuiCond_Always;
153
+ ImVec2 pivot = {0 ,0 };
154
+ ImGui::SetNextWindowPos (pos, cond, pivot);
155
+ ImVec2 size = {165 , 107 };
156
+ ImGui::SetNextWindowSize (size, cond);
157
+ auto flags =
158
+ ImGuiWindowFlags_NoTitleBar |
159
+ ImGuiWindowFlags_NoResize |
160
+ ImGuiWindowFlags_NoMove |
161
+ ImGuiWindowFlags_NoScrollbar;
162
+ bool * open = nullptr ;
163
+ ImGui::Begin (" #inventory" , open, flags);
164
+ ImGui::PushFont (ProggyTiny);
165
+ ImGui::Text (" Inventory" );
166
+
167
+ int count = 0 ;
168
+ for (auto & item : inventorySet){
169
+ auto hasItem = inventoryTexture.find (item) != inventoryTexture.end ();
170
+ if (!hasItem){
171
+ auto filename = std::string (" assets/" )+item+" .png" ;
172
+ inventoryTexture[item] = Texture::create ().withFile (filename).withFilterSampling (false ).build ();
173
+ }
174
+ ImVec2 uv0 (0 ,1 ); // flip y axis coordinates
175
+ ImVec2 uv1 (1 ,0 );
176
+ ImVec2 s (30 ,30 );
177
+ ImGui::Image (inventoryTexture[item]->getNativeTexturePtr (), s, uv0, uv1 , ImVec4 (1 ,1 ,1 ,1 ),ImVec4 (0 ,0 ,0 ,1 ));
178
+
179
+ if (ImGui::IsItemHovered ())
180
+ {
181
+ ImGui::BeginTooltip ();
182
+ ImGui::PushTextWrapPos (ImGui::GetFontSize () * 35 .0f );
183
+ ImGui::TextUnformatted (item.c_str ());
184
+ ImGui::PopTextWrapPos ();
185
+ ImGui::EndTooltip ();
186
+ }
187
+
188
+ if (count == 0 || count %4 != 0 )
189
+ ImGui::SameLine ();
190
+ count ++;
117
191
}
118
- gameInfo ();
119
192
193
+ ImGui::PopFont ();
194
+ ImGui::End ();
195
+ }
120
196
197
+ void Hero::onGui () {
198
+ if (!message.empty ()){
199
+ guiSpeechBubble ();
200
+ }
201
+ guiGameInfo ();
202
+ guiInventory ();
121
203
}
0 commit comments