Skip to content

Commit 85be33c

Browse files
committed
Added enemy spawn points to tilemap
1 parent 225e11c commit 85be33c

File tree

6 files changed

+22
-31
lines changed

6 files changed

+22
-31
lines changed

Enemy.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@
55
#include <SFML/Graphics.hpp>
66
#include <cmath>
77

8-
Enemy::Enemy(sf::Vector2f startPosition, sf::Color startColour)
9-
: texture{},
10-
sprite( // Create a enemy sprite
11-
[this]() {
12-
if (!texture.loadFromFile("playerSpritesheet.png"))
13-
throw std::runtime_error("Failed to load sprite sheet");
14-
return sf::Sprite(texture);
15-
}()
16-
)
8+
Enemy::Enemy(sf::Vector2f startPosition, sf::Color startColour, sf::Texture& texture)
9+
: texture(texture), // Store reference to texture
10+
sprite(texture) // Initialize sprite with texture
1711
{
1812

1913
// Create a enemy sprite

Enemy.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Enemy {
88
public:
9-
Enemy(sf::Vector2f startPosition, sf::Color startColour);
9+
Enemy(sf::Vector2f startPosition, sf::Color startColour, sf::Texture& texture);
1010
void Enemy::update(float deltaTime, const sf::Vector2f& playerPosition, bool playerAttacking);
1111
sf::Vector2f getPosition();
1212
void Enemy::draw(sf::RenderWindow& window);

level.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##########
2-
#........#
2+
#.......E#
33
#.....P..#
44
#........#
5-
#........#
5+
#....E...#
66
#........#
77
##########

main.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,6 @@ int main()
3030
player.sprite.setOrigin({240.0f / 2.0f, 350.0f / 2.0f});
3131
player.sprite.setPosition(toSF(playerCollider.pos));
3232

33-
// Future work, add enemies to tilemap
34-
// Create enemies
35-
std::vector<Object> enemyColliders;
36-
std::vector<Enemy> enemies;
37-
// Create enemy 1
38-
sf::Vector2<float> position1(775.f, 400.f); // Set coordinates
39-
Enemy enemy1(position1, sf::Color::Red);
40-
enemies.push_back(enemy1);
41-
// Create enemy 2
42-
sf::Vector2<float> position2(735.f, 240.f); // Set coordinates
43-
Enemy enemy2(position2, sf::Color::Red);
44-
enemies.push_back(enemy2);
45-
// Set up collider lists
46-
for (auto& enemy : enemies) {
47-
colliders.push_back(&enemy.collider);
48-
}
49-
5033
// Create tilemap
5134
MapLoader map;
5235
TileMapRenderer renderer;
@@ -56,33 +39,47 @@ int main()
5639
std::vector<Object> wallObjects;
5740
std::vector<Object*> pickupColliders;
5841
std::vector<Object> pickupObjects;
42+
std::vector<Enemy> enemies;
43+
sf::Texture enemyTex;
44+
if (!enemyTex.loadFromFile("playerSpritesheet.png"))
45+
throw std::runtime_error("Failed to load Wall.png");
5946
sf::Vector2i size = map.getSize();
6047
for (int y = 0; y < size.y; ++y) { // height
6148
for (int x = 0; x < size.x; ++x) { // width
49+
// Walls
6250
if (map.getTile(x, y) == '#') {
6351
wallObjects.reserve(size.x * size.y);
6452
Object wall;
6553
wall.pos = Vec2(x * TILE_SIZE + TILE_SIZE / 2.0f, y * TILE_SIZE + TILE_SIZE / 2.0f);
6654
wall.aabb.min = Vec2(x * TILE_SIZE, y * TILE_SIZE);
6755
wall.aabb.max = Vec2((x + 1) * TILE_SIZE, (y + 1) * TILE_SIZE);
68-
6956
// Add to colliders
7057
wallObjects.push_back(wall);
7158
colliders.push_back(&wallObjects.back());
7259
}
60+
// Pickups
7361
if (map.getTile(x, y) == 'P') {
7462
pickupObjects.reserve(size.x * size.y);
7563
Object pickup;
7664
pickup.pos = Vec2(x * TILE_SIZE + TILE_SIZE / 2.0f, y * TILE_SIZE + TILE_SIZE / 2.0f);
7765
pickup.aabb.min = Vec2(x * TILE_SIZE, y * TILE_SIZE);
7866
pickup.aabb.max = Vec2((x + 1) * TILE_SIZE, (y + 1) * TILE_SIZE);
79-
8067
// Add to colliders
8168
pickupObjects.push_back(pickup);
8269
pickupColliders.push_back(&pickupObjects.back());
8370
}
71+
// Enemies
72+
if (map.getTile(x, y) == 'E') {
73+
Vec2 pos(x * TILE_SIZE + TILE_SIZE / 2.0f, y * TILE_SIZE + TILE_SIZE / 2.0f);
74+
Enemy enemy(toSF(pos), sf::Color::Red, enemyTex);
75+
enemies.push_back(enemy);
76+
}
8477
}
8578
}
79+
// Add enemies to colliders
80+
for (auto& enemy : enemies) {
81+
colliders.push_back(&enemy.collider);
82+
}
8683

8784
// Create game window
8885
sf::RenderWindow window(sf::VideoMode({800, 600}), "2D Game", sf::Style::Titlebar | sf::Style::Close);

main.exe

0 Bytes
Binary file not shown.

main.obj

-1.19 KB
Binary file not shown.

0 commit comments

Comments
 (0)