aboutsummaryrefslogtreecommitdiffstats
path: root/src/entity.c
blob: de5f7f395b85b259b55ef5074e9ef3ceb676fecf (plain) (blame)
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
#include "entity.h"
#include "game.h"

BoundingBox entityBoxFromScale(float scale, float width, float height)
{
  Vector2 size = (Vector2){width / height * scale, scale};
  size = Vector2Scale(size, 0.5);

  return (BoundingBox){
    .min = (Vector3){-size.x, -size.y, -size.x},
    .max = (Vector3){size.x, size.y, size.x}
  };
}

// TODO: Entity creation system
Entity createEntity(EntityId id, Vector3 position)
{
  Entity entity;
  entity.id = id;

  switch (id)
  {
  case OLD_MINT:
  case STICKY_NICKEL:
    entity.box = entityBoxFromScale(1.0, 32.0, 32.0);
    break;
  case TREE:
    entity.box = entityBoxFromScale(TREE_SCALE, 225.0, 500.0);
    break;
  case BUSH:
    entity.box = entityBoxFromScale(BUSH_SCALE, 174.0, 124.0);
    break;
  default:
    break;
  }

  entity.position = Vector3Zero();
  setEntityPosition(&entity, position);
  return entity;
}

void updateEntity(Entity* entity, Game* game)
{
  switch (entity->id)
  {
  case OLD_MINT:
    DrawBillboard(game->player.camera, game->assets.textures[MINT_TEXTURE],
                  entity->position, 1.0, WHITE);
    break;
  case STICKY_NICKEL:
    DrawBillboard(game->player.camera, game->assets.textures[NICKEL_TEXTURE],
                  entity->position, 1.0, WHITE);
    break;
  case TREE:
    DrawBillboard(game->player.camera, game->assets.textures[TREE_TEXTURE],
                  entity->position, TREE_SCALE, WHITE);
    break;
  case BUSH:
    DrawBillboard(game->player.camera, game->assets.textures[BUSH_TEXTURE],
                  entity->position, BUSH_SCALE, WHITE);
    break;
  default:
    break;
  }
}

void setEntityPosition(Entity* entity, Vector3 position)
{
  Vector3 movedBy = Vector3Subtract(position, entity->position);
  entity->position = position;
  entity->box.min = Vector3Add(entity->box.min, movedBy);
  entity->box.max = Vector3Add(entity->box.max, movedBy);
}

void placeEntityOnGround(Entity* entity, const World* world)
{
  Vector3 position = entity->position;
  
  position.y = getWorldHeightAtLocation(
    world, entity->position.x, entity->position.z);
  position.y += (entity->box.max.y - entity->box.min.y) / 2.0;

  setEntityPosition(entity, position);
}