git repos / blockattack-game

blame: source/code/HelpHowtoState.cpp

normal view · raw

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