-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGoal.cpp
159 lines (117 loc) · 4.06 KB
/
Goal.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Goal.h"
#include <stdlib.h>
#include <cstdlib>
extern Status status;
#define M_RADIUS 0.14
#define PI 3.14159265
#define M_HEIGHT 0.01
#define FVF_VERTEX D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1
struct _VERTEX {
D3DXVECTOR3 pos;
D3DXVECTOR3 norm;
float tu;
float tv;
};
Goal::Goal(int stage) {
this->stage = stage;
ZeroMemory(&m_mtrl, sizeof(m_mtrl)); // memsetÀ» ÅëÇØ ¸ðµÎ 0À¸·Î ÃʱâÈ
this->m_radius = M_RADIUS;
this->m_pSphereMesh = nullptr;
D3DXMatrixIdentity(&m_mLocal);
this->goalImageFileName = "goal";
}
Goal::~Goal(void) {}
bool Goal::create(IDirect3DDevice9* pDevice) {
if (NULL == pDevice) return false;
m_mtrl.Diffuse = d3d::WHITE;
m_mtrl.Ambient = d3d::WHITE;
m_mtrl.Specular = d3d::WHITE;
m_mtrl.Emissive = d3d::BLACK;
m_mtrl.Power = 100.0f;
pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
this->m_pSphereMesh = _createMappedSphere(pDevice);
string filePath = "./image/" + this->goalImageFileName + ".bmp";
if (FAILED(D3DXCreateTextureFromFile(pDevice, filePath.c_str(), &Tex))) {
return false;
}
return true;
}
void Goal::destroy(void) {
if (m_pSphereMesh != NULL) {
m_pSphereMesh->Release();
m_pSphereMesh = NULL;
}
}
void Goal::draw(IDirect3DDevice9* pDevice, const D3DXMATRIX& mWorld) {
if (NULL == pDevice)
return;
pDevice->SetTransform(D3DTS_WORLD, &mWorld);
pDevice->MultiplyTransform(D3DTS_WORLD, &m_mLocal);
pDevice->SetTexture(0, Tex);
pDevice->SetMaterial(&m_mtrl);
m_pSphereMesh->DrawSubset(0);
}
bool Goal::hasIntersected(Jumper& jumper) {
D3DXVECTOR3 cord = this->getPosition();
D3DXVECTOR3 jumper_cord = jumper.getPosition();
double xDistance = abs((cord.x - jumper_cord.x) * (cord.x - jumper_cord.x));
double zDistance = abs((cord.z - jumper_cord.z) * (cord.z - jumper_cord.z));
double totalDistance = sqrt(xDistance + zDistance);
if (totalDistance < (this->getRadius() + JUMPERWIDTH / 2)) {
return true;
}
return false;
}
void Goal::hitBy(Jumper& jumper) {
if (this->hasIntersected(jumper)) {
if (stage == 1) {
jumper.setPosition(20, 0, 0.3);
status.setNumStage(2);
}
if (stage == 2) {
jumper.setPosition(40, 0, 0.3);
status.setNumStage(3);
}
if (stage == 3) {
jumper.setPosition(60, 0, 0.5);
status.setNumStage(4);
}
}
}
const D3DXMATRIX& Goal::getLocalTransform(void) const { return m_mLocal; }
void Goal::setLocalTransform(const D3DXMATRIX& mLocal) { m_mLocal = mLocal; }
void Goal::setPosition(float x, float y, float z) {
D3DXMATRIX m;
this->center_x = x;
this->center_y = y;
this->center_z = z;
D3DXMatrixTranslation(&m, x, y, z);
this->setLocalTransform(m);
}
LPD3DXMESH Goal::_createMappedSphere(IDirect3DDevice9* pDev) {
LPD3DXMESH mesh;
if (FAILED(D3DXCreateSphere(pDev, this->getRadius(), 50, 50, &mesh, NULL)))
return nullptr;
LPD3DXMESH texMesh;
if (FAILED(mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM, FVF_VERTEX, pDev, &texMesh)))
return mesh;
mesh->Release();
struct _VERTEX* pVerts;
if (SUCCEEDED(texMesh->LockVertexBuffer(0, reinterpret_cast<void**>(&pVerts)))) {
int numVerts = texMesh->GetNumVertices();
for (int i = 0; i < numVerts; i++) {
pVerts->tu = asinf(pVerts->norm.x) / D3DX_PI + 0.5f;
pVerts->tv = asinf(pVerts->norm.y) / D3DX_PI + 0.5f;
pVerts++;
}
texMesh->UnlockVertexBuffer();
}
return texMesh;
}
D3DXVECTOR3 Goal::getPosition() const {
D3DXVECTOR3 org(center_x, center_y, center_z);
return org;
}
float Goal::getRadius(void) const { return (float)(M_RADIUS); }