This repository was archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTile.cpp
137 lines (112 loc) · 4.06 KB
/
Tile.cpp
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
/*
* Members: Garrett Young, Nick Tallents, Oliver Spryn, Zac Stahl
* Course: 322, Obj. Oriented / Adv. Programming
* Date: 04 Dec 2013
* Description: This file implements the Tile class
*/
#include "Tile.h"
Tile::Tile(Game* game, Graphics* graphics) :
direction(-1), game(game), graphics(graphics), orientation(0) {
this->collisionType = entityNS::NONE;
this->graphic = tileNS::GRAPHIC;
this->numCols = 1;
this->mass = 1.0f;
this->spriteData.height = tileNS::HEIGHT;
this->spriteData.scale = 1;
this->spriteData.width = tileNS::WIDTH;
this->edge.bottom = tileNS::HEIGHT / 2;
this->edge.left = -tileNS::WIDTH/ 2;
this->edge.right = tileNS::WIDTH / 2;
this->edge.top = -tileNS::HEIGHT / 2;
this->spriteData.rect.bottom = tileNS::HEIGHT / 2;
this->spriteData.rect.left = -tileNS::WIDTH / 2;
this->spriteData.rect.right = tileNS::WIDTH / 2;
this->spriteData.rect.top = -tileNS::HEIGHT / 2;
this->active = false;
}
Tile::~Tile() {
}
bool Tile::collidesWith(Entity &ent, D3DXVECTOR2 &collisionVector) {
bool collide = Entity::collidesWith(ent, collisionVector);
//Store the side of the collision on the Tile
if (collide) {
//D3DXVECTOR2 v = this->intersectDepthVector(ent);
//float absX = fabsf(v.x);
//float absY = fabsf(v.y);
D3DXVECTOR2 vel = ent.getVelocity();
int x = static_cast<int>(vel.x);
int y = static_cast<int>(vel.y);
if (this->direction == -1) {
if (x > 0) {
this->direction = 0; //LEFT
return collide;
} else if (x < 0) {
this->direction = 2; //RIGHT
return collide;
}
if (y > 0) {
this->direction = 3; //DOWN
return collide;
} else if (y < 0) {
this->direction = 1; //UP
return collide;
}
}
} else {
this->direction = -1;
}
return collide;
}
bool Tile::collidesWithBottom(Entity &ent, D3DXVECTOR2 &collisionVector) {
return Tile::collidesWith(ent, collisionVector) && this->direction == 1;
}
bool Tile::collidesWithLeft(Entity &ent, D3DXVECTOR2 &collisionVector) {
return Tile::collidesWith(ent, collisionVector) && this->direction == 0;
}
bool Tile::collidesWithRight(Entity &ent, D3DXVECTOR2 &collisionVector) {
return Tile::collidesWith(ent, collisionVector) && this->direction == 2;
}
bool Tile::collidesWithTop(Entity &ent, D3DXVECTOR2 &collisionVector) {
return Tile::collidesWith(ent, collisionVector) && this->direction == 3;
}
int Tile::getDirection() {
return this->direction;
}
int Tile::getOrientation() {
return this->orientation;
}
void Tile::initialize() {
//Initialize the graphic texture
if (!this->tm.initialize(this->graphics, this->graphic))
throw GameError(gameErrorNS::FATAL_ERROR, "Could not initialize the Tile graphic");
//Initialize the Tile object
Entity::initialize(this->game, tileNS::WIDTH, tileNS::HEIGHT, numCols, &this->tm);
}
//THANK YOU SO MUCH!!!
//http://codeblog.shawson.co.uk/8-week-game-competition/how-i-tackled-collision-detection/
D3DXVECTOR2 Tile::intersectDepthVector(Entity &ent) {
//Get the centers of each object
const D3DXVECTOR2* entCenter = ent.getCenter();
const D3DXVECTOR2* tileCenter = this->getCenter();
//Calculate the current and minimum non-intersecting distances between centers
float distanceX = tileCenter->x - entCenter->x;
float distanceY = tileCenter->y - entCenter->y;
int minDistanceX = spriteData.width / 2 + ent.getWidth() / 2;
int minDistanceY = spriteData.height / 2 + ent.getHeight() / 2;
//Calculate and return the intersection depths
float depthX = distanceX > 0.0f ? static_cast<float>(minDistanceX) - distanceX : -static_cast<float>(minDistanceX) - distanceX;
float depthY = distanceY > 0.0f ? static_cast<float>(minDistanceY) - distanceY : -static_cast<float>(minDistanceY) - distanceY;
return D3DXVECTOR2(depthX, depthY);
}
void Tile::setGraphic(string graphic, int numCols) {
this->graphic = graphic;
this->numCols = numCols;
}
void Tile::setOrientation(int orientation) {
if (orientation > 3 || orientation < 0) {
throw InvalidOrientation("The Tile orientation value is invalid");
} else {
this->orientation = orientation;
setCurrentFrame(orientation);
}
}