-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntity.h
More file actions
161 lines (119 loc) · 3.94 KB
/
Entity.h
File metadata and controls
161 lines (119 loc) · 3.94 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef SUPERMARIOBROS_ENTITY_H
#define SUPERMARIOBROS_ENTITY_H
#include <SFML/System.hpp>
#include <cstdlib>
#include <memory>
#include "Animation.h"
#include "Hitbox.h"
#include "SFML/Graphics.hpp"
#define GRIDBOX_SIZE 16
class Event;
enum class EntityType
{
GOOMBA,
SMALL_MARIO,
BIG_MARIO,
FIRE_MARIO,
PIPE,
GROUND,
BLOCK,
BLOCK_SHARD,
MUSHROOM,
FIREFLOWER,
INVISIBLE_WALL,
FIREBALL,
};
std::string convertSideToString(EntitySide side);
bool isEnemy(EntityType type);
bool isMario(EntityType entityType);
bool isObject(EntityType entityType);
class Entity;
struct Collision
{
Entity* entity;
EntitySide side;
float yIntersection;
float xIntersection;
};
class Entity
{
public:
static const float NO_MAX_VELOCITY_VALUE;
static const float MAX_FALLING_VELOCITY;
Entity(const sf::Texture& texture,
size_t spriteWidth,
size_t spriteHeight,
const Hitbox& hitbox,
EntityType type,
const sf::Vector2f& position,
float maxVelocity = NO_MAX_VELOCITY_VALUE);
virtual ~Entity();
[[nodiscard]] float getBottom() const;
[[nodiscard]] float getTop() const;
[[nodiscard]] float getLeft() const;
[[nodiscard]] float getRight() const;
[[nodiscard]] float getWidth() const;
[[nodiscard]] virtual float getHeight() const;
virtual void setAnimationFromState();
[[nodiscard]] sf::Vector2f getVelocity() const;
[[nodiscard]] sf::Vector2f getAcceleration() const;
void setVelocity(const sf::Vector2f& newVelocity);
void setAcceleration(const sf::Vector2f& newAcceleration);
void updatePosition();
virtual void doInternalCalculations();
void addPositionDelta(float deltaX, float deltaY);
void updateAnimation();
virtual void draw(sf::RenderWindow& window);
virtual void terminate();
void setCleanupFlag();
bool needsCleanup();
const float GRAVITY_ACCELERATION = 1;
sf::Vector2f mDeltaP;
[[nodiscard]] virtual EntityType getType() const;
bool collideWithEntity(std::vector<std::unique_ptr<Entity>>& entities);
bool collideWithEntity(std::unique_ptr<Entity>& entity);
virtual void setPosition(float x, float y);
void setMaxVelocity(float maxVelocity);
protected:
bool detectCollision(Entity& other);
virtual void onCollision(const Collision& collision);
void clampX(float spriteX, float newSpriteX);
void clampY(float spriteY, float newSpriteY);
void handleCollision(Collision collision, Entity& entity);
virtual const Hitbox& getHitbox(EntityType type) const;
Hitbox getProjectedYHitbox(EntityType type) const;
Hitbox getProjectedXHitbox(EntityType type) const;
Hitbox createSpriteBoundsHitbox() const;
void updateHitboxPositions();
void dispatchEvent(const Event& event);
sf::Sprite mActiveSprite;
sf::Vector2f mVelocity;
sf::Vector2f mAcceleration;
bool mChangingDirection;
Animation* mActiveAnimation;
size_t mSpriteWidth;
size_t mSpriteHeight;
float mMaxVelocity;
// Current theory is that there are two hitboxes
// mMarioCollisionHitbox is used to detect collision for Mario against
// an enemy. It is typically smaller to make the game feel more "fair"
// The other is used for all (??) other collisions.
Hitbox mMarioCollisionHitbox;
Hitbox mSpriteBoundsHitbox;
bool mCleanupFlag = false;
// TODO: This only applies to Mario subclass, but
// we check it up here???
bool mInputEnabled;
int mLookDirection;
float sfmlYToScreenY(float y) const;
float screenYToSfmlY(float y) const;
private:
// These functions give the midpoint of the top edge
// Prefer getTop(), getLeft(), etc.
[[nodiscard]] float getY() const;
[[nodiscard]] float getX() const;
[[nodiscard]] sf::Vector2f upperCenterToUpperLeft(
const sf::Vector2f& originalPosition) const;
EntityType mType;
};
#endif // SUPERMARIOBROS_ENTITY_H