This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
neofx-zoomedit/Room.h

211 lines
5.7 KiB
C
Raw Normal View History

#ifndef ROOM_H_
#define ROOM_H_
#include "Polygon.h"
2008-02-15 11:01:04 +00:00
#include "SharedPtr.h"
#include "LevelObject.h"
2008-02-13 21:06:01 +00:00
#include "VertexProvider.h"
2008-02-15 11:01:04 +00:00
#include "EdgeProvider.h"
#include <string>
2008-02-15 11:01:04 +00:00
class Room : public LevelObject, private VertexProvider, private EdgeProvider {
private:
2008-02-19 20:56:04 +00:00
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) {}
virtual bool isDirect() const = 0;
2008-02-19 20:56:04 +00:00
};
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;
}
2008-02-19 20:56:04 +00:00
virtual const Vertex& operator-=(const Vertex &v) {
vertex -= v;
return vertex;
}
virtual void setLocation(float x, float y) {
vertex.setLocation(x, y);
}
virtual bool isDirect() const {return true;}
2008-02-19 20:56:04 +00:00
};
class RoomVertexIndirect : public RoomVertex {
private:
LevelVertex vertex;
public:
RoomVertexIndirect(const LevelVertex &v) : vertex(v) {}
virtual const Vertex& operator*() const {
return *vertex;
}
virtual bool isDirect() const {return false;}
2008-02-19 20:56:04 +00:00
};
std::string name;
2007-10-30 20:07:00 +00:00
float height;
2008-02-19 20:56:04 +00:00
std::vector<SharedPtr<RoomVertex> > vertices;
2008-02-15 11:01:04 +00:00
std::vector<Edge> edges;
void addEdge() {
if(vertices.size() < 2)
return;
if(vertices.size() == 2) {
edges.push_back(Edge(LevelVertex(this, 0, this), LevelVertex(this, 1, this)));
return;
}
edges.push_back(Edge(LevelVertex(this, vertices.size()-2, this), LevelVertex(this, vertices.size()-1, this)));
}
public:
2007-10-30 20:07:00 +00:00
Room() {height = 10;}
2007-12-05 22:02:03 +00:00
Room(std::string name) {this->name = name; height = 10;}
2008-02-19 20:56:04 +00:00
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));
}
2008-02-15 11:01:04 +00:00
}
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;}
2007-10-30 20:07:00 +00:00
float getHeight() const {return height;}
void setHeight(float height) {this->height = height;}
void addVertex(Vertex v) {
vertices.push_back(SharedPtr<RoomVertex>(new RoomVertexDirect(v)));
addEdge();
}
void addVertex(LevelVertex &v) {
size_t s = v.connect();
if(s == v.getId()) {
vertices.push_back(SharedPtr<RoomVertex>(new RoomVertexIndirect(v)));
addEdge();
}
else {
if(vertices.empty()) {
LevelVertex v2(v.getProvider(), s, v.getParent());
vertices.push_back(SharedPtr<RoomVertex>(new RoomVertexIndirect(v2)));
vertices.push_back(SharedPtr<RoomVertex>(new RoomVertexIndirect(v)));
}
else {
LevelVertex v2(v.getProvider(), s, v.getParent());
vertices.push_back(SharedPtr<RoomVertex>(new RoomVertexIndirect(v)));
addEdge();
vertices.push_back(SharedPtr<RoomVertex>(new RoomVertexIndirect(v2)));
}
}
}
void close() {
if(vertices.size() >= 2)
edges.push_back(Edge(LevelVertex(this, vertices.size()-1, this), LevelVertex(this, 0, this)));
}
2008-02-15 11:01:04 +00:00
2008-02-19 20:56:04 +00:00
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;
}
2008-02-15 11:01:04 +00:00
2008-02-19 20:56:04 +00:00
virtual bool hit(const Vertex &v) const {return getPolygon().contains(v);}
virtual int getPriority() const {return 0;}
2008-02-15 11:01:04 +00:00
virtual std::vector<SharedPtr<LevelObject> > getChildren();
2008-02-13 21:06:01 +00:00
virtual const char* getType() const {
return "Room";
}
2008-01-13 16:41:01 +00:00
virtual bool canMove() const {return true;}
2008-01-13 16:41:01 +00:00
virtual void move(float x, float y) {
2008-02-09 12:21:00 +00:00
Vertex m(x, y);
2008-02-19 20:56:04 +00:00
for(std::vector<SharedPtr<RoomVertex> >::iterator v = vertices.begin(); v != vertices.end(); v++)
**v += m;
2008-01-13 16:41:01 +00:00
}
virtual bool canRotate() const {return true;}
virtual void rotate(Vertex m, float a);
2008-02-13 21:06:01 +00:00
virtual const Vertex* getVertex(size_t id) const {
2008-02-19 20:56:04 +00:00
return &**vertices[id];
2008-02-13 21:06:01 +00:00
}
virtual size_t getVertexCount() const {
2008-02-19 20:56:04 +00:00
return vertices.size();
2008-02-13 21:06:01 +00:00
}
virtual void moveVertex(size_t id, float x, float y) {
2008-02-19 20:56:04 +00:00
*vertices[id] += Vertex(x, y);
2008-02-13 21:06:01 +00:00
}
virtual void rotateVertex(size_t id, Vertex m, float a) {
rotate(m, a);
2008-02-13 21:06:01 +00:00
}
2008-02-15 11:01:04 +00:00
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);
2008-02-19 20:56:04 +00:00
moveVertex((id+1)%vertices.size(), x, y);
2008-02-15 11:01:04 +00:00
}
virtual void rotateEdge(size_t id, Vertex m, float a) {
rotate(m, a);
2008-02-15 11:01:04 +00:00
}
};
#endif /*ROOM_H_*/