aboutsummaryrefslogtreecommitdiff
path: root/src/entities/antifaShip.c
blob: fb307fb7c9392e5596257e64d2e36b503efb17f8 (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
#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;
}

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

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

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)) {
		Bullet bullet = createBulletFromEntity(*entity, 1.0);
		BulletHitInfo info = shootBullet(&game->world, bullet);

		if (info.hit) {
			Entity * hitEntity = getEntityFromWorld(game->world, info.hitId);
			printVector3(hitEntity->position);
		} else {
			puts("no stink");
		}
	}

	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;

	// 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);
	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);

	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);
}