summaryrefslogtreecommitdiffstats
path: root/Room.h
blob: fb67ac5794012cb64a03f00e9765776d9a7a2b1a (plain)
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
152
153
154
155
156
157
158
159
#ifndef ROOM_H_
#define ROOM_H_

#include "Polygon.h"
#include "SharedPtr.h"
#include "LevelObject.h"
#include "VertexProvider.h"
#include "EdgeProvider.h"
#include <string>


class Room : public LevelObject, private VertexProvider, private EdgeProvider {
  private:
    class RoomVertex {
      public:
        virtual const Vertex& operator*() const = 0;
        const Vertex* operator->() {return &**this;}
                
        virtual ~RoomVertex() {}
        
        virtual const Vertex& operator+=(const Vertex &v) {return **this;}
        virtual const Vertex& operator-=(const Vertex &v) {return **this;}
        
        virtual void setLocation(float x, float y) {}
    };
    
    class RoomVertexDirect : public RoomVertex {
      private:
        Vertex vertex;
        
      public:
        RoomVertexDirect(const Vertex &v) : vertex(v) {}
        
        virtual const Vertex& operator*() const {
          return vertex;
        }
        
        virtual const Vertex& operator+=(const Vertex &v) {
          vertex += v;
          return vertex;
        }
        virtual const Vertex& operator-=(const Vertex &v) {
          vertex -= v;
          return vertex;
        }
        
        virtual void setLocation(float x, float y) {
          vertex.setLocation(x, y);
        }
    };
    
    class RoomVertexIndirect : public RoomVertex {
      private:
        LevelVertex vertex;
        
      public:
        RoomVertexIndirect(const LevelVertex &v) : vertex(v) {}
        
        virtual const Vertex& operator*() const {
          return *vertex;
        }
    };
    
    
    std::string name;
    float height;
    
    std::vector<SharedPtr<RoomVertex> > vertices;
    std::vector<Edge> edges;
    
  public:
    Room() {height = 10;}
    Room(std::string name) {this->name = name; height = 10;}
    
    Room(const Room &room) : name(room.name), height(room.height), vertices(room.vertices) {
      for(std::vector<Edge>::const_iterator edge = room.edges.begin(); edge != room.edges.end(); edge++) {
        LevelVertex v1(this, edge->getVertex1().getId(), this);
        LevelVertex v2(this, edge->getVertex2().getId(), this);
        edges.push_back(Edge(v1, v2));
      }
    }
    
    const Room& operator=(const Room &room);
    
    std::string &getName() {return name;}
    const std::string &getName() const {return name;}
    void setName(const std::string &name) {this->name = name;}
    
    float getHeight() const {return height;}
    void setHeight(float height) {this->height = height;}
    
    void addVertex(Vertex v);
    
    Polygon getPolygon() const {
      Polygon polygon;
      
      for(std::vector<SharedPtr<RoomVertex> >::const_iterator v = vertices.begin(); v != vertices.end(); v++)
        polygon.push_back(***v);
      
      return polygon;
    }
    
    virtual bool hit(const Vertex &v) const {return getPolygon().contains(v);}
    virtual int getPriority() const {return 0;}
    
    virtual std::vector<SharedPtr<LevelObject> > getChildren();
    
    virtual const char* getType() const {
      return "Room";
    }
    
    virtual bool canMove() const {return true;}
    
    virtual void move(float x, float y) {
      Vertex m(x, y);
      
      for(std::vector<SharedPtr<RoomVertex> >::iterator v = vertices.begin(); v != vertices.end(); v++)
        **v += m;
    }
    
    virtual bool canRotate() const {return true;}
    
    virtual void rotate(Vertex m, float a);
    
    virtual const Vertex* getVertex(size_t id) const {
      return &**vertices[id];
    }
    
    virtual size_t getVertexCount() const {
      return vertices.size();
    }
    
    virtual void moveVertex(size_t id, float x, float y) {
      *vertices[id] += Vertex(x, y);
    }
    
    virtual void rotateVertex(size_t id, Vertex m, float a) {
      rotate(m, a);
    }
    
    virtual const Edge* getEdge(size_t id) const {
      return &edges[id];
    }
    
    virtual size_t getEdgeCount() const {
      return edges.size();
    }
    
    virtual void moveEdge(size_t id, float x, float y) {
      moveVertex(id, x, y);
      moveVertex((id+1)%vertices.size(), x, y);
    }
    
    virtual void rotateEdge(size_t id, Vertex m, float a) {
      rotate(m, a);
    }
};

#endif /*ROOM_H_*/