@@ -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);
0 commit comments