A RPG I'm messing with made in Raylib.
at main 2.8 kB view raw
1#include "../include/player.h" 2#include "../include/animated_texture.h" 3#include "../include/config.h" 4#include "../include/player_state/default.h" 5#include "../include/raylib.h" 6#include "../include/raymath.h" 7#include <stdio.h> 8#include <stdlib.h> 9 10#define PLAYER_FRICTION 0.69f 11 12Player *player_create(float x, float y) { 13 Player *player = (Player *)malloc(sizeof(Player)); 14 15 player->textures[PLAYER_TEXTURE_FALLING] = animated_texture_create("assets/textures/player/falling.png", 4, 0.3f); 16 17 player->textures[PLAYER_TEXTURE_FORWARD] = animated_texture_create( 18 "assets/textures/player/stand-forward.png", 1, 0.0f 19 ); 20 21 player->textures[PLAYER_TEXTURE_WALK_FORWARD] = animated_texture_create( 22 "assets/textures/player/walk-forward.png", 2, 0.3f 23 ); 24 25 player->textures[PLAYER_TEXTURE_WALK_BACKWARD] = animated_texture_create( 26 "assets/textures/player/walk-backward.png", 2, 0.3f 27 ); 28 29 player->textures[PLAYER_TEXTURE_WALK_LEFT] = animated_texture_create("assets/textures/player/walk-left.png", 2, 0.3f); 30 31 player->textures[PLAYER_TEXTURE_WALK_RIGHT] = animated_texture_create("assets/textures/player/walk-right.png", 2, 0.3f); 32 33 for (int i = 0; i < _PLAYER_TEXTURE_LENGTH; i++) { 34 if (player->textures[i].texture.id == 0) { 35 perror("Failed to load player sprites!\n"); 36 free(player); 37 CloseWindow(); 38 39 return NULL; 40 } 41 } 42 43 player->camera = (Camera2D){ 0 }; 44 player->camera.zoom = 4.0f; 45 player->camera.offset = (Vector2){ WINDOW_WIDTH / 2.0f, WINDOW_HEIGHT / 2.0f }; 46 47 player_state_transition_to_default(&player->state, player); 48 49 player->position.x = x; 50 player->position.y = y; 51 52 player->velocity = Vector2Zero(); 53 player->control = Vector2Zero(); 54 player->speed = 12.0f; 55 player->friction = 0.69f; 56 57 return player; 58} 59 60int player_destroy(Player *player) { 61 for (int i = 0; i < _PLAYER_TEXTURE_LENGTH; i++) { 62 animated_texture_delete(player->textures[i]); 63 } 64 65 free(player); 66 67 return EXIT_SUCCESS; 68} 69 70Vector2 player_center(Player *player) { 71 return (Vector2){ player->position.x + 8, player->position.y + 8 }; 72} 73 74void player_draw(Player *player) { 75 DrawRectangleV(player->position, (Vector2){ 16, 16 }, player->color); 76 77 DrawText(player->state.name, 0, 0, 20, BLACK); 78 79 animated_texture_draw(&player->texture, player->position, WHITE); 80 81 Vector2 center = player_center(player); 82 Vector2 velocity_from_position = { 83 center.x + player->velocity.x, center.y + player->velocity.y 84 }; 85 DrawLineEx(center, velocity_from_position, 1.0f, GREEN); 86} 87 88void player_update(Player *player) { 89 if (player->state.update) { 90 player->state.update(&player->state, player); 91 } 92 93 player->camera.target = player_center(player); 94}