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/Portal.h
2008-02-15 22:44:03 +00:00

121 lines
2.9 KiB
C++

#ifndef PORTAL_H_
#define PORTAL_H_
#include "LevelObject.h"
#include "Polygon.h"
#include "VertexProvider.h"
#include "LevelVertex.h"
#include <math.h>
class Portal : public LevelObject, public VertexProvider {
private:
float width, height, thickness;
Vertex pos;
float orient;
Vertex vertices[4];
float s, c;
void updateVertices() {
float x = width/2*c;
float y = width/2*s;
float ts = thickness/2*s;
float tc = thickness/2*c;
vertices[0].setLocation(pos.getX()-x-ts, pos.getY()-y+tc);
vertices[1].setLocation(pos.getX()-x+ts, pos.getY()-y-tc);
vertices[2].setLocation(pos.getX()+x+ts, pos.getY()+y-tc);
vertices[3].setLocation(pos.getX()+x-ts, pos.getY()+y+tc);
}
void updateOrient() {
s = sinf(orient);
c = cosf(orient);
updateVertices();
}
Polygon createPolygon() const {
Polygon p;
for(int i = 0; i < 4; i++)
p.push_back(vertices[i]);
return p;
}
public:
Portal(float width, float height, float thickness) {
this->width = width;
this->height = height;
this->thickness = thickness;
orient = 0;
updateOrient();
}
float getWidth() const {return width;}
float getHeight() const {return height;}
float getThickness() const {return thickness;}
const Vertex& getPosition() const {return pos;}
void setPosition(Vertex v) {pos = v; updateVertices();}
float getOrientation() const {return orient;}
void setOrientation(float orient) {this->orient = orient; updateOrient();}
virtual std::vector<SharedPtr<LevelObject> > getChildren() {
std::vector<SharedPtr<LevelObject> > children;
for(size_t i = 0; i < 4; i++)
children.push_back(SharedPtr<LevelObject>(new LevelVertex(this, i, this)));
return children;
}
virtual bool hit(const Vertex &v) const {return createPolygon().contains(v);}
virtual int getPriority() const {return 1;}
virtual const char* getType() const {
return "Portal";
}
virtual void move(float x, float y) {
pos += Vertex(x, y);
updateVertices();
}
virtual void rotate(Vertex m, float a) {
orient = fmodf(orient+a, 2*M_PI);
s = sinf(a);
c = cosf(a);
pos -= m;
pos.setLocation(c*pos.getX() - s*pos.getY(), c*pos.getY() + s*pos.getX());
pos += m;
updateOrient();
}
virtual const Vertex* getVertex(size_t id) const {
return &vertices[id];
}
virtual size_t getVertexCount() const {
return 1;
}
virtual void moveVertex(size_t id, float x, float y) {
move(x, y);
}
virtual void rotateVertex(size_t id, Vertex m, float a) {
rotate(m, a);
}
};
#endif /*PORTAL_H_*/