]> _ Git - aichan-platformer.git/commitdiff
a LOT of stuff. also apparently raylib has built in collisionsgit add main.cgit add...
authorparking_turkeys <roya1060wien@gmail.com>
Wed, 8 Jul 2026 22:54:07 +0000 (18:54 -0400)
committerparking_turkeys <roya1060wien@gmail.com>
Wed, 8 Jul 2026 22:54:07 +0000 (18:54 -0400)
main.c

diff --git a/main.c b/main.c
index 65606a0..a9a40cf 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,23 +1,43 @@
 #include "raylib.h"
+#include <math.h>
+#include <stdio.h>
 
 //constants
 #define SCREENWIDTH     640
 #define SCREENHEIGHT    480
 #define WINDOWTITLE     "platformer"
 #define FPS             30
+#define MAXSPEED        300
+#define GRAVITY         50
 
 //structures
 struct character {
        Vector2 position; //top left corner iirc
        Vector2 size; //x is width, y is height
+       Vector2 velocity; //in pixels per second
+};
+
+struct platform {
+       Vector2 position;
+       Vector2 size;
 };
 
 //variables
 struct character player = { {310,220}, {20,40} };
+struct platform ground[] = {
+       {{0,320}, {640, 160}}
+};
+int ground_length = 1;
 
 //functions
-int handle_inputs(struct character player);
-int draw_character(struct character character);
+Vector2 handle_inputs(struct character *player);
+int draw_character(struct character *character);
+int draw_platform(struct platform platform);
+int move_x(struct character *character, int direction);
+int move_y(struct character *character, int direction);
+int onfloor(struct character *chara, struct platform *ground, int ground_length);
+int gravity(struct character *character, struct platform *ground, int ground_length);
+int move_character(struct character *character, struct platform *ground, int ground_length, float delta);
 
 int main(void)
 {
@@ -26,14 +46,22 @@ int main(void)
        SetTargetFPS(FPS);
 
        while (!WindowShouldClose()) {
-               handle_inputs(player);
+               Vector2 direction = handle_inputs(&player);
+               // set velocity
+               move_x(&player, direction.x);
+               move_y(&player, direction.y);
+               gravity(&player, ground, ground_length);
+
+               //set position
+               move_character(&player, ground, ground_length, GetFrameTime());
+               //player.position.y += 1;
 
                BeginDrawing();
 
                        ClearBackground(RAYWHITE);
                        
-                       draw_character(player);
-
+                       draw_character(&player);
+                       draw_platform(ground[0]);
                EndDrawing();
        }
 
@@ -42,30 +70,107 @@ int main(void)
        return 0;
 }
 
-int draw_character(struct character chara)
+int draw_character(struct character *chara)
+{
+       DrawRectangle(chara->position.x,chara->position.y,chara->size.x,chara->size.y, MAROON);
+       return 1;
+}
+
+int draw_platform(struct platform platform)
 {
-       DrawRectangle(chara.position.x,chara.position.y,chara.size.x,chara.size.y, MAROON);
+       DrawRectangle(platform.position.x,platform.position.y,platform.size.x,platform.size.y,GREEN);
        return 1;
 }
 
-char inputs[5] = "wsadh"; //up, down, left, right, jump
+int inputs[5] = {KEY_W, KEY_S, KEY_A, KEY_D, KEY_H}; //up, down, left, right, jump
 
-int handle_inputs(struct character player)
+Vector2 handle_inputs(struct character *player)
 {
        int input_key;
+       Vector2 direction = {0,0};
 
-       while(input_key = GetCharPressed() != 0) {
-               if (input_key == inputs[0]) {
+       
+               
+               if (IsKeyDown(inputs[0])) {
                        //up
-               } else if (input_key == inputs[1]) {
+               }; if (IsKeyDown(inputs[1])) {
                        //down
-               } else if (input_key == inputs[2]) {
-                       //left
-               } else if (input_key == inputs[3]) {
-                       //right
-               } else if (input_key == inputs[4]) {
-                       //jump
+               }; if (IsKeyDown(inputs[2])) {
+                       direction.x--;
+               }; if (IsKeyDown(inputs[3])) {
+                       direction.x++;
+               }; if (IsKeyDown(inputs[4])) {
+                       direction.y--;
                }
+       return direction;
+}
+
+int move_x(struct character *chara, int dir)
+{
+       if (dir == 0) { chara->velocity.x = 0; return 1; };
+       if ((dir > 0 && chara->velocity.x > 0) || (dir < 0 && chara->velocity.x < 0)){
+               chara->velocity.x += dir*10;
+       } else {
+               chara->velocity.x = 0 + dir*10;
+       }
+       if (chara->velocity.x > MAXSPEED) {
+               chara->velocity.x = MAXSPEED;
+       }
+       if (chara->velocity.x < (-1*MAXSPEED)) {
+               chara->velocity.x = -1*MAXSPEED;
        }
        return 1;
 }
+
+int move_y(struct character *chara, int dir)
+{
+       if (dir >= 0) { return 1; };
+       if (dir < 0) {
+               if (onfloor(chara, ground, ground_length)==1) {
+                       chara->velocity.y = 0 - MAXSPEED;
+               }
+       }
+       if (chara->velocity.y > MAXSPEED) {
+               chara->velocity.y = MAXSPEED;
+       }
+       return 1;
+}
+
+int onfloor(struct character *chara, struct platform *ground, int length)
+{
+       for (int i = 0; i < length; i++) {
+               if ((((ground + i)->position.y - 1) == (chara->position.y + chara->size.y)) && (((ground + i)->position.x < chara->position.x < ((ground + i)->position.x + (ground + i)->size.x)) || ((ground + i)->position.x < (chara->position.x + chara->size.x) < ((ground + i)->position.x + (ground + i)->size.x)))) {
+                                               return 1;
+                                               } else {
+                                               return 0;
+                                               }
+       }
+}
+
+int gravity(struct character *chara, struct platform *ground, int length)
+{
+       chara->velocity.y += GRAVITY;
+       return 1;
+}
+
+struct Vector2 infloor(struct character *chara, struct platform *ground, int length) {
+       struct Vector2 depth = {0,0};
+       struct Rectangle charrect = {chara->position.x, chara->position.y, chara->size.x, chara->size.y};
+       struct Rectangle groundrect, collrect;
+       
+       
+       for (int i = 0; i < length; i++) {
+               if (CheckCollisionRecs(charrect, ground[i])) {
+                       collrect = GetCollisionRect(charrect, ground[i]);
+                       if (ground[i].position.x > charrect.x) { depth.x = (int)(collrect.width) * -1; } else { depth.x = (int)(collrect.width) };
+                       if (ground[i].position.y > charrect.y) { depth.y = (int)(collrect.height) * -1; } else { depth.y = (int)(collrect.height) };
+       }
+       return depth;
+}       
+
+int move_character(struct character *chara, struct platform *ground, int length, float delta)
+{
+       chara->position.x += ceil(chara->velocity.x * delta);
+       chara->position.y += ceil(chara->velocity.y * delta);
+       return 1;
+}