SG пре 1 година
родитељ
комит
3ced9d47ba
2 измењених фајлова са 39 додато и 1 уклоњено
  1. 28 1
      level.c
  2. 11 0
      level.h

+ 28 - 1
level.c

@@ -62,7 +62,7 @@ static void level_process_remove(Level* level) {
     EntityList_clear(level->to_remove);
 }
 
-static bool level_entity_in_list_p(EntityList_t list, Entity* entity) {
+static bool level_entity_in_list_p(const EntityList_t list, Entity* entity) {
     FOREACH(item, list) {
         if(*item == entity) {
             return true;
@@ -244,6 +244,33 @@ size_t level_entity_count(const Level* level, const EntityDescription* descripti
     return count;
 }
 
+Entity* level_entity_get(const Level* level, const EntityDescription* description, size_t index) {
+    size_t count = 0;
+    FOREACH(item, level->entities) {
+        if(description == NULL || description == entity_description_get(*item)) {
+            if(!level_entity_in_list_p(level->to_remove, *item)) {
+                if(count == index) {
+                    return *item;
+                }
+                count++;
+            }
+        }
+    }
+
+    FOREACH(item, level->to_add) {
+        if(description == NULL || description == entity_description_get(*item)) {
+            if(!level_entity_in_list_p(level->to_remove, *item)) {
+                if(count == index) {
+                    return *item;
+                }
+                count++;
+            }
+        }
+    }
+
+    return NULL;
+}
+
 void* level_context_get(Level* level) {
     return level->context;
 }

+ 11 - 0
level.h

@@ -65,6 +65,17 @@ void level_send_event(
  */
 size_t level_entity_count(const Level* level, const EntityDescription* description);
 
+/**
+ * @brief Get an entity of a certain type in the level
+ * Returns NULL if the index is out of bounds
+ * 
+ * @param level level instance
+ * @param description entity description, NULL for all entities
+ * @param index entity index
+ * @return Entity* entity
+ */
+Entity* level_entity_get(const Level* level, const EntityDescription* description, size_t index);
+
 /**
  * @brief Get the context of the level
  *