aboutsummaryrefslogtreecommitdiffstats
path: root/src/entity.c
blob: aff02958b6ac1cfe96c6f41ee32019823ab11292 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "entity.h"
#include "game.h"
#include "entitiesInclude.h"

const EntityEntry entityEntries[ENTITY_COUNT] = {
  (EntityEntry){initOldMint, updateOldMint, NULL, false, true},
  (EntityEntry){initStickyNickel, updateStickyNickel, NULL, false, true},
  (EntityEntry){initTree, updateTree, NULL, false, true},
  (EntityEntry){initBush, updateBush, NULL, false, true},
  (EntityEntry){initFlower, updateFlower, NULL, false, true},
  (EntityEntry){initPond, updatePond, NULL, true, true},
  (EntityEntry){initUtilityPole, NULL, NULL, false, false},
  (EntityEntry){initSamantha, updateSamantha, NULL, false, true},
  (EntityEntry){initSamanthasSpot, updateSamanthasSpot, NULL, true, false},
  (EntityEntry){initTrashcan, updateTrashcan, NULL, false, true},
  (EntityEntry){initTrash, updateTrash, NULL, false, true},
  (EntityEntry){initMedicalTrash, updateMedicalTrash, NULL, false, true},
  (EntityEntry){initJohn, updateJohn, NULL, false, true},
  (EntityEntry){initRon, updateRon, NULL, false, true}
};

Entity createEntity(EntityId id, Vector3 position)
{
  Entity entity;
  entity.id = id;
  entity.state = ENTITY_DEFAULT_STATE;
  entity.data = NULL;

  // Run init callback.
  InitEntityCallback initCallback = entityEntries[id].initCallback;

  if (initCallback != NULL)
  {
    initCallback(&entity);
  }

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

void updateEntity(Entity* entity, Game* game)
{
  //DrawBoundingBox(entity->box, BLUE);

  UpdateEntityCallback updateCallback =
    entityEntries[entity->id].updateCallback;

  if (updateCallback != NULL)
  {
    updateCallback(entity, game);
  }
}

void closeEntity(Entity* entity)
{
  if (entity->id == ENTITY_NONE)
  {
    return;
  }
  
  CloseEntityCallback closeCallback = entityEntries[entity->id].closeCallback;

  if (closeCallback != NULL)
  {
    closeCallback(entity);
  }

  entity->id = ENTITY_NONE;
}

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);
}

bool entityIsPlace(EntityId id)
{
  return entityEntries[id].isPlace;
}

bool entityCanBeSelected(EntityId id)
{
  return entityEntries[id].canBeSelected;
}

InteractionCommand interactWithOldMint(Entity* entity, Game* game,
                                       Selection selection)
{
  if (selection == SELECTION_INTERACT)
  {
    // Run display message code.
    return INTERACTION_TALK;
  }
  else
  {
    return INTERACTION_END;
  }
}

InteractionCommand interactWithEntity(Entity* entity, Game* game,
                                      Selection selection)
{
  switch (entity->id)
  {
  case OLD_MINT: // For testing.
    return interactWithOldMint(entity, game, selection);
  default:
    break;
  }
  
  return INTERACTION_END;
}

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}
  };
}