summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDrNuget <drnuget@outlook.com>2025-03-03 18:38:18 +0200
committerDrNuget <drnuget@outlook.com>2025-03-03 18:38:18 +0200
commit23dac85d2f600f0073e77bf83f4a544f3a087416 (patch)
tree76fbaa200140036473c3b9bacace389c14bdcd25 /src
let the madness begin
Diffstat (limited to 'src')
-rw-r--r--src/entity.c9
-rw-r--r--src/entity.h12
-rw-r--r--src/globals.h6
-rw-r--r--src/main.c136
-rw-r--r--src/main.h0
-rw-r--r--src/player.c36
-rw-r--r--src/player.h18
-rw-r--r--src/world.c101
-rw-r--r--src/world.h18
9 files changed, 336 insertions, 0 deletions
diff --git a/src/entity.c b/src/entity.c
new file mode 100644
index 0000000..f2c4190
--- /dev/null
+++ b/src/entity.c
@@ -0,0 +1,9 @@
+#include "entity.h"
+
+Hitbox createHitbox(Vector3* vertices)
+{
+ return (Hitbox){
+ .vertices = vertices,
+ .num_vertices = sizeof(vertices)/sizeof(Vector3)
+ };
+}
diff --git a/src/entity.h b/src/entity.h
new file mode 100644
index 0000000..01705b1
--- /dev/null
+++ b/src/entity.h
@@ -0,0 +1,12 @@
+#ifndef ENTTIY_H
+#define ENTITY_H
+#include <raylib.h>
+
+typedef struct hitbox {
+ Vector3* vertices;
+ int num_vertices;
+} Hitbox;
+
+Hitbox createHitbox(Vector3* vertices);
+
+#endif
diff --git a/src/globals.h b/src/globals.h
new file mode 100644
index 0000000..6fe6348
--- /dev/null
+++ b/src/globals.h
@@ -0,0 +1,6 @@
+#ifndef GLOBALS_H
+#define GLOBALS_H
+
+#define GRAVITY 0.1f
+
+#endif
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..3934c2a
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,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;
+}
diff --git a/src/main.h b/src/main.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/main.h
diff --git a/src/player.c b/src/player.c
new file mode 100644
index 0000000..20afbe2
--- /dev/null
+++ b/src/player.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <raylib.h>
+#include <rcamera.h>
+#include <math.h>
+#include <stdlib.h>
+
+#include "globals.h"
+#include "player.h"
+
+void handleMovement(Player *player)
+{
+ Vector2 mdelta = GetMouseDelta();
+
+ UpdateCameraPro(&player->camera,
+ (Vector3){
+ player->velocity.x * (float)player->speed,
+ player->velocity.z * (float)player->speed,
+ player->velocity.y * (float)GRAVITY
+ },
+ (Vector3){
+ mdelta.x*0.08f,
+ mdelta.y*0.08f,
+ 0.0f
+ },
+ 0.0f
+ );
+ player->pos.x = player->camera.position.x;
+ player->pos.z = player->camera.position.z;
+ player->pos.y = roundf(player->camera.position.y) - 2.0f;
+ printf(
+ "cpos: %f\n"
+ "ppos: %f\n",
+ player->camera.position.y,
+ player->pos.y
+ );
+}
diff --git a/src/player.h b/src/player.h
new file mode 100644
index 0000000..346b800
--- /dev/null
+++ b/src/player.h
@@ -0,0 +1,18 @@
+#ifndef PLAYER_H
+#define PLAYER_H
+#include <raylib.h>
+#include "entity.h"
+
+typedef struct player {
+ Vector3 pos;
+ Vector3 velocity;
+ Camera3D camera;
+ Hitbox hitbox;
+
+ float speed;
+ short is_on_floor;
+} Player;
+
+void handleMovement(Player *player);
+
+#endif
diff --git a/src/world.c b/src/world.c
new file mode 100644
index 0000000..17e35b9
--- /dev/null
+++ b/src/world.c
@@ -0,0 +1,101 @@
+#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;
+ }
+ }
+}
+
+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;
+ //Floor check:
+ if (world->map[posY][posX][posZ]!=0) {
+ player->is_on_floor=1;
+ if (player->velocity.y < 0.0f) {
+ player->velocity.y = 0.0f;
+ }
+ } else {
+ player->is_on_floor=0;
+ }
+ if (world->map[posY][posX][posZ]!=0
+ && (float)posY!=player->pos.y) {
+ player->camera.position.y=(float)posY+2;
+ //player->velocity.y = 0.0f;
+ }
+}
+
+void handleGravity(World *world, Player *player)
+{
+ if (player->velocity.y > 0.0f) {
+ player->velocity.y *= 0.9f;
+ }
+ if (player->velocity.y <= 0.1f && player->velocity.y > 0.0f) {
+ player->velocity.y = 0.0f;
+ }
+ int posX, posY, posZ;
+ posX = (int)player->pos.x;
+ posY = (int)player->pos.y;
+ posZ = (int)player->pos.z;
+ 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.1f;
+ }
+}
diff --git a/src/world.h b/src/world.h
new file mode 100644
index 0000000..88bdfdb
--- /dev/null
+++ b/src/world.h
@@ -0,0 +1,18 @@
+#ifndef WORLD_H
+#define WORLD_H
+#include "player.h"
+
+#define WORLD_WIDTH 10
+#define WORLD_HEIGHT 10
+
+typedef struct world {
+ int map[WORLD_HEIGHT][WORLD_WIDTH][WORLD_WIDTH];
+} World;
+
+void drawWorld(World *world);
+
+void handleCollision(World *world, Player *player);
+
+void handleGravity(World *world, Player *player);
+
+#endif