-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ship.cpp
125 lines (100 loc) · 2.96 KB
/
Ship.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
#include "DXUT.h"
#include "Ship.h"
Ship::Ship() : SpaceObject(Vec2(0, 0)), m_texture(0), m_effect(0), m_angularA(0.0f),
m_angularVelocity(0.0), m_angle(0.0f), m_maxAngularSpeed(2.0f), m_engineForce(0.0f)
{
D3DXMatrixIdentity(&m_transform);
D3DXMatrixIdentity(&m_rotation);
m_size = Vec2(100, 62);
}
Texture* texture;
Texture* texture2;
void Ship::draw(Scene* scene, SceneCamera* camera, Device* device)
{
m_effect->setBaseTexture(m_texture);
Effect* effect = m_effect->dxEffect();
m_effect->setWorldMatrix(m_transform);
//apply view and proj params
m_effect->applyCamera(*camera);
effect->SetTechnique(effect->GetTechniqueByName("RenderShip"));
effect->Begin(0, 0);
effect->BeginPass(0);
//DrawScreenSprite(device, 0, Vec2(220.0f - m_size.x * 0.5f, 240.0f - m_size.y * 0.5f), Vec2(m_size.x, m_size.y));
DrawWorldSprite(device, 0, Vec3(-m_size.x * 0.5f, m_size.y * 0.5f, 0.5f), Vec3(m_size.x, -m_size.y, 0.5f));
effect->EndPass();
effect->End();
}
void Ship::loadFromFile(const char* filename)
{
mass = 1.0f;
}
void Ship::update(float fDelta, SceneCamera* sceneCamera)
{
m_angularA = 0.0f;
m_engineForce = 0.0f;
if (GetAsyncKeyState(VK_LEFT))
{
m_angularA = 1.0f;
}
if (GetAsyncKeyState(VK_RIGHT))
{
m_angularA = -1.0f;
}
if (GetAsyncKeyState('W'))
{
m_engineForce = 30.0f;
}
if (GetAsyncKeyState('S'))
{
m_engineForce = -30.0f;
}
computeAngular(fDelta);
computeTranslation(fDelta);
D3DXMatrixTranslation(&m_transform, position.x, position.y, 0.0f);
D3DXMatrixMultiply(&m_transform, &m_rotation, &m_transform);
SpaceObject::update(fDelta, sceneCamera);
}
void Ship::computeTranslation(float dt)
{
float a = m_engineForce / mass;
velocity += m_dir * a * dt;
position += velocity * dt;
if (fabs(a) < 0.01f)
{
velocity -= velocity * dt * 0.1f;
}
}
void Ship::computeAngular(float dt)
{
m_angularVelocity += m_angularA * dt;
m_angularVelocity = Math::clamp(m_angularVelocity, -m_maxAngularSpeed, m_maxAngularSpeed);
m_angle += m_angularVelocity * dt;
D3DXMatrixRotationZ(&m_rotation, m_angle);
if (fabs(m_angularA) < 0.01f)
{
m_angularVelocity -= m_angularVelocity * dt * 0.5f;
}
Vec3 dir(0, 1, 0);
D3DXVec3TransformCoord(&dir, &dir, &m_rotation);
m_dir = Vec2(dir.x, dir.y);
}
void Ship::onLostDevice()
{
SAFE_RELEASE(m_texture);
m_effect->dxEffect()->OnLostDevice();
}
void Ship::onCreateDevice(Device* device)
{
m_effect = new SceneEffect(LoadEffectFromFile(device, "Data/render.fx"));
}
void Ship::onDestroyDevice()
{
m_effect->release();
}
void Ship::onResetDevice(Device* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc)
{
m_effect->dxEffect()->OnResetDevice();
m_texture = LoadTextureFromFile(pd3dDevice, "Data/ships/ARC-170/base.png");
D3DSURFACE_DESC desc;
m_texture->GetLevelDesc(0, &desc);
}