-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangle.h
63 lines (48 loc) · 1.73 KB
/
rectangle.h
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
//
// rectangle.h
// Cavestory
//
// Created by Alex Gomez on 12/30/15.
// Copyright (c) 2015 Alex Gomez. All rights reserved.
//
#ifndef __Cavestory__rectangle__
#define __Cavestory__rectangle__
#include "globals.h"
class Rectangle {
public:
Rectangle() {};
Rectangle(int x, int y, int width, int height) :
_x(x),
_y(y),
_width(width),
_height(height){
}
const int getCenterX() const { return _x + this->_width / 2; }
const int getCenterY() const { return _y + this->_height / 2; }
const int getLeft() const { return this->_x; }
const int getRight() const { return this->_x + this->_width; }
const int getTop() const { return this->_y; }
const int getBottom() const { return this->_y + this->_height; }
const int getWidth() const { return this->_width; }
const int getHeight() const { return this->_height; }
const int getSide(const sides::Side side) const {
return side == sides::TOP ? this->getTop() :
side == sides::BOTTOM ? this->getBottom() :
side == sides::LEFT ? this->getLeft() :
side == sides::RIGHT ? this->getRight() :
sides::NONE;
}
//takes in another rectangle and check if the 2 are colliding
const bool collidesWith(const Rectangle &other) const {
return this->getRight() >= other.getLeft() &&
this->getLeft() <= other.getRight() &&
this->getTop() <= other.getBottom() &&
this->getBottom() >= other.getTop();
}
const bool isValidRectangle() const {
return (this->_x >= 0 && this->_y >= 0 && this->_width >= 0 && this->_height >= 0);
}
private:
int _x, _y, _width, _height;
};
#endif /* defined(__Cavestory__rectangle__) */