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
e3e89d02 sago007 2018-04-26 06:50 30
extern sago::SagoSprite bricks[7];
d4f28be2 sago007 2018-04-24 09:33 31
e0c7836a sago007 2018-04-25 12:38 32
/**
e0c7836a sago007 2018-04-25 12:38 33
 * Draws bricks with a string like:
e0c7836a sago007 2018-04-25 12:38 34
 * "aab" for two identical one and another
e0c7836a sago007 2018-04-25 12:38 35
 * "aaB" the third one will have a bomb
d2ba3cf5 sago007 2018-05-13 15:00 36
 * The any char not in 'a' to 'g' or 'A' to 'G' the behavior is undefined.
e0c7836a sago007 2018-04-25 12:38 37
 * @param target Target to draw to
e0c7836a sago007 2018-04-25 12:38 38
 * @param bricks description on what to draw as a string
e0c7836a sago007 2018-04-25 12:38 39
 * @param x
e0c7836a sago007 2018-04-25 12:38 40
 * @param y
e0c7836a sago007 2018-04-25 12:38 41
 */
e0c7836a sago007 2018-04-25 12:38 42
static void RenderRowOfBricks(SDL_Renderer* target, const std::string& brickStr, int x, int y) {
e3e89d02 sago007 2018-04-26 06:50 43
	Uint32 tick = SDL_GetTicks();
e0c7836a sago007 2018-04-25 12:38 44
	for (size_t i = 0; i < brickStr.size(); ++i) {
e0c7836a sago007 2018-04-25 12:38 45
		bool bomb = false;
e0c7836a sago007 2018-04-25 12:38 46
		char brickChar = brickStr[i];
e0c7836a sago007 2018-04-25 12:38 47
		if (brickChar >= 'A' && brickChar <= 'G') {
e0c7836a sago007 2018-04-25 12:38 48
			bomb = true;
e0c7836a sago007 2018-04-25 12:38 49
			brickChar = brickChar + 'a' - 'A';
e0c7836a sago007 2018-04-25 12:38 50
		}
e0c7836a sago007 2018-04-25 12:38 51
		if (brickChar >= 'a' &&  brickChar <= 'g') {
e0c7836a sago007 2018-04-25 12:38 52
			bricks[brickChar - 'a'].Draw(target, tick, x+i*50, y);
e0c7836a sago007 2018-04-25 12:38 53
			if (bomb) {
e0c7836a sago007 2018-04-25 12:38 54
				globalData.spriteHolder->GetSprite("block_bomb").Draw(target, tick, x+i*50, y);
e0c7836a sago007 2018-04-25 12:38 55
			}
e0c7836a sago007 2018-04-25 12:38 56
		}
e0c7836a sago007 2018-04-25 12:38 57
	}
e0c7836a sago007 2018-04-25 12:38 58
}
e0c7836a sago007 2018-04-25 12:38 59
e3e89d02 sago007 2018-04-26 06:50 60
class HorizontalSwitchAnimation {
e3e89d02 sago007 2018-04-26 06:50 61
public:
e3e89d02 sago007 2018-04-26 06:50 62
	std::string brickStr = "abc";
e3e89d02 sago007 2018-04-26 06:50 63
	int cursorPos = 0;
e3e89d02 sago007 2018-04-26 06:50 64
	int state = 0; //0=move left, 1 = switch, 2 = move right, 3 = switch
e3e89d02 sago007 2018-04-26 06:50 65
	Uint32 lastTick = 0;
e3e89d02 sago007 2018-04-26 06:50 66
	Uint32 animationSpeed = 2000;
d2ba3cf5 sago007 2018-05-13 15:00 67
e3e89d02 sago007 2018-04-26 06:50 68
	void Update (Uint32 tick) {
e3e89d02 sago007 2018-04-26 06:50 69
		if (tick > lastTick + animationSpeed) {
e3e89d02 sago007 2018-04-26 06:50 70
			lastTick = tick;
e3e89d02 sago007 2018-04-26 06:50 71
			switch (state) {
d2ba3cf5 sago007 2018-05-13 15:00 72
			case 0:
d2ba3cf5 sago007 2018-05-13 15:00 73
				cursorPos = 1;
d2ba3cf5 sago007 2018-05-13 15:00 74
				break;
d2ba3cf5 sago007 2018-05-13 15:00 75
			case 1:  //fallthough
d2ba3cf5 sago007 2018-05-13 15:00 76
			case 3:
d2ba3cf5 sago007 2018-05-13 15:00 77
				std::swap(brickStr[cursorPos], brickStr[cursorPos + 1]);
d2ba3cf5 sago007 2018-05-13 15:00 78
				break;
d2ba3cf5 sago007 2018-05-13 15:00 79
			case 2:
d2ba3cf5 sago007 2018-05-13 15:00 80
				cursorPos = 0;
d2ba3cf5 sago007 2018-05-13 15:00 81
				break;
e3e89d02 sago007 2018-04-26 06:50 82
			}
e3e89d02 sago007 2018-04-26 06:50 83
			++state;
e3e89d02 sago007 2018-04-26 06:50 84
			if (state > 3) {
e3e89d02 sago007 2018-04-26 06:50 85
				state = 0;
e3e89d02 sago007 2018-04-26 06:50 86
			}
e3e89d02 sago007 2018-04-26 06:50 87
		}
e3e89d02 sago007 2018-04-26 06:50 88
	}
e3e89d02 sago007 2018-04-26 06:50 89
};
e3e89d02 sago007 2018-04-26 06:50 90
a66aa2dd sago007 2018-04-27 10:00 91
class MultiLineBlocks {
a66aa2dd sago007 2018-04-27 10:00 92
private:
a66aa2dd sago007 2018-04-27 10:00 93
	std::vector<std::string> lines;
a66aa2dd sago007 2018-04-27 10:00 94
public:
a66aa2dd sago007 2018-04-27 10:00 95
	MultiLineBlocks& addLine(const std::string& line) {
a66aa2dd sago007 2018-04-27 10:00 96
		lines.push_back(line);
a66aa2dd sago007 2018-04-27 10:00 97
		return *this;
a66aa2dd sago007 2018-04-27 10:00 98
	}
d2ba3cf5 sago007 2018-05-13 15:00 99
a66aa2dd sago007 2018-04-27 10:00 100
	void Render(SDL_Renderer* target, int x, int y) {
a66aa2dd sago007 2018-04-27 10:00 101
		for (size_t i = 0; i < lines.size(); ++i) {
a66aa2dd sago007 2018-04-27 10:00 102
			RenderRowOfBricks(target, lines[i], x, y+i*50);
a66aa2dd sago007 2018-04-27 10:00 103
		}
a66aa2dd sago007 2018-04-27 10:00 104
	}
d2ba3cf5 sago007 2018-05-13 15:00 105
a66aa2dd sago007 2018-04-27 10:00 106
};
a66aa2dd sago007 2018-04-27 10:00 107
e3e89d02 sago007 2018-04-26 06:50 108
HorizontalSwitchAnimation switchAnimation;
e3e89d02 sago007 2018-04-26 06:50 109
sago::SagoTextField switchAnimationField;
8f6cc9b3 sago007 2018-04-29 15:12 110
sago::SagoTextField clearRowfield;
8f6cc9b3 sago007 2018-04-29 15:12 111
sago::SagoTextField comboField;
8f6cc9b3 sago007 2018-04-29 15:12 112
sago::SagoTextField dropField;
8f6cc9b3 sago007 2018-04-29 15:12 113
sago::SagoTextField chainField;
8f6cc9b3 sago007 2018-04-29 15:12 114
8f6cc9b3 sago007 2018-04-29 15:12 115
static void InitTextField(sago::SagoTextField& field, const char* text) {
8f6cc9b3 sago007 2018-04-29 15:12 116
	field.SetHolder(&globalData.spriteHolder->GetDataHolder());
8f6cc9b3 sago007 2018-04-29 15:12 117
	field.SetFontSize(30);
8f6cc9b3 sago007 2018-04-29 15:12 118
	field.SetOutline(2, {0,0,0,255});
8f6cc9b3 sago007 2018-04-29 15:12 119
	field.SetText(text);
8f6cc9b3 sago007 2018-04-29 15:12 120
}
e3e89d02 sago007 2018-04-26 06:50 121
d8fe60bd sago007 2018-04-02 16:15 122
HelpHowtoState::HelpHowtoState() {
8f6cc9b3 sago007 2018-04-29 15:12 123
	InitTextField(switchAnimationField, _("Switch block horizontally"));
8f6cc9b3 sago007 2018-04-29 15:12 124
	InitTextField(clearRowfield, _("Match 3 to clear"));
8f6cc9b3 sago007 2018-04-29 15:12 125
	InitTextField(comboField, _("Create combos!"));
8f6cc9b3 sago007 2018-04-29 15:12 126
	InitTextField(dropField, _("Drop blocks!"));
8f6cc9b3 sago007 2018-04-29 15:12 127
	InitTextField(chainField, _("Create a chain effect"));
d8fe60bd sago007 2018-04-02 16:15 128
}
d8fe60bd sago007 2018-04-02 16:15 129
d8fe60bd sago007 2018-04-02 16:15 130
HelpHowtoState::~HelpHowtoState() {
d8fe60bd sago007 2018-04-02 16:15 131
}
d8fe60bd sago007 2018-04-02 16:15 132
702c3f63 sago007 2018-04-29 16:40 133
const double PI  =3.141592653589793238463;
702c3f63 sago007 2018-04-29 16:40 134
702c3f63 sago007 2018-04-29 16:40 135
static void DrawArrow(SDL_Renderer* target, int x1, int y1, int x2, int y2) {
702c3f63 sago007 2018-04-29 16:40 136
	double dx = x1-x2;
702c3f63 sago007 2018-04-29 16:40 137
	double dy = y1-y2;
702c3f63 sago007 2018-04-29 16:40 138
	double distance = std::sqrt((dx*dx)+(dy*dy));
702c3f63 sago007 2018-04-29 16:40 139
	dx = dx * 10.0 / distance;
702c3f63 sago007 2018-04-29 16:40 140
	dy = dy * 10.0 / distance;
702c3f63 sago007 2018-04-29 16:40 141
	double angle= PI/4.0;
51ba5a85 Poul Sander 2018-05-06 12:48 142
	double nx1 = dx * std::cos(angle) - dy * std::sin(angle) + x2;
51ba5a85 Poul Sander 2018-05-06 12:48 143
	double ny1 = dx * std::sin(angle) + dy * std::cos(angle) + y2;
702c3f63 sago007 2018-04-29 16:40 144
	SDL_RenderDrawLine(target, x1, y1, x2, y2);
702c3f63 sago007 2018-04-29 16:40 145
	SDL_RenderDrawLine(target, nx1, ny1, x2, y2);
51ba5a85 Poul Sander 2018-05-06 12:48 146
	nx1 = dx * std::cos(-angle) - dy * std::sin(-angle) + x2;
51ba5a85 Poul Sander 2018-05-06 12:48 147
	ny1 = dx * std::sin(-angle) + dy * std::cos(-angle) + y2;
702c3f63 sago007 2018-04-29 16:40 148
	SDL_RenderDrawLine(target, nx1, ny1, x2, y2);
702c3f63 sago007 2018-04-29 16:40 149
}
702c3f63 sago007 2018-04-29 16:40 150
d8fe60bd sago007 2018-04-02 16:15 151
void HelpHowtoState::Draw(SDL_Renderer* target) {
d8fe60bd sago007 2018-04-02 16:15 152
	DrawBackground(target);
855544ea Poul Sander 2021-10-06 20:05 153
	SDL_SetRenderDrawColor(target, 0, 0, 0, SDL_ALPHA_OPAQUE);
e3e89d02 sago007 2018-04-26 06:50 154
	RenderRowOfBricks(target, switchAnimation.brickStr, 50, 50);
e3e89d02 sago007 2018-04-26 06:50 155
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+switchAnimation.cursorPos*50, 50);
8f6cc9b3 sago007 2018-04-29 15:12 156
	switchAnimationField.Draw(target, 50 +150+30, 50+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
a66aa2dd sago007 2018-04-27 10:00 157
	RenderRowOfBricks(target, "adaa", 50, 150);
a66aa2dd sago007 2018-04-27 10:00 158
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 150);
a66aa2dd sago007 2018-04-27 10:00 159
	RenderRowOfBricks(target, "dAAA", 50+300, 150);
702c3f63 sago007 2018-04-29 16:40 160
	DrawArrow(target, 50+200+25, 150+25, 50+300-25, 150+25);
8f6cc9b3 sago007 2018-04-29 15:12 161
	clearRowfield.Draw(target, 600, 150+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
8f6cc9b3 sago007 2018-04-29 15:12 162
	comboField.Draw(target, 50+175, 410, sago::SagoTextField::Alignment::center);
a66aa2dd sago007 2018-04-27 10:00 163
	MultiLineBlocks().addLine("ab").addLine("ba").addLine("ab").Render(target, 50, 250);
a66aa2dd sago007 2018-04-27 10:00 164
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 250+50);
a66aa2dd sago007 2018-04-27 10:00 165
	MultiLineBlocks().addLine("AB").addLine("AB").addLine("AB").Render(target, 50+200, 250);
4830f315 Poul Sander 2018-05-06 12:44 166
	DrawArrow(target, 175, 325, 225, 325);
a66aa2dd sago007 2018-04-27 10:00 167
	MultiLineBlocks().addLine("a").addLine("b").addLine("e").Render(target, 50+400, 250);
a66aa2dd sago007 2018-04-27 10:00 168
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+400, 250);
a66aa2dd sago007 2018-04-27 10:00 169
	MultiLineBlocks().addLine(" ").addLine("b").addLine("ea").Render(target, 50+400+200, 250);
8f6cc9b3 sago007 2018-04-29 15:12 170
	dropField.Draw(target, 50+400+150, 410, sago::SagoTextField::Alignment::center);
4830f315 Poul Sander 2018-05-06 12:44 171
	DrawArrow(target, 575, 325, 625, 325);
4830f315 Poul Sander 2018-05-06 12:44 172
	DrawArrow(target, 475, 275, 525, 275);
4830f315 Poul Sander 2018-05-06 12:44 173
	DrawArrow(target, 525, 275, 525, 375);
4830f315 Poul Sander 2018-05-06 12:44 174
	DrawArrow(target, 675, 275, 725, 275);
4830f315 Poul Sander 2018-05-06 12:44 175
	DrawArrow(target, 725, 275, 725, 375);
a66aa2dd sago007 2018-04-27 10:00 176
	MultiLineBlocks().addLine(" d").addLine(" f").addLine(" f").addLine("fdd").Render(target, 50, 500);
a66aa2dd sago007 2018-04-27 10:00 177
	MultiLineBlocks().addLine(" d").addLine(" F").addLine(" F").addLine("dFd").Render(target, 50+200, 500);
a66aa2dd sago007 2018-04-27 10:00 178
	MultiLineBlocks().addLine(" d").addLine("  ").addLine("  ").addLine("d d").Render(target, 50+200*2, 500);
a66aa2dd sago007 2018-04-27 10:00 179
	MultiLineBlocks().addLine("  ").addLine("  ").addLine("  ").addLine("DDD").Render(target, 50+200*3, 500);
4830f315 Poul Sander 2018-05-06 12:44 180
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 650);
4830f315 Poul Sander 2018-05-06 12:44 181
	DrawArrow(target, 200, 600, 250, 600);
4830f315 Poul Sander 2018-05-06 12:44 182
	DrawArrow(target, 400, 600, 450, 600);
4830f315 Poul Sander 2018-05-06 12:44 183
	DrawArrow(target, 600, 600, 650, 600);
4830f315 Poul Sander 2018-05-06 12:44 184
	DrawArrow(target, 525, 525, 525, 675);
8f6cc9b3 sago007 2018-04-29 15:12 185
	chainField.Draw(target, 400, 710, sago::SagoTextField::Alignment::center);
ec3f09de Poul Sander 2022-01-29 14:33 186
	HelpCommonState::Draw(target);
d8fe60bd sago007 2018-04-02 16:15 187
}
d8fe60bd sago007 2018-04-02 16:15 188
d8fe60bd sago007 2018-04-02 16:15 189
void HelpHowtoState::Update() {
e3e89d02 sago007 2018-04-26 06:50 190
	switchAnimation.Update(SDL_GetTicks());
ec3f09de Poul Sander 2022-01-29 14:33 191
	HelpCommonState::Update();
d8fe60bd sago007 2018-04-02 16:15 192
}