-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobilegameobject.cpp
75 lines (57 loc) · 1.72 KB
/
mobilegameobject.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
/*
* mobilegameobject.hpp
*
* Description: This file contains the declaration of the MobileGameObject class, which represents a mobile game object in the game.
* It is a base class for objects that can move on the screen.
*
* Author: Brandon Xu
* Date: 4/28/23
*/
#include "mobilegameobject.hpp"
// Constructor that takes starting position
MobileGameObject::MobileGameObject(int x, int y) {
//Initialize member attributes (location)
this->_x = x * 50;
this->_y = y * 50;
// Random direction (0 or 1)
_direction = y % 2;
//_speed = generateSpeed(); // Set speed using abstract setSpeed method
_speed = 1;
//_hitbox = setHitBox(); // Set hitbox using abstract setHitBox method
}
// Draw object to display
void MobileGameObject::draw(sf::RenderWindow& window) {
// Set sprite to most updated position
_sprite.setPosition(_x, _y);
// Set the texture for the sprite
_sprite.setTexture(_texture);
// Draw sprite
window.draw(_sprite);
}
void MobileGameObject::update()
{
// Determine direction
if (_direction) { // Going right
_x -= _speed; // Move right
if (_x < -100) {
_x = 1201; // Return to start after reaching the end of the screen
}
}
else { // Going left
_x += _speed; // Move left
if (_x > 1200) {
_x = -100; // Return to start after reaching the end of the screen
}
}
// Update hitbox position
_hitbox.left = _x;
_hitbox.top = _y;
}
// Returns rectangle object that represents hitbox
sf::IntRect MobileGameObject::get_hitbox() {
return _hitbox; // Return member hitbox
}
// Adds the specified amount to speed modifier
void MobileGameObject::incrementSpeed(int incr) {
_speed += incr;
}