1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2018 Poul Sander
5 
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see http://www.gnu.org/licenses/
18 
19 Source information and contacts persons can be found at
20 https://blockattack.net
21 ===========================================================================
22 */
23 
24 #include "HelpHowtoState.hpp"
25 #include "global.hpp"
26 #include "common.h"
27 #include "MenuSystem.h"
28 #include <cmath>
29 
30 const int buttonOffset = 160;
31 extern sago::SagoSprite bExit;
32 extern sago::SagoSprite bricks[7];
33 
34 /**
35  * Draws bricks with a string like:
36  * "aab" for two identical one and another
37  * "aaB" the third one will have a bomb
38  * The any char not in 'a' to 'g' or 'A' to 'G' the behavior is undefined.
39  * @param target Target to draw to
40  * @param bricks description on what to draw as a string
41  * @param x
42  * @param y
43  */
RenderRowOfBricks(SDL_Renderer * target,const std::string & brickStr,int x,int y)44 static void RenderRowOfBricks(SDL_Renderer* target, const std::string& brickStr, int x, int y) {
45 	Uint32 tick = SDL_GetTicks();
46 	for (size_t i = 0; i < brickStr.size(); ++i) {
47 		bool bomb = false;
48 		char brickChar = brickStr[i];
49 		if (brickChar >= 'A' && brickChar <= 'G') {
50 			bomb = true;
51 			brickChar = brickChar + 'a' - 'A';
52 		}
53 		if (brickChar >= 'a' &&  brickChar <= 'g') {
54 			bricks[brickChar - 'a'].Draw(target, tick, x+i*50, y);
55 			if (bomb) {
56 				globalData.spriteHolder->GetSprite("block_bomb").Draw(target, tick, x+i*50, y);
57 			}
58 		}
59 	}
60 }
61 
62 class HorizontalSwitchAnimation {
63 public:
64 	std::string brickStr = "abc";
65 	int cursorPos = 0;
66 	int state = 0; //0=move left, 1 = switch, 2 = move right, 3 = switch
67 	Uint32 lastTick = 0;
68 	Uint32 animationSpeed = 2000;
69 
Update(Uint32 tick)70 	void Update (Uint32 tick) {
71 		if (tick > lastTick + animationSpeed) {
72 			lastTick = tick;
73 			switch (state) {
74 			case 0:
75 				cursorPos = 1;
76 				break;
77 			case 1:  //fallthough
78 			case 3:
79 				std::swap(brickStr[cursorPos], brickStr[cursorPos + 1]);
80 				break;
81 			case 2:
82 				cursorPos = 0;
83 				break;
84 			}
85 			++state;
86 			if (state > 3) {
87 				state = 0;
88 			}
89 		}
90 	}
91 };
92 
93 class MultiLineBlocks {
94 private:
95 	std::vector<std::string> lines;
96 public:
addLine(const std::string & line)97 	MultiLineBlocks& addLine(const std::string& line) {
98 		lines.push_back(line);
99 		return *this;
100 	}
101 
Render(SDL_Renderer * target,int x,int y)102 	void Render(SDL_Renderer* target, int x, int y) {
103 		for (size_t i = 0; i < lines.size(); ++i) {
104 			RenderRowOfBricks(target, lines[i], x, y+i*50);
105 		}
106 	}
107 
108 };
109 
110 HorizontalSwitchAnimation switchAnimation;
111 sago::SagoTextField switchAnimationField;
112 sago::SagoTextField clearRowfield;
113 sago::SagoTextField comboField;
114 sago::SagoTextField dropField;
115 sago::SagoTextField chainField;
116 
InitTextField(sago::SagoTextField & field,const char * text)117 static void InitTextField(sago::SagoTextField& field, const char* text) {
118 	field.SetHolder(&globalData.spriteHolder->GetDataHolder());
119 	field.SetFontSize(30);
120 	field.SetOutline(2, {0,0,0,255});
121 	field.SetText(text);
122 }
123 
HelpHowtoState()124 HelpHowtoState::HelpHowtoState() {
125 	InitTextField(switchAnimationField, _("Switch block horizontally"));
126 	InitTextField(clearRowfield, _("Match 3 to clear"));
127 	InitTextField(comboField, _("Create combos!"));
128 	InitTextField(dropField, _("Drop blocks!"));
129 	InitTextField(chainField, _("Create a chain effect"));
130 }
131 
~HelpHowtoState()132 HelpHowtoState::~HelpHowtoState() {
133 }
134 
IsActive()135 bool HelpHowtoState::IsActive() {
136 	return isActive;
137 }
138 
ProcessInput(const SDL_Event & event,bool & processed)139 void HelpHowtoState::ProcessInput(const SDL_Event& event, bool& processed) {
140 
141 	UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
142 
143 	if (isConfirmEvent(event) || isEscapeEvent(event)) {
144 		isActive = false;
145 		processed = true;
146 	}
147 }
148 
149 const double PI  =3.141592653589793238463;
150 
DrawArrow(SDL_Renderer * target,int x1,int y1,int x2,int y2)151 static void DrawArrow(SDL_Renderer* target, int x1, int y1, int x2, int y2) {
152 	double dx = x1-x2;
153 	double dy = y1-y2;
154 	double distance = std::sqrt((dx*dx)+(dy*dy));
155 	dx = dx * 10.0 / distance;
156 	dy = dy * 10.0 / distance;
157 	double angle= PI/4.0;
158 	double nx1 = dx * std::cos(angle) - dy * std::sin(angle) + x2;
159 	double ny1 = dx * std::sin(angle) + dy * std::cos(angle) + y2;
160 	SDL_RenderDrawLine(target, x1, y1, x2, y2);
161 	SDL_RenderDrawLine(target, nx1, ny1, x2, y2);
162 	nx1 = dx * std::cos(-angle) - dy * std::sin(-angle) + x2;
163 	ny1 = dx * std::sin(-angle) + dy * std::cos(-angle) + y2;
164 	SDL_RenderDrawLine(target, nx1, ny1, x2, y2);
165 }
166 
Draw(SDL_Renderer * target)167 void HelpHowtoState::Draw(SDL_Renderer* target) {
168 	DrawBackground(target);
169 	SDL_SetRenderDrawColor(target, 0, 0, 0, SDL_ALPHA_OPAQUE);
170 	RenderRowOfBricks(target, switchAnimation.brickStr, 50, 50);
171 	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+switchAnimation.cursorPos*50, 50);
172 	switchAnimationField.Draw(target, 50 +150+30, 50+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
173 	RenderRowOfBricks(target, "adaa", 50, 150);
174 	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 150);
175 	RenderRowOfBricks(target, "dAAA", 50+300, 150);
176 	DrawArrow(target, 50+200+25, 150+25, 50+300-25, 150+25);
177 	clearRowfield.Draw(target, 600, 150+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
178 	comboField.Draw(target, 50+175, 410, sago::SagoTextField::Alignment::center);
179 	MultiLineBlocks().addLine("ab").addLine("ba").addLine("ab").Render(target, 50, 250);
180 	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 250+50);
181 	MultiLineBlocks().addLine("AB").addLine("AB").addLine("AB").Render(target, 50+200, 250);
182 	DrawArrow(target, 175, 325, 225, 325);
183 	MultiLineBlocks().addLine("a").addLine("b").addLine("e").Render(target, 50+400, 250);
184 	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+400, 250);
185 	MultiLineBlocks().addLine(" ").addLine("b").addLine("ea").Render(target, 50+400+200, 250);
186 	dropField.Draw(target, 50+400+150, 410, sago::SagoTextField::Alignment::center);
187 	DrawArrow(target, 575, 325, 625, 325);
188 	DrawArrow(target, 475, 275, 525, 275);
189 	DrawArrow(target, 525, 275, 525, 375);
190 	DrawArrow(target, 675, 275, 725, 275);
191 	DrawArrow(target, 725, 275, 725, 375);
192 	MultiLineBlocks().addLine(" d").addLine(" f").addLine(" f").addLine("fdd").Render(target, 50, 500);
193 	MultiLineBlocks().addLine(" d").addLine(" F").addLine(" F").addLine("dFd").Render(target, 50+200, 500);
194 	MultiLineBlocks().addLine(" d").addLine("  ").addLine("  ").addLine("d d").Render(target, 50+200*2, 500);
195 	MultiLineBlocks().addLine("  ").addLine("  ").addLine("  ").addLine("DDD").Render(target, 50+200*3, 500);
196 	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 650);
197 	DrawArrow(target, 200, 600, 250, 600);
198 	DrawArrow(target, 400, 600, 450, 600);
199 	DrawArrow(target, 600, 600, 650, 600);
200 	DrawArrow(target, 525, 525, 525, 675);
201 	chainField.Draw(target, 400, 710, sago::SagoTextField::Alignment::center);
202 	bExit.Draw(globalData.screen, SDL_GetTicks(), globalData.xsize-buttonOffset, globalData.ysize-buttonOffset);
203 #if DEBUG
204 	static sago::SagoTextField mousePos;
205 	mousePos.SetHolder(&globalData.spriteHolder->GetDataHolder());
206 	mousePos.SetFontSize(16);
207 	mousePos.SetOutline(1, {128,128,128,255});
208 	mousePos.SetText(std::string("Mouse position: ")+std::to_string(globalData.mousex)+std::string(", ")+std::to_string(globalData.mousey));
209 	mousePos.Draw(target, 0,0);
210 #endif
211 }
212 
Update()213 void HelpHowtoState::Update() {
214 	// If the mouse button is released, make bMouseUp equal true
215 	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
216 		bMouseUp=true;
217 	}
218 
219 	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
220 		bMouseUp = false;
221 
222 		//The Score button:
223 		if ((globalData.mousex>globalData.xsize-buttonOffset) && (globalData.mousex<globalData.xsize-buttonOffset+bExit.GetWidth())
224 		        && (globalData.mousey>globalData.ysize-buttonOffset) && (globalData.mousey<globalData.ysize-buttonOffset+bExit.GetHeight())) {
225 			isActive = false;
226 		}
227 
228 	}
229 	switchAnimation.Update(SDL_GetTicks());
230 }