forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.cpp
More file actions
106 lines (95 loc) · 2.59 KB
/
Copy pathShip.cpp
File metadata and controls
106 lines (95 loc) · 2.59 KB
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
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "Ship.h"
#include "SpriteComponent.h"
#include "InputComponent.h"
#include "Game.h"
#include "Laser.h"
#include "CircleComponent.h"
#include "Asteroid.h"
Ship::Ship(Game* game)
:Actor(game)
,mLaserCooldown(0.0f)
,mElapsed(0.0f)
,mIsDead(false)
,mDeadTime(0.0f)
{
// Create a sprite component
this->mSprite = new SpriteComponent(this, 150);
mSprite->SetTexture(game->GetTexture("Assets/Ship.png"));
// Create an input component and set keys/speed
InputComponent* ic = new InputComponent(this);
ic->SetForwardKey(SDL_SCANCODE_W);
ic->SetBackKey(SDL_SCANCODE_S);
ic->SetClockwiseKey(SDL_SCANCODE_A);
ic->SetCounterClockwiseKey(SDL_SCANCODE_D);
ic->SetMaxForwardSpeed(300.0f);
ic->SetMaxAngularSpeed(Math::TwoPi);
this->mCircle = new CircleComponent(this);
mCircle->SetRadius(32.0f);
}
void Ship::UpdateActor(float deltaTime)
{
if (mIsDead) {
mDeadTime += deltaTime;
if (mDeadTime > 1.0f) {
mSprite->SetTexture(GetGame()->GetTexture("Assets/Ship.png"));
SetPosition(GetRespawnPosition());
SetRotation(GetRespawnRotation());
this->mIsDead = false;
}
return;
}
mLaserCooldown -= deltaTime;
if (mElapsed < 1.0f) {
this->mElapsed += deltaTime;
return;
}
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
mSprite->SetTexture(nullptr);
ast->SetState(EDead);
this->mElapsed = 0.0f;
this->mDeadTime = 0.0f;
this->mIsDead = true;
break;
}
}
}
void Ship::ActorInput(const uint8_t* keyState)
{
if (mIsDead) {
return;
}
if (keyState[SDL_SCANCODE_SPACE] && mLaserCooldown <= 0.0f)
{
// Create a laser and set its position/rotation to mine
Laser* laser = new Laser(GetGame());
laser->SetPosition(GetPosition());
laser->SetRotation(GetRotation());
// Reset laser cooldown (half second)
mLaserCooldown = 0.5f;
}
}
void Ship::SetRespawnPosition(const Vector2 respawnPosition) {
this->mRespawnPosition = respawnPosition;
}
Vector2 Ship::GetRespawnPosition() const {
return mRespawnPosition;
}
void Ship::SetRespawnRotation(const float rotation) {
this->mRespawnRotation = rotation;
}
float Ship::GetRespawnRotation() const {
return mRespawnRotation;
}