-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ship.cpp
77 lines (70 loc) · 1.88 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
// ----------------------------------------------------------------
// 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 "CircleComponent.h"
#include "Game.h"
#include "Laser.h"
#include "Asteroid.h"
#include <cstdio>
Ship::Ship(Game* game)
:Actor(game)
,mLaserCooldown(0.0f)
,revivalTime(0.0f)
{
// Create an animated sprite component
msc = new SpriteComponent(this, 150);
msc->SetTexture(game->GetTexture("Assets/ShipWithThrust.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);
mCircle = new CircleComponent(this);
mCircle->SetRadius(msc->GetTexWidth() * 0.75f);
}
void Ship::UpdateActor(float deltaTime)
{
if (revivalTime <= 0.0f)
{
mLaserCooldown -= deltaTime;
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
GetGame()->RemoveSprite(msc);
RemoveComponent(msc);
revivalTime = 2.0f;
break;
}
}
}
else {
if (revivalTime - deltaTime <= 0) {
GetGame()->AddSprite(msc);
AddComponent(msc);
SetPosition(Vector2(512.0f, 384.0f));
SetRotation(0.0f);
}
revivalTime -= deltaTime;
}
}
void Ship::ActorInput(const uint8_t* keyState)
{
if (keyState[SDL_SCANCODE_SPACE] && mLaserCooldown <= 0.0f)
{
Laser* laser = new Laser(GetGame());
laser->SetPosition(GetPosition());
laser->SetRotation(GetRotation());
mLaserCooldown = 0.5f;
}
}