aboutsummaryrefslogtreecommitdiff
path: root/src/entities/antifaShip.c
blob: 342f0f91dbc8208a736327c21f1d8c0a3a4c9e5c (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
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
285
286
287
288
289
290
291
292
#include "antifaShip.h"
#include "game.h"
#include "settings.h"
#include "bullets.h"
#include "assets.h"

void initAntifaShip(Entity * entity, Game * game) {
	entity->model = &game->assets.models[ANTIFA_SHIP_ASSET];
	entity->collisionModel = entityCreateCollisionModel(*entity->model);
	entity->transformedCollisionModel = entityCreateCollisionModel(*entity->model);
	setEntityRadius(entity);

	// Acceleration stuff.
	entity->useAcceleration = true;
	entity->acceleration = (EntityAcceleration){
		.speedUp = 30.0,
		.speedDown = 15,
		.rotation = (Vector3){2000000.0, 2000000.0, 2000000.0}
	};

	// Set Data pointer.
	entity->data = KF_MALLOC(sizeof(AntifaShip));

	if (entity->data == NULL) {
		ALLOCATION_ERROR;
		return;
	}

	AntifaShip * data = (AntifaShip*)entity->data;
	data->lastMouse = GetMousePosition();
	data->forwardSpeed = 0.0;
	data->shouldInitMousePosition = true;
	data->timeSinceLastBullet = GetTime();
	data->doAutoTarget = false;
	data->targetedEntityId = ENTITY_NONE;
	data->isOnTarget = false;
}

// Go back to burger king you elon musking loving mother fucker!

void closeAntifaShip(Entity * entity) {
	if (entity->data != NULL)
		KF_FREE(entity->data);

	entityFreeCollisionModel(entity->collisionModel);
	entityFreeCollisionModel(entity->transformedCollisionModel);
}

float getAntifaShipAimDistance(Entity * ship, Entity * targetEntity) {
	Vector3 directionAiming = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, ship->rotation);
	Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, ship->position));
	return Vector3Distance(directionAiming, directionToTarget);
}

// Returns closest entity to ship and in range or none.
EntityId getShipInRangeAntifaShip(Game * game, Entity * entity) {
	int i;

	// Needs at least two entities to work correctly.
	if (game->world.entitiesCount < 2)
		return ENTITY_NONE;

	// Start out with entity 1 as closest.
	Entity * currentEntity = &game->world.entities[1];
	EntityId closestId = ENTITY_NONE;
	float closestDistance = (float)INT_MAX;
	float distance;

	// This entity will only ever be at id 0 so skip it.
	for (i = 1; i < game->world.entitiesCount; ++i) {

		// Get entity and distance.
		currentEntity = &game->world.entities[i];
		distance = Vector3Distance(currentEntity->position, entity->position);

		// Is closest and in range.
		if (distance < closestDistance && getAntifaShipAimDistance(entity, currentEntity) <= ANTIFA_START_AUTO_TARGET_MAX) {
			closestDistance = distance;
			closestId = currentEntity->id;
		}
	}

	return closestId;
}

void updateAntifaShipTarget(Game * game, Entity * entity) {
	float t = GetFrameTime();
	AntifaShip * data = (AntifaShip*)entity->data;

	if (data->doAutoTarget) {
		Entity * targetEntity = getEntityFromWorld(game->world, data->targetedEntityId);

		// Lost target.
		if (targetEntity == NULL) {
			data->doAutoTarget = false;
			return;
		} else if (targetEntity->fingerprint != data->targetedEntityFingerprint) {
			data->doAutoTarget = false;
			return;
		}

		// If to far from target.
		float aimDistance = getAntifaShipAimDistance(entity, targetEntity);

		if (aimDistance > ANTIFA_START_AUTO_TARGET_MAX) {
			data->doAutoTarget = false;
			return;
		}

		Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, entity->position));

		// Update target.
		float speed = ANTIFA_TARGETING_SPEED / aimDistance;
		data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, speed * t);
		//printf("%f\n", speed);

		// Is on target.
		data->isOnTarget = traceRayToEntity(*targetEntity, (Ray){entity->position, data->gunTarget}).hit;
	} else {
		data->isOnTarget = false;
		data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
	}
}

void toggleAntifaShipAutoTarget(Game * game, Entity * entity) {
	AntifaShip * data = (AntifaShip*)entity->data;

	// Turn off auto target.
	if (data->doAutoTarget) {
		data->doAutoTarget = false;
		return;
	}

	EntityId closestId = getShipInRangeAntifaShip(game, entity);

	if (closestId == ENTITY_NONE)
		return;

	data->targetedEntityId = closestId;
	Entity * closestEntity = getEntityFromWorld(game->world, closestId);
	data->targetedEntityFingerprint = closestEntity->fingerprint;

	data->doAutoTarget = true;
}

// This fucker will fire a bullet!!!
void fireBulletAntifa(Game * game, Entity * entity) {
	double t = GetTime();
	AntifaShip * data = (AntifaShip*)entity->data;

	// Let it cool down.
	if (t - data->timeSinceLastBullet < ANTIFA_BULLET_COOLDOWN)
		return;

	Bullet bullet = createBulletFromDirection(*entity, data->gunTarget, ANTIFA_BULLET_DAMAGE);
	BulletHitInfo info = shootBullet(&game->world, bullet);
	data->lastBulletShot = bullet;

	if (info.hit) {
		Entity * hitEntity = getEntityFromWorld(game->world, info.hitId);

		// We were the fucker that killed this low iq fascist!
		if (hitEntity->health <= 0.0)
			hitEntity->killedByPlayer = true;
	}

	data->timeSinceLastBullet = t;
}

void controlAntifaShipJoystick(Game * game, Entity * entity) {
	Settings settings = game->settings;
	int gamePadNum = settings.gamePadNum;

	// Get joystick values.
	float pitchStick = GetGamepadAxisMovement(gamePadNum, settings.pitchStick);
	float yawStick = GetGamepadAxisMovement(gamePadNum, settings.yawStick);
	float rollStick = GetGamepadAxisMovement(gamePadNum, settings.rollStick);
	float speedStick = GetGamepadAxisMovement(gamePadNum, settings.speedStick);

	// Shoot button.
	if (IsGamepadButtonPressed(gamePadNum, 8))
		fireBulletAntifa(game, entity);

	Vector3 stick = (Vector3){
		pitchStick,
		-yawStick,
		rollStick
	};

	stick = Vector3Scale(stick, settings.joystickSensitivity);
	float speed = fabs(speedStick * ANTIFA_SHIP_MAX_SPEED);

	entityJoystickControl(entity, stick, speed);
}

void controlAntifaShipKeyboardAndMouse(Game * game, Entity * entity) {
	AntifaShip * data = (AntifaShip*)entity->data;

	// Resets mouse on init.
	if (data->shouldInitMousePosition) {
		data->shouldInitMousePosition = false;
		SetMousePosition(0, 0);
		data->lastMouse = Vector2Zero();
	}

#ifdef DEBUG_KEYS
	// Kill me.
	if (IsKeyPressed(KEY_K))
		entity->health = 0.0;
	// Kill everyone else.
	if (IsKeyPressed(KEY_L))
		for (int i = 1; i < game->world.entitiesCount; ++i)
			game->world.entities[i].health = 0.0;
#endif

	// Shoot bullet.
	if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))
		toggleAntifaShipAutoTarget(game, entity);
	if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
		fireBulletAntifa(game, entity);

	// Get mouse values.
	Vector2 mouse = GetMousePosition();
	float speed = GetMouseWheelMove();
	
	data->forwardSpeed += (speed * game->settings.scrollBarSpeed);

	if (data->forwardSpeed < 0.)
		data->forwardSpeed = 0.0;
	else if (data->forwardSpeed > ANTIFA_SHIP_MAX_SPEED)
		data->forwardSpeed = ANTIFA_SHIP_MAX_SPEED;

	Vector2 v = Vector2Subtract(mouse, data->lastMouse);
	v = Vector2Scale(v, 1.0 / GetFrameTime());
	data->lastMouse = mouse;

	// Using mouse as a joystick.
	Vector3 mouseStick = (Vector3){v.y, -v.x, 0.0};
	mouseStick = Vector3Scale(mouseStick, game->settings.mouseSensitivity);

	// Swap axis for more movement with mouse.
	if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE)) {
		mouseStick.z = -mouseStick.y;
		mouseStick.y = 0.0;
	}

	entityJoystickControl(entity, mouseStick, data->forwardSpeed);
}

void updateAntifaShip(Game * game, Entity * entity) {
	entityUpdateLastValues(entity);

	updateAntifaShipTarget(game, entity);

	switch (game->settings.controlMode) {
		case JOYSTICK_CONTROL:
			controlAntifaShipJoystick(game, entity);
			break;
		case KEYBOARD_AND_MOUSE_CONTROL:
			controlAntifaShipKeyboardAndMouse(game, entity);
			break;
		default:
			break;
	}

	//printf("%f\n", entity->health);

	entityCheckTransformedCollisionModel(entity);
}

void drawAntifaShip(Game * game, Entity * entity) {
	entityDraw(entity);

	// Draw bullet.
	AntifaShip * data = (AntifaShip*)entity->data;

	// Auto target line.
	if (data->doAutoTarget) {
		Entity * targetedEntity = getEntityFromWorld(game->world, data->targetedEntityId);

		if (targetedEntity != NULL)
			DrawLine3D(
				entity->position,
				targetedEntity->position,
				data->isOnTarget ? RED : BLUE
			);
	}

	// Draw bullet being shot.
	if (GetTime() - data->timeSinceLastBullet <= ANTIFA_DRAW_BULLET_FOR)
		DrawRay(data->lastBulletShot.ray, RED);
}