summaryrefslogtreecommitdiffstats
path: root/LevelEdge.h
blob: b2977f164166599635c97d9a121c9f9ec04b92ec (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
#ifndef LEVELEDGE_H_
#define LEVELEDGE_H_

#include "LevelObject.h"
#include "EdgeProvider.h"
#include <math.h>


class LevelEdge : public LevelObject {
  private:
    EdgeProvider *provider;
    size_t id;
    
    static const float width = 1;
    
  public:
    LevelEdge(EdgeProvider *p, size_t i, LevelObject *parent)
    : LevelObject(parent), provider(p), id(i) {}
    
    virtual bool hit(const Vertex &v, float scale) const {
      const Edge &edge = **this;
      const Vertex &v1 = *edge.getVertex1(), &v2 = *edge.getVertex2();
      
      float width = this->width/scale;
      
      if(v.getX() < fminf(v1.getX(), v2.getX())-width)
        return false;
      
      if(v.getX() > fmaxf(v1.getX(), v2.getX())+width)
        return false;
      
      if(v.getY() < fminf(v1.getY(), v2.getY())-width)
        return false;
      
      if(v.getY() > fmaxf(v1.getY(), v2.getY())+width)
        return false;
      
      Vertex r = v2 - v1, p = v - v1;
      r *= (r.getX()*p.getX() + r.getY()*p.getY())/v1.distanceSq(v2);
      
      return (r.distanceSq(p) <= width*width);
    }
    
    virtual int getPriority() const {return 500;}
    
    virtual const char* getType() const {
      return "LevelEdge";
    }
    
    virtual bool canMove() const {return true;}
    
    virtual void move(float x, float y) {
      provider->moveEdge(id, x, y);
    }
    
    virtual bool canRotate() const {return true;}
    
    virtual void rotate(Vertex m, float a) {
      provider->rotateEdge(id, m, a);
    }
    
    const EdgeProvider* getProvider() const {
      return provider;
    }
    
    size_t getId() const {
      return id;
    }
    
    const Edge* operator->() const {return provider->getEdge(id);}
    const Edge& operator*() const {return *provider->getEdge(id);}
};

#endif /*LEVELEDGE_H_*/