summaryrefslogtreecommitdiff
path: root/src/world.c
blob: 65a209594c96dbc0d2b57ff4c99e94b69268c31e (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
#include <raylib.h>
#include <stdio.h>
#include <math.h>

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

Color colors[7] = {
	GREEN,
	BLUE,
	RED,
	YELLOW,
	ORANGE,
	PURPLE,
	WHITE
};

void drawWorld(World *world)
{
	int cubeMaterial;
	Color cubeColor;
	for (int x=0;x<WORLD_WIDTH;  x++)
	for (int y=0;y<WORLD_HEIGHT; y++)
	for (int z=0;z<WORLD_WIDTH;  z++) {
		cubeMaterial = world->map[y][x][z];
		switch (cubeMaterial) {
			case 1 ... 7:
			cubeColor=colors[cubeMaterial-1];
			draw:
				DrawCube(
					(Vector3){
						(float)x,
						(float)y,
						(float)z
					},
					1.0f, 1.0f, 1.0f,
					cubeColor
				);
				DrawCubeWires(
					(Vector3){
						(float)x,
						(float)y,
						(float)z
					},
					1.0f, 1.0f, 1.0f,
					BLACK
				);
				break;
		}
	}
}

int intersects(Vector3 h1, Vector3 h2, Vector3 pos1, Vector3 pos2)
{
	return (
			pos1.x <= h2.x+pos2.x &&
			h1.x+pos1.x >= h2.x &&
			pos1.y <= h2.y+pos2.y &&
			h1.y+pos1.y >= h2.y &&
			pos1.z <= h2.z+pos2.z &&
			h1.z+pos1.z >= h2.z
		   );
}

void handleCollision(World *world, Player *player)
{
	int posX, posY, posZ;
	posX = (int)player->pos.x;
	posY = (int)player->pos.y;
	posZ = (int)player->pos.z;
	player->is_on_floor=0;
	for(int x=-1;x<2;x++) 
	for(int y=-1;y<2;y++)
	for(int z=-1;z<2;z++) {
		int cPosX, cPosY, cPosZ;
		cPosX = posX+x;
		cPosY = posY+y;
		cPosZ = posZ+z;
		if (world->map[cPosY][cPosX][cPosZ]!=0) {
			int inter = intersects(
						player->hitbox,
						(Vector3){1.0f, 1.0f, 1.0f},
						player->pos,
						(Vector3){
							(float)cPosX,
							(float)cPosY-1,
							(float)cPosZ,
						}
						);
			if (inter) {
				player->velocity.y=0.0f;
				player->pos.y=cPosY+1.0f;
				player->is_on_floor=1;
			}
		}
	}
}

void handleGravity(World *world, Player *player)
{
	//if (player->velocity.y > 0.0f) {
	//	player->velocity.y *= 0.9f;
	//}
	int posX, posY, posZ;
	posX = (int)player->pos.x;
	posY = (int)player->pos.y;
	posZ = (int)player->pos.z;
	player->velocity.y -= 0.01f;
	printf(
			"Player.pos.y:\t%d\n"
			"Player.pos.x:\t%d\n"
			"Player.pos.z:\t%d\n",
			posY,
			posX,
			posZ
			);
	printf("%d\n", (int)world->map[posY][posX][posZ]);
	//if (player->velocity.y <= 0.0f
	//&& world->map[posY][posX][posZ]==0) {
	//	player->velocity.y -= 0.01f;
	//}
}