#include #include #include "constants.h" #include "game.h" #include "player.h" #include "physics.h" #include "assets.h" #include "utils.h" extern Game game; #define PLAYER_SPEED 300.0f const Rectangle shadowDestRect = (Rectangle) { .x = 0, .y = 100, .width = 100, .height = 40 }; const Rectangle physicsCollider = (Rectangle) { .x = 0, .y = 0, .width = 100, .height = 100 }; Vector2 GetMovementDirection() { Vector2 movementDirection = {0.0f, 0.0f}; if (IsKeyDown(KEY_W)) { movementDirection.y = -1.0f; } else if (IsKeyDown(KEY_S)) { movementDirection.y = 1.0f; } if (IsKeyDown(KEY_A)) { movementDirection.x = -1.0f; } else if (IsKeyDown(KEY_D)) { movementDirection.x = 1.0f; } if (Vector2Length(movementDirection) > 0.0f) return Vector2Normalize(movementDirection); return movementDirection; } void CameraFollow(const Entity* player) { game.camera.target = (Vector2) {fmaxf(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; } void UpdatePlayer(Entity* player, float deltaTime) { player->velocity = Vector2Scale(GetMovementDirection(), PLAYER_SPEED); MoveAndSlide(player, deltaTime); CameraFollow(player); } void AddPlayer(float xpos, float ypos) { Entity player = {0}; player.type = Player_Entity; player.position = (Vector2) {xpos, ypos}; player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE); player.physicsCollider = physicsCollider; #ifdef BEATEMUP_DEBUG player.physicsColliderColor = RED; #endif Asset* shadowTexture = GetMatchingAssetWithType("human-shadow", Texture_Asset); if (shadowTexture == NULL) { TraceLog(LOG_ERROR, "Failed to find texture asset human-shadow, exitting!"); exit(EXIT_FAILURE); } AddSpriteToEntity(&player, (Sprite){ .texture = shadowTexture->texture, .layer = Foreground_Layer, .destRect = shadowDestRect }); AddEntity(&player); }