aboutsummaryrefslogtreecommitdiffstats
path: root/src/entity.c
blob: 90ee6416e8a066d709111463b4ce86dba4db6408 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "entity.h"
#include "game.h"
#include "entitiesInclude.h"

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

  if (entity->id == ENTITY_NONE)
  {
    return;
  }

  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)
{
  if (id == ENTITY_NONE)
  {
    return false;
  }
  
  return entityEntries[id].isPlace;
}

bool entityCanBeSelected(EntityId id)
{
  if (id == ENTITY_NONE)
  {
    return false;
  }
  
  return entityEntries[id].canBeSelected;
}

float getEntityDistance(Entity entity, Vector3 position)
{
  return Vector3Distance(entity.position, position)
    - (Vector3Distance(entity.box.min, entity.box.max) / 2.0);
}

InteractionCommand interactWithEntity(Entity* entity, Game* game,
                                      Selection selection)
{
  if (entity->id == ENTITY_NONE)
  {
    return INTERACTION_END;
  }
  
  InteractionCallback callback = entityEntries[entity->id].interactionCallback;

  if (callback != NULL)
  {
    return callback(entity, game, selection);
  }
  
  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}
  };
}