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"
d8fe60bd sago007 2018-04-02 16:15 28
d4f28be2 sago007 2018-04-24 09:33 29
const int xsize = 1024;
d4f28be2 sago007 2018-04-24 09:33 30
const int ysize = 768;
d4f28be2 sago007 2018-04-24 09:33 31
const int buttonOffset = 160;
d4f28be2 sago007 2018-04-24 09:33 32
extern sago::SagoSprite bExit;
e3e89d02 sago007 2018-04-26 06:50 33
extern sago::SagoSprite bricks[7];
d4f28be2 sago007 2018-04-24 09:33 34
e0c7836a sago007 2018-04-25 12:38 35
/**
e0c7836a sago007 2018-04-25 12:38 36
 * Draws bricks with a string like:
e0c7836a sago007 2018-04-25 12:38 37
 * "aab" for two identical one and another
e0c7836a sago007 2018-04-25 12:38 38
 * "aaB" the third one will have a bomb
e0c7836a sago007 2018-04-25 12:38 39
 * The any char not in 'a' to 'g' or 'A' to 'G' the behavior is undefined. 
e0c7836a sago007 2018-04-25 12:38 40
 * @param target Target to draw to
e0c7836a sago007 2018-04-25 12:38 41
 * @param bricks description on what to draw as a string
e0c7836a sago007 2018-04-25 12:38 42
 * @param x
e0c7836a sago007 2018-04-25 12:38 43
 * @param y
e0c7836a sago007 2018-04-25 12:38 44
 */
e0c7836a sago007 2018-04-25 12:38 45
static void RenderRowOfBricks(SDL_Renderer* target, const std::string& brickStr, int x, int y) {
e3e89d02 sago007 2018-04-26 06:50 46
	Uint32 tick = SDL_GetTicks();
e0c7836a sago007 2018-04-25 12:38 47
	for (size_t i = 0; i < brickStr.size(); ++i) {
e0c7836a sago007 2018-04-25 12:38 48
		bool bomb = false;
e0c7836a sago007 2018-04-25 12:38 49
		char brickChar = brickStr[i];
e0c7836a sago007 2018-04-25 12:38 50
		if (brickChar >= 'A' && brickChar <= 'G') {
e0c7836a sago007 2018-04-25 12:38 51
			bomb = true;
e0c7836a sago007 2018-04-25 12:38 52
			brickChar = brickChar + 'a' - 'A';
e0c7836a sago007 2018-04-25 12:38 53
		}
e0c7836a sago007 2018-04-25 12:38 54
		if (brickChar >= 'a' &&  brickChar <= 'g') {
e0c7836a sago007 2018-04-25 12:38 55
			bricks[brickChar - 'a'].Draw(target, tick, x+i*50, y);
e0c7836a sago007 2018-04-25 12:38 56
			if (bomb) {
e0c7836a sago007 2018-04-25 12:38 57
				globalData.spriteHolder->GetSprite("block_bomb").Draw(target, tick, x+i*50, y);
e0c7836a sago007 2018-04-25 12:38 58
			}
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
e3e89d02 sago007 2018-04-26 06:50 63
class HorizontalSwitchAnimation {
e3e89d02 sago007 2018-04-26 06:50 64
public:
e3e89d02 sago007 2018-04-26 06:50 65
	std::string brickStr = "abc";
e3e89d02 sago007 2018-04-26 06:50 66
	int cursorPos = 0;
e3e89d02 sago007 2018-04-26 06:50 67
	int state = 0; //0=move left, 1 = switch, 2 = move right, 3 = switch
e3e89d02 sago007 2018-04-26 06:50 68
	Uint32 lastTick = 0;
e3e89d02 sago007 2018-04-26 06:50 69
	Uint32 animationSpeed = 2000;
e3e89d02 sago007 2018-04-26 06:50 70
	
e3e89d02 sago007 2018-04-26 06:50 71
	void Update (Uint32 tick) {
e3e89d02 sago007 2018-04-26 06:50 72
		if (tick > lastTick + animationSpeed) {
e3e89d02 sago007 2018-04-26 06:50 73
			lastTick = tick;
e3e89d02 sago007 2018-04-26 06:50 74
			switch (state) {
e3e89d02 sago007 2018-04-26 06:50 75
				case 0:
e3e89d02 sago007 2018-04-26 06:50 76
					cursorPos = 1;
e3e89d02 sago007 2018-04-26 06:50 77
					break;
e3e89d02 sago007 2018-04-26 06:50 78
				case 1:  //fallthough
e3e89d02 sago007 2018-04-26 06:50 79
				case 3:
e3e89d02 sago007 2018-04-26 06:50 80
					std::swap(brickStr[cursorPos], brickStr[cursorPos + 1]);
e3e89d02 sago007 2018-04-26 06:50 81
					break;
e3e89d02 sago007 2018-04-26 06:50 82
				case 2:
e3e89d02 sago007 2018-04-26 06:50 83
					cursorPos = 0;
e3e89d02 sago007 2018-04-26 06:50 84
					break;
e3e89d02 sago007 2018-04-26 06:50 85
			}
e3e89d02 sago007 2018-04-26 06:50 86
			++state;
e3e89d02 sago007 2018-04-26 06:50 87
			if (state > 3) {
e3e89d02 sago007 2018-04-26 06:50 88
				state = 0;
e3e89d02 sago007 2018-04-26 06:50 89
			}
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
a66aa2dd sago007 2018-04-27 10:00 94
class MultiLineBlocks {
a66aa2dd sago007 2018-04-27 10:00 95
private:
a66aa2dd sago007 2018-04-27 10:00 96
	std::vector<std::string> lines;
a66aa2dd sago007 2018-04-27 10:00 97
public:
a66aa2dd sago007 2018-04-27 10:00 98
	MultiLineBlocks& addLine(const std::string& line) {
a66aa2dd sago007 2018-04-27 10:00 99
		lines.push_back(line);
a66aa2dd sago007 2018-04-27 10:00 100
		return *this;
a66aa2dd sago007 2018-04-27 10:00 101
	}
a66aa2dd sago007 2018-04-27 10:00 102
	
a66aa2dd sago007 2018-04-27 10:00 103
	void Render(SDL_Renderer* target, int x, int y) {
a66aa2dd sago007 2018-04-27 10:00 104
		for (size_t i = 0; i < lines.size(); ++i) {
a66aa2dd sago007 2018-04-27 10:00 105
			RenderRowOfBricks(target, lines[i], x, y+i*50);
a66aa2dd sago007 2018-04-27 10:00 106
		}
a66aa2dd sago007 2018-04-27 10:00 107
	}
a66aa2dd sago007 2018-04-27 10:00 108
	
a66aa2dd sago007 2018-04-27 10:00 109
};
a66aa2dd sago007 2018-04-27 10:00 110
e3e89d02 sago007 2018-04-26 06:50 111
HorizontalSwitchAnimation switchAnimation;
e3e89d02 sago007 2018-04-26 06:50 112
sago::SagoTextField switchAnimationField;
8f6cc9b3 sago007 2018-04-29 15:12 113
sago::SagoTextField clearRowfield;
8f6cc9b3 sago007 2018-04-29 15:12 114
sago::SagoTextField comboField;
8f6cc9b3 sago007 2018-04-29 15:12 115
sago::SagoTextField dropField;
8f6cc9b3 sago007 2018-04-29 15:12 116
sago::SagoTextField chainField;
8f6cc9b3 sago007 2018-04-29 15:12 117
8f6cc9b3 sago007 2018-04-29 15:12 118
static void InitTextField(sago::SagoTextField& field, const char* text) {
8f6cc9b3 sago007 2018-04-29 15:12 119
	field.SetHolder(&globalData.spriteHolder->GetDataHolder());
8f6cc9b3 sago007 2018-04-29 15:12 120
	field.SetFontSize(30);
8f6cc9b3 sago007 2018-04-29 15:12 121
	field.SetOutline(2, {0,0,0,255});
8f6cc9b3 sago007 2018-04-29 15:12 122
	field.SetText(text);
8f6cc9b3 sago007 2018-04-29 15:12 123
}
e3e89d02 sago007 2018-04-26 06:50 124
d8fe60bd sago007 2018-04-02 16:15 125
HelpHowtoState::HelpHowtoState() {
8f6cc9b3 sago007 2018-04-29 15:12 126
	InitTextField(switchAnimationField, _("Switch block horizontally"));
8f6cc9b3 sago007 2018-04-29 15:12 127
	InitTextField(clearRowfield, _("Match 3 to clear"));
8f6cc9b3 sago007 2018-04-29 15:12 128
	InitTextField(comboField, _("Create combos!"));
8f6cc9b3 sago007 2018-04-29 15:12 129
	InitTextField(dropField, _("Drop blocks!"));
8f6cc9b3 sago007 2018-04-29 15:12 130
	InitTextField(chainField, _("Create a chain effect"));
d8fe60bd sago007 2018-04-02 16:15 131
}
d8fe60bd sago007 2018-04-02 16:15 132
d8fe60bd sago007 2018-04-02 16:15 133
HelpHowtoState::~HelpHowtoState() {
d8fe60bd sago007 2018-04-02 16:15 134
}
d8fe60bd sago007 2018-04-02 16:15 135
d8fe60bd sago007 2018-04-02 16:15 136
bool HelpHowtoState::IsActive() {
d8fe60bd sago007 2018-04-02 16:15 137
	return isActive;
d8fe60bd sago007 2018-04-02 16:15 138
}
d8fe60bd sago007 2018-04-02 16:15 139
d8fe60bd sago007 2018-04-02 16:15 140
void HelpHowtoState::ProcessInput(const SDL_Event& event, bool& processed) {
d8fe60bd sago007 2018-04-02 16:15 141
d8fe60bd sago007 2018-04-02 16:15 142
	UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
d8fe60bd sago007 2018-04-02 16:15 143
d8fe60bd sago007 2018-04-02 16:15 144
	if (isConfirmEvent(event) || isEscapeEvent(event)) {
d8fe60bd sago007 2018-04-02 16:15 145
		isActive = false;
d8fe60bd sago007 2018-04-02 16:15 146
		processed = true;
d8fe60bd sago007 2018-04-02 16:15 147
	}
d8fe60bd sago007 2018-04-02 16:15 148
}
d8fe60bd sago007 2018-04-02 16:15 149
d8fe60bd sago007 2018-04-02 16:15 150
void HelpHowtoState::Draw(SDL_Renderer* target) {
d8fe60bd sago007 2018-04-02 16:15 151
	DrawBackground(target);
e3e89d02 sago007 2018-04-26 06:50 152
	RenderRowOfBricks(target, switchAnimation.brickStr, 50, 50);
e3e89d02 sago007 2018-04-26 06:50 153
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+switchAnimation.cursorPos*50, 50);
8f6cc9b3 sago007 2018-04-29 15:12 154
	switchAnimationField.Draw(target, 50 +150+30, 50+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
a66aa2dd sago007 2018-04-27 10:00 155
	RenderRowOfBricks(target, "adaa", 50, 150);
a66aa2dd sago007 2018-04-27 10:00 156
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 150);
a66aa2dd sago007 2018-04-27 10:00 157
	RenderRowOfBricks(target, "dAAA", 50+300, 150);
8f6cc9b3 sago007 2018-04-29 15:12 158
	clearRowfield.Draw(target, 600, 150+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
8f6cc9b3 sago007 2018-04-29 15:12 159
	comboField.Draw(target, 50+175, 410, sago::SagoTextField::Alignment::center);
a66aa2dd sago007 2018-04-27 10:00 160
	MultiLineBlocks().addLine("ab").addLine("ba").addLine("ab").Render(target, 50, 250);
a66aa2dd sago007 2018-04-27 10:00 161
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 250+50);
a66aa2dd sago007 2018-04-27 10:00 162
	MultiLineBlocks().addLine("AB").addLine("AB").addLine("AB").Render(target, 50+200, 250);
a66aa2dd sago007 2018-04-27 10:00 163
	MultiLineBlocks().addLine("a").addLine("b").addLine("e").Render(target, 50+400, 250);
a66aa2dd sago007 2018-04-27 10:00 164
	globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+400, 250);
a66aa2dd sago007 2018-04-27 10:00 165
	MultiLineBlocks().addLine(" ").addLine("b").addLine("ea").Render(target, 50+400+200, 250);
8f6cc9b3 sago007 2018-04-29 15:12 166
	dropField.Draw(target, 50+400+150, 410, sago::SagoTextField::Alignment::center);
a66aa2dd sago007 2018-04-27 10:00 167
	MultiLineBlocks().addLine(" d").addLine(" f").addLine(" f").addLine("fdd").Render(target, 50, 500);
a66aa2dd sago007 2018-04-27 10:00 168
	MultiLineBlocks().addLine(" d").addLine(" F").addLine(" F").addLine("dFd").Render(target, 50+200, 500);
a66aa2dd sago007 2018-04-27 10:00 169
	MultiLineBlocks().addLine(" d").addLine("  ").addLine("  ").addLine("d d").Render(target, 50+200*2, 500);
a66aa2dd sago007 2018-04-27 10:00 170
	MultiLineBlocks().addLine("  ").addLine("  ").addLine("  ").addLine("DDD").Render(target, 50+200*3, 500);
8f6cc9b3 sago007 2018-04-29 15:12 171
	chainField.Draw(target, 400, 710, sago::SagoTextField::Alignment::center);
d4f28be2 sago007 2018-04-24 09:33 172
	bExit.Draw(globalData.screen, SDL_GetTicks(), xsize-buttonOffset, ysize-buttonOffset);
d8fe60bd sago007 2018-04-02 16:15 173
}
d8fe60bd sago007 2018-04-02 16:15 174
d8fe60bd sago007 2018-04-02 16:15 175
void HelpHowtoState::Update() {
d4f28be2 sago007 2018-04-24 09:33 176
	// If the mouse button is released, make bMouseUp equal true
d4f28be2 sago007 2018-04-24 09:33 177
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
d4f28be2 sago007 2018-04-24 09:33 178
		bMouseUp=true;
d4f28be2 sago007 2018-04-24 09:33 179
	}
d4f28be2 sago007 2018-04-24 09:33 180
d4f28be2 sago007 2018-04-24 09:33 181
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
d4f28be2 sago007 2018-04-24 09:33 182
		bMouseUp = false;
d4f28be2 sago007 2018-04-24 09:33 183
d4f28be2 sago007 2018-04-24 09:33 184
		//The Score button:
d4f28be2 sago007 2018-04-24 09:33 185
		if ((globalData.mousex>xsize-buttonOffset) && (globalData.mousex<xsize-buttonOffset+bExit.GetWidth()) 
d4f28be2 sago007 2018-04-24 09:33 186
				&& (globalData.mousey>ysize-buttonOffset) && (globalData.mousey<ysize-buttonOffset+bExit.GetHeight())) {
d4f28be2 sago007 2018-04-24 09:33 187
			isActive = false;
d4f28be2 sago007 2018-04-24 09:33 188
		}
d4f28be2 sago007 2018-04-24 09:33 189
d4f28be2 sago007 2018-04-24 09:33 190
	}
e3e89d02 sago007 2018-04-26 06:50 191
	switchAnimation.Update(SDL_GetTicks());
d8fe60bd sago007 2018-04-02 16:15 192
}