summaryrefslogtreecommitdiff
path: root/src/wm_path_manager.cpp
blob: d94e5bea306eeaaf5dd5ec7ff8096888ebacac7b (plain)
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
#include "worldmap.h"

dWMPathManager_c *dWMPathManager_c::instance = 0;

dWMPathManager_c *dWMPathManager_c::build() {
	OSReport("Creating WM_PathManager\n");

	void *buffer = AllocFromGameHeap1(sizeof(dWMPathManager_c));
	dWMPathManager_c *c = new(buffer) dWMPathManager_c;

	OSReport("Created WM_PathManager @ %p\n", c);

	instance = c;
	return c;
}



dWMPathManager_c::dWMPathManager_c() {
}


int dWMPathManager_c::onCreate() {
	return true;
}


int dWMPathManager_c::onDelete() {
	return true;
}


int dWMPathManager_c::onExecute() {
	if (!dScNewerWorldMap_c::instance->canDoStuff()) return true;

	int heldButtons = Remocon_GetButtons(GetActiveRemocon());
	int nowPressed = Remocon_GetPressed(GetActiveRemocon());

	if (isMoving) {
		moveThroughPath();
	} else {
		if (nowPressed & WPAD_LEFT && currentPoint->exits.asDirection.left.isValid())
			startMovementTo(LEFT);
		else if (nowPressed & WPAD_RIGHT && currentPoint->exits.asDirection.right.isValid())
			startMovementTo(RIGHT);
		else if (nowPressed & WPAD_UP && currentPoint->exits.asDirection.up.isValid())
			startMovementTo(UP);
		else if (nowPressed & WPAD_DOWN && currentPoint->exits.asDirection.down.isValid())
			startMovementTo(DOWN);
		else if (nowPressed & WPAD_TWO)
			activatePoint();
	}

	return true;
}


void dWMPathManager_c::moveThroughPath() {
	// figure out how much to move on each step
	Vec from, to;
	if (reverseThroughPath) {
		from = currentSegment->end;
		to = currentSegment->start;
	} else {
		from = currentSegment->start;
		to = currentSegment->end;
	}

	Vec move;
	move.x = to.x - from.x;
	move.y = to.y - from.y;
	move.z = to.z - from.z;

	VECNormalize(&move, &move);
	VECScale(&move, &move, currentSegment->stepsPerFrame);

	daWMPlayer_c *player = daWMPlayer_c::instance;
	player->pos.x += move.x;
	player->pos.y += move.y;
	player->pos.z += move.z;

	// have we reached the end?
	bool xAtEnd = false;
	bool yAtEnd = false;
	bool zAtEnd = false;

	if (move.x > 0) {
		xAtEnd = (player->pos.x >= to.x);
	} else {
		xAtEnd = (player->pos.x <= to.x);
	}

	if (move.y > 0) {
		yAtEnd = (player->pos.y >= to.y);
	} else {
		yAtEnd = (player->pos.y <= to.y);
	}

	if (move.z > 0) {
		zAtEnd = (player->pos.z >= to.z);
	} else {
		zAtEnd = (player->pos.z <= to.z);
	}

	if (xAtEnd && yAtEnd && zAtEnd) {
		MapReport("reached end of segment %d\n", currentSegmentID);

		int nextSegment = currentSegmentID + (reverseThroughPath ? -1 : 1);
		if (nextSegment == -1 || nextSegment == currentPath->segCount) {
			MapReport("reached end of path\n");

			currentPoint = nextPoint;
			player->pos = currentPoint->position;
			player->startAnimation(0, 1.2, 10.0, 0.0);
			isMoving = false;

			SaveBlock *save = GetSaveFile()->GetBlock(-1);
			//save->current_world = newPage; ?
			save->current_path_node = pathData.getPointID(currentPoint);

			dWMHud_c::instance->updateText();
		} else {
			moveToSegment(nextSegment);
		}
	}
}


void dWMPathManager_c::startMovementTo(WMDirection direction) {
	isMoving = true;

	WMPathEntrance *thisExit = &currentPoint->exits.asArray[direction];
	MapReport("Using an exit in direction %d\n", direction);

	currentPath = thisExit->path;
	if (thisExit->isEndSide) {
		nextPoint = thisExit->path->startPoint;
		reverseThroughPath = true;
		moveToSegment(thisExit->path->segCount - 1);
	} else {
		nextPoint = thisExit->path->endPoint;
		reverseThroughPath = false;
		moveToSegment(0);
	}
}

void dWMPathManager_c::moveToSegment(int id) {
	MapReport("Moving to segment %d\n", id);

	currentSegmentID = id;
	currentSegment = currentPath->segments[id];

	// calculate rotation
	Vec from, to;
	if (reverseThroughPath) {
		from = currentSegment->end;
		to = currentSegment->start;
	} else {
		from = currentSegment->start;
		to = currentSegment->end;
	}

	MapReport("From: %f,%f,%f To: %f,%f,%f\n", from.x, from.y, from.z, to.x, to.y, to.z);

	daWMPlayer_c *player = daWMPlayer_c::instance;
	player->pos = from;

	// update rotation
	if (!currentSegment->useLastDir) {
		player->rot.x = 0;
		player->rot.y = currentSegment->direction;
		player->rot.z = 0;

		if (reverseThroughPath && !currentSegment->alwaysSameDir) {
			player->rot.y = ((currentSegment->direction) + 0x8000) & 0xFFFF;
		}
	}

	player->startAnimation(currentSegment->animationType, currentSegment->animationSpeed, 10.0, 0.0);
}

void dWMPathManager_c::activatePoint() {
	MapReport("Point activated!\n");
	daWMPlayer_c::instance->startAnimation(170, 1.2, 10.0, 0.0);

	if (currentPoint->type == WMPathPoint::LEVEL_TYPE) {
		int w = currentPoint->params[0] - 1;
		int l = currentPoint->params[1] - 1;
		LevelInfo::Entry *level = dScNewerWorldMap_c::instance->levelInfo.search(w, l);
		dScNewerWorldMap_c::instance->startLevel(level);
	}
}


int dWMPathManager_c::onDraw() {
	return true;
}


void dWMPathManager_c::setup() {
	pathData.load(dScNewerWorldMap_c::instance->resMng['PATH']);

	SaveBlock *save = GetSaveFile()->GetBlock(-1);
	if (save->current_path_node >= pathData.pointCount()) {
		currentPoint = pathData.getPath(0)->startPoint;
	} else {
		currentPoint = pathData.getPoint(save->current_path_node);
	}

}