summaryrefslogtreecommitdiff
path: root/src/koopatlas/player.cpp
blob: 1c68222017a499ca5b0677a7db362d03e62a9629 (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
#include "koopatlas/player.h"

daWMPlayer_c *daWMPlayer_c::instance;
static const char *mdlNames[] = {"MB_model", "SMB_model", "PLMB_model", "PMB_model"};
static const char *patNames[] = {"PB_switch_swim", "PB_switch_swim", "PLMB_switch_swim", "PLB_switch_swim"};

int daWMPlayer_c::onCreate() {

	this->modelHandler = new dPlayerModelHandler_c(0);
	// loadModel(u8 player_id, int powerup_id, int unk);
	// Unk is some kind of mode: 0=in-game, 1=map, 2=2D

	// Here we do a bit of a hack
	//this->modelHandler->loadModel(0, 3, 1);
	dPlayerModel_c *pm = (dPlayerModel_c*)modelHandler->mdlClass;

	pm->mode_maybe = 1;
	pm->player_id_1 = 0;
	pm->allocator.link(0xC000, GameHeaps[0], 0, 0x20);
	pm->prepare();

	for (int i = 0; i < 4; i++) {
		nw4r::g3d::ResMdl mdl = pm->modelResFile.GetResMdl(mdlNames[i]);
		nw4r::g3d::ResAnmTexPat pat = pm->modelResFile.GetResAnmTexPat(patNames[i]);

		pats[i].setup(mdl, pat, &pm->allocator, 0, 1);
	}

	pm->allocator.unlink();
	pm->setPowerup(3);
	pm->finaliseModel();

	pm->startAnimation(0, 1.2, 10.0, 0.0);
	modelHandler->setSRT((Vec){0.0,100.0,-100.0}, (S16Vec){0,0,0}, (Vec){2.0,2.0,2.0});

	hammerSuit.setup(this->modelHandler);

	pos = (Vec){0.0f,0.0f,3000.0f};
	rot = (S16Vec){0x1800,0,0};
	scale = (Vec){1.6f,1.6f,1.6f};

	hasEffect = false;
	hasSound = false;
	step = false;
	effectName = "";
	soundName = 0;
	timer = 0;
	jumpOffset = 0.0;

	// -1 or 0xC000? normally we use -1 but Player uses 0xC000....
	//allocator.link(0xC000, GameHeaps[0], 0, 0x20);
	//allocator.unlink();

	return true;
}

int daWMPlayer_c::onDelete() {
	delete modelHandler;

	return true;
}


int daWMPlayer_c::onExecute() {
	if (Player_Flags[0] & 1) {
		modelHandler->mdlClass->enableStarColours();
		modelHandler->mdlClass->enableStarEffects();
		dKPMusic::playStarMusic();
	}

	if (spinning)
		rot.y += 0xC00;
	else
		SmoothRotation(&rot.y, targetRotY, 0xC00);

	if (dScKoopatlas_c::instance->mapIsRunning())
		dScKoopatlas_c::instance->pathManager.execute();

	this->modelHandler->update();
	pats[((dPlayerModel_c*)modelHandler->mdlClass)->currentPlayerModelID].process();

	mMtx myMatrix;
	myMatrix.scale(scale.x, scale.y, scale.z);
	myMatrix.applyTranslation(pos.x, pos.y + jumpOffset, pos.z);
	if (dScKoopatlas_c::instance->warpZoneHacks && (currentAnim == jump || currentAnim == jumped))
		myMatrix.applyTranslation(0, 0, 600.0f);
	myMatrix.applyRotationX(&rot.x);
	myMatrix.applyRotationY(&rot.y);
	// Z is unused for now
	modelHandler->setMatrix(myMatrix);

	if (dScKoopatlas_c::instance->mapIsRunning()) {
		if (hasEffect) { 
			Vec effPos = {pos.x, pos.y, 3300.0f};
			effect.spawn(effectName, 0, &effPos, &rot, &scale);
		}

		if (hasSound) {
			timer++;

			if (timer == 12) {
				if (step) { MapSoundPlayer(SoundRelatedClass, soundName, 1); step = false; }
				else { MapSoundPlayer(SoundRelatedClass, soundName+1, 1); step = true; }
				timer = 0;
			}

			if (timer > 12) { timer = 0; }
		}
	}

	return true;
}

int daWMPlayer_c::onDraw() {
	if (!visible)
		return true;

	this->modelHandler->draw();
	hammerSuit.draw();

	return true;
}


void daWMPlayer_c::startAnimation(int id, float frame, float unk, float updateRate) {
	if (id == currentAnim && frame == currentFrame && unk == currentUnk && updateRate == currentUpdateRate)
		return;

	bool isOldSwimming = (currentAnim == swim_wait);
	bool isNewSwimming = (id == swim_wait);

	currentAnim = id;
	currentFrame = frame;
	currentUnk = unk;
	currentUpdateRate = updateRate;
	this->modelHandler->mdlClass->startAnimation(id, frame, unk, updateRate);

	if (isOldSwimming != isNewSwimming)
		bindPats();
}

void daWMPlayer_c::bindPats() {
	dPlayerModel_c *pm = (dPlayerModel_c*)modelHandler->mdlClass;
	int id = pm->currentPlayerModelID;

	static const float frames[] = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, 3.0f};
	float frame = frames[pm->powerup_id];
	if (currentAnim == swim_wait)
		frame += (pm->powerup_id == 4 || pm->powerup_id == 5) ? 1.0f : 4.0f;

	nw4r::g3d::ResAnmTexPat pat = pm->modelResFile.GetResAnmTexPat(patNames[id]);
	pats[id].bindEntry(
			&pm->models[id].body,
			&pat,
			0, 4);
	pats[id].setUpdateRateForEntry(0.0f, 0);
	pats[id].setFrameForEntry(frame, 0);

	pm->models[id].body.bindAnim(&pats[id]);
}


daWMPlayer_c *daWMPlayer_c::build() {

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


	instance = c;
	return c;
}