aboutsummaryrefslogtreecommitdiff
path: root/src/player.c
blob: f25c1fcde2839542f62d6492d1cf4ae725a08b94 (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
#include "player.h"
#include "raymath.h"
#include "globals.h"

void UpdatePlayer(Player *p, MapManager *mapMgr) {
  Vector2 move = {0};
  if (IsKeyDown(KEY_W)) move.y -= 1;
  if (IsKeyDown(KEY_S)) move.y += 1;
  if (IsKeyDown(KEY_A)) move.x -= 1;
  if (IsKeyDown(KEY_D)) move.x += 1;

  int newState = (Vector2Length(move) > 0) ? 1 : 0;
  bool newFacingRight = p->facingRight;
  if (move.x > 0) newFacingRight = true;
  else if (move.x < 0) newFacingRight = false;

  if (newState != p->state || newFacingRight != p->facingRight) {
    p->state = newState;
    p->facingRight = newFacingRight;
    p->currentFrame = 0;
    p->frameTime = 0;
  }

  // Update bounds based on current position (Feet hitbox: 8x6 centered horizontally, offset bottom)
  p->bounds = (Rectangle){ p->position.x - 4, p->position.y + 2, 8, 6 };

  if (Vector2Length(move) > 0) {
    move = Vector2Scale(Vector2Normalize(move), p->speed * GetFrameTime());

    // Try moving X
    Rectangle nextX = p->bounds;
    nextX.x += move.x;
    if (!IsWallCollision(mapMgr, nextX)) {
      p->position.x += move.x;
      p->bounds.x += move.x;
    }

    // Try moving Y
    Rectangle nextY = p->bounds;
    nextY.y += move.y;
    if (!IsWallCollision(mapMgr, nextY)) {
      p->position.y += move.y;
      p->bounds.y += move.y;
    }
  }

  // Determine base tile ID from tileset for current animation
  int baseTileId = 0;
  if (p->state == 0) baseTileId = p->facingRight ? 0 : 7;
  else if (p->state == 1) baseTileId = p->facingRight ? 14 : 21;

  // Animation Update using Tiled metadata
  TmxTilesetTile *tileData = NULL;
  for (uint32_t i = 0; i < p->tileset.tilesLength; i++) {
    if (p->tileset.tiles[i].id == (uint32_t)baseTileId) {
      tileData = &p->tileset.tiles[i];
      break;
    }
  }

  if (tileData && tileData->hasAnimation) {
    p->frameTime += GetFrameTime(); // raytmx stores durations in seconds
    float duration = tileData->animation.frames[p->currentFrame].duration;
    if (p->frameTime >= duration) {
      p->frameTime -= duration;
      p->currentFrame = (p->currentFrame + 1) % tileData->animation.framesLength;
    }
  }
}

void DrawPlayer(Player *p) {
  int baseTileId = 0;
  if (p->state == 0) baseTileId = p->facingRight ? 0 : 7;
  else if (p->state == 1) baseTileId = p->facingRight ? 14 : 21;

  TmxTilesetTile *tileData = NULL;
  for (uint32_t i = 0; i < p->tileset.tilesLength; i++) {
    if (p->tileset.tiles[i].id == (uint32_t)baseTileId) {
      tileData = &p->tileset.tiles[i];
      break;
    }
  }

  int displayTileId = baseTileId;
  if (tileData && tileData->hasAnimation) {
    displayTileId = tileData->animation.frames[p->currentFrame].gid;
  }

  // Calculate source rectangle from tile ID
  int col = displayTileId % p->tileset.columns;
  int row = displayTileId / p->tileset.columns;

  Rectangle source = {
    (float)(col * TILE_SIZE),
    (float)(row * TILE_SIZE),
    (float)TILE_SIZE,
    (float)TILE_SIZE
  };
  
  Vector2 pos = { p->position.x - (float)TILE_SIZE / 2.0f, p->position.y - (float)TILE_SIZE / 2.0f };
  DrawTextureRec(p->tileset.image.texture, source, pos, WHITE);

  if (debugMode) {
    DrawRectangleLinesEx(p->bounds, 1, GREEN);
  }
}