summary refs log tree commit diff stats
path: root/src/entityfactory.cpp
blob: bf137e2fdaf833c106cff77c598dba93030a242d (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
#include "entityfactory.h"
#include <libxml/parser.h>
#include "components.h"
#include "muxer.h"
#include <cstdio>
#include <map>

struct EntityData {
  char* sprite;
  char* action;
  bool hasPhysics;
  int width;
  int height;
};

static std::map<std::string, EntityData> factories;

std::shared_ptr<Entity> EntityFactory::createNamedEntity(const std::string name, const Map& map)
{
  auto it = factories.find(name);
  EntityData data = factories[name];
  if (it == factories.end())
  {
    xmlDocPtr doc = xmlParseFile(("../entities/" + name + ".xml").c_str());
    if (doc == nullptr)
    {
      fprintf(stderr, "Error reading entity %s\n", name.c_str());
      exit(-1);
    }
  
    xmlNodePtr top = xmlDocGetRootElement(doc);
    if (top == nullptr)
    {
      fprintf(stderr, "Empty entity %s\n", name.c_str());
      exit(-1);
    }
  
    if (xmlStrcmp(top->name, (const xmlChar*) "entity-def"))
    {
      fprintf(stderr, "Invalid entity definition %s\n", name.c_str());
      exit(-1);
    }
  
    for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
    {
      if (!xmlStrcmp(node->name, (const xmlChar*) "sprite"))
      {
        xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
        data.sprite = (char*) calloc(xmlStrlen(key)+1, sizeof(char));
        strcpy(data.sprite, (char*) key);
        xmlFree(key);
      } else if (!xmlStrcmp(node->name, (const xmlChar*) "action"))
      {
        xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
        data.action = (char*) calloc(xmlStrlen(key)+1, sizeof(char));
        strcpy(data.action, (char*) key);
        xmlFree(key);
      } else if (!xmlStrcmp(node->name, (const xmlChar*) "size"))
      {
        xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
        data.hasPhysics = true;
        sscanf((char*) key, "%d,%d", &data.width, &data.height);
        xmlFree(key);
      }
    }
  
    xmlFreeDoc(doc);
    
    factories[name] = data;
  }
  
  auto entity = std::make_shared<Entity>();
  
  if (data.sprite)
  {
    auto component = std::make_shared<StaticImageComponent>(data.sprite);
    entity->addComponent(component);
  }
  
  if (data.action)
  {
    if (!strcmp(data.action, "save"))
    {
      auto component = std::make_shared<SimpleColliderComponent>([&] (Game& game, Entity& collider) {
        playSound("../res/Pickup_Coin23.wav", 0.25);
  
        game.saveGame(map, collider.position);
      });
      entity->addComponent(component);
    }
  }
  
  if (data.hasPhysics)
  {
    auto component = std::make_shared<PhysicsBodyComponent>();
    entity->addComponent(component);
    
    entity->size = std::make_pair(data.width, data.height);
  }
  
  return entity;
}