/
ship.cpp
executable file
·73 lines (66 loc) · 2.89 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
// Programming 2D Games
// Copyright (c) 2012 by:
// Charles Kelly
// CollisionTypes ship.cpp v1.0
#include "ship.h"
//=============================================================================
// default constructor
//=============================================================================
Ship::Ship() : Entity()
{
spriteData.width = shipNS::WIDTH; // size of Ship1
spriteData.height = shipNS::HEIGHT;
spriteData.x = shipNS::X; // location on screen
spriteData.y = shipNS::Y;
spriteData.rect.bottom = shipNS::HEIGHT; // rectangle to select parts of an image
spriteData.rect.right = shipNS::WIDTH;
velocity.x = 0; // velocity X
velocity.y = 0; // velocity Y
startFrame = 0; // first frame of ship animation
endFrame = 0; // last frame of ship animation
currentFrame = startFrame;
radius = shipNS::WIDTH/2.0; // collision radius
collision = false;
collisionType = entityNS::CIRCLE;
target = false;
}
//=============================================================================
// Initialize the Ship.
// Post: returns true if successful, false if failed
//=============================================================================
bool Ship::initialize(Game *gamePtr, int width, int height, int ncols,
TextureManager *textureM)
{
return(Entity::initialize(gamePtr, width, height, ncols, textureM));
}
//=============================================================================
// update
// typically called once per frame
// frameTime is used to regulate the speed of movement and animation
//=============================================================================
void Ship::update(float frameTime)
{
switch (direction) // rotate ship
{
case shipNS::LEFT:
spriteData.angle -= frameTime * shipNS::ROTATION_RATE; // rotate left
break;
case shipNS::RIGHT:
spriteData.angle += frameTime * shipNS::ROTATION_RATE; // rotate right
break;
}
spriteData.x += velocity.x * frameTime;
velocity.x = 0;
spriteData.y += velocity.y * frameTime;
velocity.y = 0;
Entity::update(frameTime);
// wrap around screen
if (spriteData.x > GAME_WIDTH) // if off screen right
spriteData.x = -shipNS::WIDTH; // position off screen left
else if (spriteData.x < -shipNS::WIDTH) // else if off screen left
spriteData.x = GAME_WIDTH; // position off screen right
if (spriteData.y < -shipNS::HEIGHT) // if off screen top
spriteData.y = GAME_HEIGHT; // position off screen bottom
else if (spriteData.y > GAME_HEIGHT) // else if off screen bottom
spriteData.y = -shipNS::HEIGHT; // position off screen top
}