summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 3934c2af34812f06b509e0f6686707842f39cf08 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <raylib.h>
#include <rcamera.h>

#include "world.h"
#include "player.h"

void handleInput(Player *player)
{
	player->velocity.x = (float)(IsKeyDown(KEY_W)-IsKeyDown(KEY_S))*1.0f;
	player->velocity.z = (float)(IsKeyDown(KEY_D)-IsKeyDown(KEY_A))*1.0f;
	printf(
			"Player.velocity.x: %f\n"
			"Player.velocity.y: %f\n"
			"Player.velocity.z: %f\n"
			"\n\n",
			player->velocity.x, player->velocity.y, player->velocity.z
		  );
	if (IsKeyPressed(KEY_E)) {
		printf("Key E pressed\n");
	}
	if (IsKeyPressed(KEY_SPACE) && player->is_on_floor) {
		printf("Key Space pressed\n");
		player->velocity.y=1.0f;
	}
	if (IsKeyDown(KEY_LEFT_CONTROL)) {
		player->speed = 0.06f;
	} else {
		player->speed = 0.04f;
	}
}

int main(void)
{
	InitWindow(1200, 800, "My game");
	SetTargetFPS(165);
	
	World world = {
		.map = {
			{
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1},
				{1,1,1,1,1,1,1,1,1,1}
			},
			{
				{1,1,1,1,1,1,1,1,1,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,1,1,1,1,1,1,1,1,1}
			},
		}
	};

	Player player = {
		.pos		= (Vector3){5.0f, 10.0f, 5.0f},
		.velocity	= (Vector3){0.0f, 0.0f, 0.0f},
		.speed		= 0.04f,
		.camera		= (Camera3D){
			.target = (Vector3){0.0f, 0.0f, 0.0f},
			.up = (Vector3){0.0f, 1.0f, 0.0f},
			.fovy = 60.0f,
			.projection = CAMERA_PERSPECTIVE,
		},
		.hitbox 	= createHitbox((Vector3[]){
				{ 1.0f, 0.0f,  1.0f},
				{ 1.0f, 0.0f, -1.0f},
				{-1.0f, 0.0f,  1.0f},
				{ 1.0f, 0.0f, -1.0f},

				{ 1.0f, 2.0f,  1.0f},
				{ 1.0f, 2.0f, -1.0f},
				{-1.0f, 2.0f,  1.0f},
				{ 1.0f, 2.0f, -1.0f},
		}),
	};

	player.camera.position = (Vector3){5.0f,10.0f,5.0f};

	int cameraMode = CAMERA_FIRST_PERSON;

	DisableCursor();

	char out[100];

	while(!WindowShouldClose()) {
		printf("Camera position: (%f,%f,%f)\n",
				player.camera.position.x,
				player.camera.position.y,
				player.camera.position.z
				);
		//UpdateCamera(&player.camera, cameraMode);
		handleInput(&player);
		handleCollision(&world, &player);
		handleGravity(&world, &player);
		handleMovement(&player);
		BeginDrawing();
			BeginMode3D(player.camera);

				ClearBackground(BLACK);
				drawWorld(&world);

			EndMode3D();
			snprintf(
					out, 100,
					"Advanced info:\n"
					"pos.x: %.2f\n"
					"pos.y: %.2f\n"
					"pos.z: %.2f\n", 
					player.pos.x,
					player.pos.y,
					player.pos.z
			);
			DrawText(out, 10, 40, 20, DARKGRAY);
			DrawFPS(10,10);
		EndDrawing();
	}
	CloseWindow();
	return 0;
}