Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Decoration sprites #151

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(MVP_SOURCES
src/mvp/objects/Ship.cpp
src/mvp/objects/Sprite.cpp
src/mvp/objects/TreasureIsland.cpp
src/mvp/objects/Decoration.cpp
)

set(INCLUDE_DIRS
Expand Down
Binary file modified src/assets/admirals_texture_atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 121 additions & 8 deletions src/mvp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "managers/SelectionManager.hpp"
#include "managers/UIManager.hpp"
#include "objects/Background.hpp"
#include "objects/Decoration.hpp"
#include "objects/Grid.hpp"
#include "objects/IconifiedButton.hpp"
#include "objects/MenuMovingShip.hpp"
Expand Down Expand Up @@ -56,10 +57,117 @@ void ToggleHelpMenu() {
GameData::engine->ToggleLayer(GameData::HelpMenuIdx);
}

void GenerateTreasureIsland(const Texture &atlas) {
// generate island
int index = 0;
for (const auto &islandLocation : IslandLocations) {
GameData::GameScene->MakeGameObject<TreasureIsland>(
"treasureIsland" + std::to_string(index++), atlas, 2,
Rect(static_cast<float>(islandLocation.x),
static_cast<float>(islandLocation.y), GameData::CellSize,
GameData::CellSize));

GameData::GameScene->MakeGameObject<Decoration>(
"treasureIslandBeach" + std::to_string(index), atlas, 2, Vector2(),
0,
Rect(static_cast<float>(islandLocation.x),
static_cast<float>(islandLocation.y), GameData::CellSize,
GameData::CellSize));
}

// generate water edges around island
std::vector<Vector2> spriteoffset = {
Vector2(6, 2), Vector2(9, 3), Vector2(5, 2), Vector2(11, 2),
Vector2(8, 1), Vector2(6, 1), Vector2(10, 0), Vector2(5, 1)};
for (const auto &islandLocation : IslandLocations) {
index = 0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) {
continue;
}
GameData::GameScene->MakeGameObject<Decoration>(
"wateredgeisland" + std::to_string(islandLocation.x + dx) +
std::to_string(islandLocation.y + dy),
atlas, 1, spriteoffset[index], 0,
Rect(static_cast<float>(islandLocation.x + dx),
static_cast<float>(islandLocation.y + dy),
GameData::CellSize, GameData::CellSize));
index++;
}
}
}
}

void GenerateWater(const Texture &atlas) {
// generate water
for (int x = 0; x < GameData::GridCells; x++) {
for (int y = 0; y < GameData::GridCells; y++) {
// avoid base islands
if ((x < 2 && y < 4) || (x >= (GameData::GridCells - 2) &&
y >= (GameData::GridCells - 4))) {
continue;
}
// avoid treasure islands
bool islandFound = false;
for (const auto &islandLocation : IslandLocations) {
if ((x >= islandLocation.x - 1 && x <= islandLocation.x + 1) &&
(y >= islandLocation.y - 1 && y <= islandLocation.y + 1)) {
islandFound = true;
break;
}
}
if (islandFound) {
continue;
}
GameData::GameScene->MakeGameObject<Decoration>(
"water" + std::to_string(x) + std::to_string(y), atlas, 1,
Vector2(9, 2), 0,
Rect(static_cast<float>(x), static_cast<float>(y),
GameData::CellSize, GameData::CellSize));
}
}

// generate water edges at the base islands
for (int y = 0; y < 3; y++) {
GameData::GameScene->MakeGameObject<Decoration>(
"wateredgetop" + std::to_string(y), atlas, 1, Vector2(8, 1), 0,
Rect(static_cast<float>(1), static_cast<float>(y),
GameData::CellSize, GameData::CellSize));
}
GameData::GameScene->MakeGameObject<Decoration>(
"wateredgetop3", atlas, 1, Vector2(5, 1), 0,
Rect(static_cast<float>(1), static_cast<float>(3), GameData::CellSize,
GameData::CellSize));
GameData::GameScene->MakeGameObject<Decoration>(
"wateredgetop4", atlas, 1, Vector2(10, 0), 0,
Rect(static_cast<float>(0), static_cast<float>(3), GameData::CellSize,
GameData::CellSize));

for (int y = 0; y < 3; y++) {
GameData::GameScene->MakeGameObject<Decoration>(
"wateredgebot" + std::to_string(y), atlas, 1, Vector2(11, 2), 0,
Rect(static_cast<float>(GameData::GridCells - 2),
static_cast<float>(GameData::GridCells - 1 - y),
GameData::CellSize, GameData::CellSize));
}
GameData::GameScene->MakeGameObject<Decoration>(
"wateredgebot3", atlas, 1, Vector2(6, 2), 0,
Rect(static_cast<float>(GameData::GridCells - 2),
static_cast<float>(GameData::GridCells - 4), GameData::CellSize,
GameData::CellSize));
GameData::GameScene->MakeGameObject<Decoration>(
"wateredgebot4", atlas, 1, Vector2(9, 3), 0,
Rect(static_cast<float>(GameData::GridCells - 1),
static_cast<float>(GameData::GridCells - 4), GameData::CellSize,
GameData::CellSize));
}

void CreateGameBoard(const Texture &atlas) {
GameData::GameScene->MakeGameObject<Background>("background", blue,
Color::BLACK);
GameData::GameScene->MakeGameObject<Grid>("grid", Color::BLACK);

GameData::GameScene->MakeGameObject<Quad>(
"islandLeft", 2, Rect(0, 0, GameData::CellSize, GameData::CellSize * 3),
green);
Expand All @@ -69,14 +177,19 @@ void CreateGameBoard(const Texture &atlas) {
static_cast<float>(GameData::GridCells) - 3, GameData::CellSize,
GameData::CellSize * 3),
green);
int index = 0;
for (const auto &islandLocation : IslandLocations) {
GameData::GameScene->MakeGameObject<TreasureIsland>(
"treasureIsland" + std::to_string(index++), atlas, 2,
Rect(static_cast<float>(islandLocation.x),
static_cast<float>(islandLocation.y), GameData::CellSize,
GameData::CellSize));
}

GenerateTreasureIsland(atlas);
GenerateWater(atlas);

// generate additional decorations
GameData::GameScene->MakeGameObject<Decoration>(
"ring", atlas, 4, Vector2(), 2,
Rect(static_cast<float>(0), static_cast<float>(GameData::GridCells - 1),
GameData::CellSize, GameData::CellSize));
GameData::GameScene->MakeGameObject<Decoration>(
"waterGreen", atlas, 5, Vector2(), 2,
Rect(static_cast<float>(GameData::GridCells - 1), static_cast<float>(0),
GameData::CellSize, GameData::CellSize));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract some of this into seperate methods to improve readability

}

void CreateGameUI(const Texture &atlas,
Expand Down
52 changes: 52 additions & 0 deletions src/mvp/objects/Decoration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "Decoration.hpp"
#include "DataObjects.hpp"

using namespace ::admirals;
using namespace ::admirals::mvp;
using namespace ::admirals::mvp::objects;

Decoration::Decoration(const std::string &name, const Texture &source, int type,
Vector2 spriteOffset, float order, const Rect &bounds)
: Sprite(name, source, order, bounds,
Decoration::DecorationTypeToTexOffset(type, spriteOffset)) {}

void Decoration::Render(const EngineContext &ctx) const { Sprite::Render(ctx); }

std::vector<Vector2>
Decoration::DecorationTypeToTexOffset(int type, Vector2 spriteOffset) {

std::vector<Vector2> textureOffset;
switch (type) {
case DecorationType::Water:
textureOffset.push_back((Vector2(8, 3) + spriteOffset) *
GameData::SpriteSize);
textureOffset.push_back((Vector2(8, 7) + spriteOffset) *
GameData::SpriteSize);
textureOffset.push_back((Vector2(8, 11) + spriteOffset) *
GameData::SpriteSize);
textureOffset.push_back((Vector2(8, 15) + spriteOffset) *
GameData::SpriteSize);
break;
case DecorationType::Beach:
textureOffset.push_back(Vector2(15, 1) * GameData::SpriteSize);
break;
case DecorationType::Grass:
break;
case DecorationType::Ring:
textureOffset.push_back(Vector2(13, 0) * GameData::SpriteSize);
textureOffset.push_back(Vector2(14, 0) * GameData::SpriteSize);
textureOffset.push_back(Vector2(15, 0) * GameData::SpriteSize);
textureOffset.push_back(Vector2(16, 0) * GameData::SpriteSize);
break;
case DecorationType::Green:
textureOffset.push_back(Vector2(0, 7) * GameData::SpriteSize);
textureOffset.push_back(Vector2(1, 7) * GameData::SpriteSize);
textureOffset.push_back(Vector2(2, 7) * GameData::SpriteSize);
textureOffset.push_back(Vector2(3, 7) * GameData::SpriteSize);
break;
default:
textureOffset.push_back(Vector2(0, 0));
break;
}
return textureOffset;
}
27 changes: 27 additions & 0 deletions src/mvp/objects/Decoration.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "objects/Sprite.hpp"

namespace admirals::mvp::objects {

class Decoration : public Sprite {
public:
enum DecorationType {
None = 0,
Water = 1,
Beach = 2,
Grass = 3,
Ring = 4,
Green = 5,
};

Decoration(const std::string &name, const Texture &source, int type,
Vector2 spriteOffset = Vector2(), float order = 0,
const Rect &bounds = Rect());

void Render(const EngineContext &ctx) const override;

std::vector<Vector2> DecorationTypeToTexOffset(int type,
Vector2 spriteOffset);
};

} // namespace admirals::mvp::objects
9 changes: 8 additions & 1 deletion src/mvp/objects/TreasureIsland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ using namespace ::admirals::mvp::objects;
TreasureIsland::TreasureIsland(const std::string &name, const Texture &source,
float order, const Rect &bounds)
: Sprite(name, source, order, bounds,
Vector2(GameData::SpriteSize, GameData::SpriteSize * 0)) {}
{(Vector2(15, 2) * GameData::SpriteSize),
(Vector2(15, 2) * GameData::SpriteSize),
(Vector2(15, 2) * GameData::SpriteSize),
(Vector2(15, 2) * GameData::SpriteSize),
(Vector2(16, 2) * GameData::SpriteSize),
(Vector2(16, 2) * GameData::SpriteSize),
(Vector2(16, 2) * GameData::SpriteSize),
(Vector2(16, 2) * GameData::SpriteSize)}) {}

void TreasureIsland::Render(const EngineContext &ctx) const {
Sprite::Render(ctx);
Expand Down