File: untangler.cpp

package info (click to toggle)
brainparty 0.61%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,624 kB
  • sloc: cpp: 12,991; objc: 252; makefile: 61
file content (284 lines) | stat: -rw-r--r-- 9,642 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)

// Brain Party is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "untangler.h"
#include "Minigame.h"
		
BPMiniGame_Untangler::BPMiniGame_Untangler(BPGame* game) : BPMiniGame(game) {
	SelectedPoint = NULL;
	sfcCircleHandle = TheGame->LoadBitmap("circle_handle", 16, 16);
	sfcBackground = TheGame->LoadBitmap("stars_glow", 320, 416);
	
	LastMoveTime = CurrentLevel = TimeStarted = 0;
	SuccessTime = -1;
	
	GenerateStarfield(Stars);
	
	GameTitle = "Untangler";
	GameHelp = "Drag the line ends around so the lines don't cross. You need to ensure that none of the lines cross to advance!";
	GameHelp2 = "The white circles at the ends of each line can be moved. If a line is green, it means it doesn't cross any other line. If it is red, it's crossing at least one other line. You need to move the line ends until no lines cross any others.";
	
	MiniGameType = PUZZLE;
	
	LevelUp();
}

BPMiniGame_Untangler::~BPMiniGame_Untangler() {
	TanglePoints.Clear();
	Stars.Clear();
	
	SAFE_DELETE(SelectedPoint);
	
	SAFE_DELETE(sfcCircleHandle);
	SAFE_DELETE(sfcBackground);
}

void BPMiniGame_Untangler::Start() {
	TimeStarted = TheGame->TickCount;
}

int BPMiniGame_Untangler::GetWeight() {
	float TimePassed = (TheGame->TickCount - TimeStarted) / 1000.0f;
	return MinMax(530 - floor(TimePassed * 1.1));
}

void BPMiniGame_Untangler::Render() {
	TheGame->DrawImage(sfcBackground, 0, 0);
	
	DrawStarfield(Stars);
	
	bool p1_collides;
	bool p2_collides;
	
	bool any_collisions = false;
	
	for (int i = 0; i < TanglePoints.Count; ++i) {
		BPMiniGame_Untangler_Point* point = TanglePoints[i];
		
		p1_collides = false;
		p2_collides = false;
		
		// does this line collide with any other?
		for (int j = 0; j < TanglePoints.Count; ++j) {
			BPMiniGame_Untangler_Point* other_point = TanglePoints[j];
			//if (point == other_point) continue;
			
			// if our first point collides with the other first point
			if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect1->Pos.X, point->Connect1->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect1->Pos.X, other_point->Connect1->Pos.Y) == 1) {
				p1_collides = true;
			} else {
				// if our first point collides with the other second point
				if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect1->Pos.X, point->Connect1->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect2->Pos.X, other_point->Connect2->Pos.Y) == 1) {
					p1_collides = true;
				}
			}
			
			// if our second point collides with the other first point
			if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect2->Pos.X, point->Connect2->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect1->Pos.X, other_point->Connect1->Pos.Y) == 1) {
				p2_collides = true;
			} else if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect2->Pos.X, point->Connect2->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect2->Pos.X, other_point->Connect2->Pos.Y) == 1) {
				// if our second point collides with the other second point
				p2_collides = true;
			}
		}
		
		if (p1_collides) {
			any_collisions = true;
			TheGame->DrawLine(point->Pos.X, point->Pos.Y, point->Connect1->Pos.X, point->Connect1->Pos.Y, TheGame->Red, 3.0f);
		} else {
			TheGame->DrawLine(point->Pos.X, point->Pos.Y, point->Connect1->Pos.X, point->Connect1->Pos.Y, TheGame->Green, 3.0f);
		}
		
		if (p2_collides) {
			any_collisions = true;
			TheGame->DrawLine(point->Pos.X, point->Pos.Y, point->Connect2->Pos.X, point->Connect2->Pos.Y, TheGame->Red, 3.0f);
		} else {
			TheGame->DrawLine(point->Pos.X, point->Pos.Y, point->Connect2->Pos.X, point->Connect2->Pos.Y, TheGame->Green, 3.0f);
		}
	}

	// draw blocks separately to the lines so they always appear on top
	for (int i = 0; i < TanglePoints.Count; ++i) {
		BPMiniGame_Untangler_Point* point = TanglePoints[i];
		TheGame->DrawImage(sfcCircleHandle, point->Pos.X - 8, point->Pos.Y - 10);
	}
	
	if (SelectedPoint == NULL && !any_collisions && LastMoveTime + 500 < TheGame->TickCount) {
		// success!
		TheGame->PlaySound("correct");
		LevelUp();
	}
}

void BPMiniGame_Untangler::Tick() {
	if (SuccessTime != -1 && SuccessTime + 250 < TheGame->TickCount) {
		Success();
		return;
	}
	
	UpdateStarfield(Stars);
}

void BPMiniGame_Untangler::OnMouseUp() {
	if (SelectedPoint != NULL) {
		// don't let people drag the points off the screen!
		if (SelectedPoint->Pos.X < Size) {
			SelectedPoint->Pos.X = Size;
		} else if (SelectedPoint->Pos.X > MiniGameWidth - Size) {
			SelectedPoint->Pos.X = MiniGameWidth - Size;
		}
		
		if (SelectedPoint->Pos.Y < Size) {
			SelectedPoint->Pos.Y = Size;
		} else if (SelectedPoint->Pos.Y > 416 - Size) {
			SelectedPoint->Pos.Y = 416 - Size;
		}
	}
	
	SelectedPoint = NULL;
	LastMoveTime = TheGame->TickCount;
}

void BPMiniGame_Untangler::OnMouseDown() {
	BPMiniGame_Untangler_Point* closest = NULL;
	float distance = 999.0f;
	
	for (int i = 0; i < TanglePoints.Count; ++i) {
		BPMiniGame_Untangler_Point* point = TanglePoints[i];
		
		float thisdist = BPPoint::DistanceSquared(BPPoint(TouchEvent.X, TouchEvent.Y), BPPoint(point->Pos.X, point->Pos.Y));
		
		if (thisdist < distance) {
			closest = point;
			distance = thisdist;
		}
		
//		if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, point->Pos.X - (HalfSize + 8), point->Pos.Y - (HalfSize + 8), (Size + 16), (Size + 16))) {
//			SelectedPoint = point;
//			break;
//		}
	}
	
	if (distance < 2304.0f) { // 48 * 48
		SelectedPoint = closest;
	}
}

void BPMiniGame_Untangler::OnMouseMove() {
	if (SelectedPoint != NULL) {
		SelectedPoint->Pos = TouchEvent;
	}
}

float BPMiniGame_Untangler::abs2(float x) {
	if (x < 0) { return -x; }
	return x;
}

int BPMiniGame_Untangler::LinesCross(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) {
	float d = (x1 - x0) * (y3 - y2) - (y1 - y0) * (x3 - x2);
	if (abs2(d) < 0.001) { return -1; }
	float AB = ((y0 - y2) * (x3 - x2) - (x0 - x2) * (y3 - y2)) / d;
	if (AB > 0.0 && AB < 1.0) {
		float CD = ((y0 - y2) * (x1 - x0) - (x0 - x2) * (y1 - y0)) / d;
		if (CD > 0.0 && CD < 1.0) {
			//linx = x0 + AB * (x1 - x0);
			//liny = y0 + AB * (y1 - y0);
			return 1;
		}
	}
	return 0;
}

void BPMiniGame_Untangler::LevelUp() {
	if (!MarathonMode && CurrentLevel >= 8 && SuccessTime == -1) {
		SuccessTime = TheGame->TickCount;
		return;
	}
	
	if (SuccessTime != -1) {
		// we've finished - bail out!
		return;
	}
	
	++CurrentLevel;
	
	do {	
		TanglePoints.Clear();
		
		// first, add all the lines
		for (int i = 0; i < CurrentLevel + 5; ++i) {
			BPMiniGame_Untangler_Point* line = new BPMiniGame_Untangler_Point();
			line->Pos.X = TheGame->RandomRange(5, 235);
			line->Pos.Y = TheGame->RandomRange(5, 290);
			TanglePoints.Add(line);
		}
		
		for (int i = 0; i < TanglePoints.Count; ++i) {
			if (i == 0) {
				// this is the first item; set its Connect1 value to be the last item
				TanglePoints[i]->Connect1 = TanglePoints[TanglePoints.Count - 1];
			} else {
				TanglePoints[i]->Connect1 = TanglePoints[i - 1];
			}
			
			if (i == TanglePoints.Count - 1) {
				// this is the last item; set its Connect2 value to be the first item
				TanglePoints[i]->Connect2 = TanglePoints[0];
			} else {
				TanglePoints[i]->Connect2 = TanglePoints[i + 1];
			}
		}
	} while (!AnyCollisions());
}

bool BPMiniGame_Untangler::AnyCollisions() {
	for (int i = 0; i < TanglePoints.Count; ++i) {
		BPMiniGame_Untangler_Point* point = TanglePoints[i];
		
		// does this line collide with any other?
		for (int j = 0; j < TanglePoints.Count; ++j) {
			BPMiniGame_Untangler_Point* other_point = TanglePoints[j];
			//if (point == other_point) continue;
			
			// if our first point collides with the other first point
			if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect1->Pos.X, point->Connect1->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect1->Pos.X, other_point->Connect1->Pos.Y) == 1) {
				return true;
			} else {
				// if our first point collides with the other second point
				if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect1->Pos.X, point->Connect1->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect2->Pos.X, other_point->Connect2->Pos.Y) == 1) {
					return true;
				}
			}
			
			// if our second point collides with the other first point
			if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect2->Pos.X, point->Connect2->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect1->Pos.X, other_point->Connect1->Pos.Y) == 1) {
				return true;
			} else if (LinesCross(point->Pos.X, point->Pos.Y, point->Connect2->Pos.X, point->Connect2->Pos.Y, other_point->Pos.X, other_point->Pos.Y, other_point->Connect2->Pos.X, other_point->Connect2->Pos.Y) == 1) {
				// if our second point collides with the other second point
				return true;
			}
		}
	}
	
	// if we're stil here, there are no collisions
	return false;
}

void BPMiniGame_Untangler::SetMarathon() {
	MarathonMode = true;
}