Page MenuHomePhabricator (Chris)

No OneTemporary

Size
22 KB
Referenced Files
None
Subscribers
None
diff --git a/enfucraft.pro b/enfucraft.pro
index 6c510f6..0b64a71 100644
--- a/enfucraft.pro
+++ b/enfucraft.pro
@@ -1,42 +1,46 @@
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += c++11
SOURCES += main.cpp \
game.cpp \
tilemap.cpp \
selector.cpp \
playerhud.cpp \
boardelem.cpp \
boardelems.cpp \
textureholder.cpp \
hover.cpp \
guiwindow.cpp \
purchaseguielem.cpp \
- guichoosebuilding.cpp
+ guichoosebuilding.cpp \
+ rounddice.cpp \
+ guirounddice.cpp
LIBS += -lsfml-window -lsfml-system -lsfml-graphics -lsfml-audio
DESTDIR = ../build_release_enfucraft
assets.path = $${DESTDIR}/assets
assets.files = assets/*
INSTALLS += assets
HEADERS += \
game.h \
tilemap.h \
selector.h \
playerhud.h \
boardelem.h \
boardelems.h \
textureholder.h \
hover.h \
guiwindow.h \
purchaseguielem.h \
guichoosebuilding.h \
- elemsdescription.h
+ elemsdescription.h \
+ rounddice.h \
+ guirounddice.h
RESOURCES += \
images.qrc
diff --git a/game.cpp b/game.cpp
index 70ba3a8..1ea4ae0 100644
--- a/game.cpp
+++ b/game.cpp
@@ -1,453 +1,485 @@
#include "game.h"
namespace efc {
void Game::initBoard()
{
// Grass tile starts at 342 and has 11 tiles
// int level[256];
// int level[256];
// Structure of the board
/*
{
342, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3 }
*/
// Fill the array
for (int i=0;i<256;i++)
{
int grass = (rand() % 10) + 1;
level[i] = 342 + grass;
level[i] = level[i];
}
// level[0] = 441;
// level[240] = 0;
// level[255] = 0;
// level[15] = 0;
level[8] = 813;
level[24] = 803;
level[40] = 803;
level[56] = 803;
level[72] = 803;
level[88] = 801;
level[167] = 809;
level[183] = 803;
level[199] = 803;
level[215] = 803;
level[231] = 803;
level[247] = 812;
level[112] = 811;
for (int i=113;i<117;i++)
level[i] = 816;
level[117] = 815;
level[138] = 800;
for (int i=139;i<143;i++)
level[i] = 816;
level[143] = 814;
map.load(&textures, sf::Vector2u(efc::TILE_SIZE, efc::TILE_SIZE), level, efc::BOARD_SIZE, efc::BOARD_SIZE);
PlayerHud playerHud1(&textures, std::rand() % 80, &gameFont, 32,0);
PlayerHud playerHud2(&textures, std::rand() % 30, &gameFont, 32,1);
PlayerHud playerHud3(&textures, std::rand() % 60, &gameFont, 32,2);
PlayerHud playerHud4(&textures, std::rand() % 50, &gameFont, 32,3);
players[0] = playerHud1;
players[1] = playerHud2;
players[2] = playerHud3;
players[3] = playerHud4;
+
+
+
+
players[0].setActive(true);
setCurrentNeighbours();
}
void Game::setCurrentNeighbours ()
{
currentNeighbours = players[turn].getNeighbours();
}
void Game::loadAssets()
{
if (!textureFaces.loadFromFile("assets/img/faces.jpg"))
std::exit(1);
if (!textureTiles.loadFromFile("assets/img/zw-tilesets/_MAP.png"))
std::exit(1);
if (!gameFont.loadFromFile("assets/fnt/8bitOperatorPlus-Regular.ttf"))
{
std::exit(1);
}
if (!menuFont.loadFromFile("assets/fnt/MorrisJensonInitialen.ttf"))
{
std::exit(1);
}
- menuTxt.setFont(menuFont);
- menuTxt.setCharacterSize(120);
- menuTxt.setString(gameTitle);
+ menuTxt.setFont(menuFont);
+ menuTxt.setCharacterSize(120);
+ menuTxt.setString(gameTitle);
- menuTxt.setColor(sf::Color(55, 255, 35, 85));
+ menuTxt.setColor(sf::Color(55, 255, 35, 85));
- int width = menuTxt.getLocalBounds().width;
- int height = menuTxt.getLocalBounds().height;
+ int width = menuTxt.getLocalBounds().width;
+ int height = menuTxt.getLocalBounds().height;
- menuTxt.setPosition(400-(width/2),300-(height/2)-150);
- menuTxt.setScale(0.5, 0.5);
+ menuTxt.setPosition(400-(width/2),300-(height/2)-150);
+ menuTxt.setScale(0.5, 0.5);
}
void Game::showMenu()
{
menuBackground.setTexture(textures.textureMenu);
musicMenu.play();
musicMenu.setLoop(true);
currentState = state_menu;
}
void Game::hideMenu()
{
musicMenu.stop();
}
void Game::showGameBoard()
{
musicGame.play();
musicGame.setLoop(true);
currentState = state_game;
}
void Game::hideGameBoard()
{
musicGame.play();
}
Game::Game():
window(sf::VideoMode(800, 600), "Pagan Board"),
viewTiles(sf::FloatRect(00, 00, 400, 400)),
viewFull(sf::FloatRect(00, 00, 400, 400)),
viewGui(sf::FloatRect(00, 00, 112, 400)),
selector(efc::TILE_SIZE),
guiSelectBuilding(&textures),
turn(0),
- gameTitle(" PAgAN\nBOaRD ")
+ gameTitle(" PAgAN\nBOaRD "),
+ roundDice(players),
+ roundNumber(1),
+ guiRoundDice(&textures)
{
-
+ guiRoundDice.active = true;
showPlayerBoardElems = false;
if (!musicGame.openFromFile("assets/audio/game.ogg"))
std::exit(1);
if (!musicBackground.openFromFile("assets/audio/wind.ogg"))
std::exit(1);
if (!musicMenu.openFromFile("assets/audio/menu.ogg"))
std::exit(1);
if (!sfxClickBuffer.loadFromFile("assets/audio/click.ogg"))
std::exit(1);
sfxClick.setBuffer(sfxClickBuffer);
window.setVerticalSyncEnabled(true);
Hover hover;
GuiWindow guiWindow(&textures);
+
+
std::srand (time(NULL));
loadAssets();
viewGui.setViewport(sf::FloatRect(0.8f,0, 1.0f, 1.0f));
viewTiles.setViewport(sf::FloatRect(0,0, 0.8f, 1.0f));
selector.changeColor(turn); //This is only for the test TODO: remove
initBoard();
showMenu();
// run the main loop
while (window.isOpen())
{
std::string resultCommand = "";
// handle events
sf::Event event;
while (window.pollEvent(event))
{
sf::Vector2i localPositionTmp = sf::Mouse::getPosition(window);
sf::Vector2f localPosition = window.mapPixelToCoords(localPositionTmp,viewTiles);
sf::Vector2f localPositionGui = window.mapPixelToCoords(localPositionTmp,viewGui);
int mousePosX = (int)localPosition.x / efc::TILE_SIZE;
int mousePosY = (int)localPosition.y / efc::TILE_SIZE;
int mousePos = efc::transCords(sf::Vector2i(mousePosX, mousePosY));
if(event.type == sf::Event::Closed)
window.close();
if (currentState==state_gui_elem)
{
resultCommand = guiSelectBuilding.getElem(localPosition);
if (resultCommand.find("elem_")==0)
command(resultCommand);
else
command("hide_gui_elem_description");
}
+
+
if (currentState==state_game)
{
if ((localPosition.x>400) || (localPosition.x<0) || (localPosition.y>400) || (localPosition.y<0))
{
showPlayerBoardElems = false;
players[turn].elems.displayNeighbours = false;
} else {
showPlayerBoardElems = true;
players[turn].elems.displayNeighbours = true;
}
}
if (event.type == sf::Event::MouseButtonReleased)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
if (currentState==state_game)
{
resultCommand = players[turn].getElem(localPositionGui);
command(resultCommand);
if (currentNeighbours.find(mousePos) != currentNeighbours.end())
{
if ((!guiSelectBuilding.active) && (showPlayerBoardElems))
{
float hover_x =localPosition.x;
float hover_y = localPosition.y;
if (localPosition.y > 290)
hover_y = hover_y - 100;
if (localPosition.x > 240)
hover_x = hover_x - 150;
if (hover_x>250)
hover_x = 249.0f;
if (hover_x<0)
hover_x = 1.0f;
if (hover_y>300)
hover_y = 299.0f;
if (hover_y<0)
hover_y = 1.0f;
selectedPos = mousePos;
guiSelectBuilding.setPosition(hover_x, hover_y);
guiSelectBuilding.active = true;
sfxClick.play();
currentState = state_gui_elem;
}
break;
}
// nextPlayer();
}
if (currentState==state_gui_elem)
{
resultCommand = guiSelectBuilding.getElem(localPosition);
if (resultCommand.find("elem_")==0)
{
std::string resultCommandWrapped = "build_" + resultCommand;
command(resultCommandWrapped);
} else if (resultCommand.find("close_gui")==0)
{ command(resultCommand);}
}
if (currentState==state_menu)
{
hideMenu();
showGameBoard();
}
+ if (currentState==state_gui_end_round)
+ {
+ resultCommand = guiRoundDice.getElem(localPosition);
+ command(resultCommand);
+
+
+ }
+
+
}
}
if ((localPosition.x>=0) && (localPosition.y>=0) && (localPosition.x<=efc::BOARD_SIZE*efc::TILE_SIZE) && (localPosition.y<=efc::BOARD_SIZE*efc::TILE_SIZE))
{
selector.setPosition((int) (localPosition.x / efc::TILE_SIZE)*efc::TILE_SIZE, ((int) localPosition.y / efc::TILE_SIZE)*efc::TILE_SIZE);
}
}
render();
}
}
+void Game::nextRound() {
+ turn = 0;
+ std::string result = roundDice.drawRound();
+ roundNumber += 1;
+ std::cout << "END OF ROUND " << roundNumber << " " << result << std::endl;
+ command(result);
+}
void Game::nextPlayer(){
players[turn].updatePlayer();
turn++;
if (turn==4)
- turn = 0;
+ {
+ nextRound();
+ }
selector.changeColor(turn);
for (int i=0;i<4;i++)
{
if (i==turn)
{
players[i].setActive(true);
currentNeighbours = players[i].getNeighbours();
}
else
players[i].setActive(false);
}
sfxClick.play();
}
void Game::drawPlayersGui(){
for (int i=0;i<4;i++)
{
window.draw(players[i]);
}
}
void Game::drawSquares() {
if (showPlayerBoardElems)
{
window.draw(selector);
}
}
void Game::update()
{
}
+void Game::drawBaseGame()
+{
+ window.setView(viewTiles);
+ window.draw(map);
+ for (int i=0;i<4;i++)
+ {
+ window.draw(players[i].elems);
+ }
+ drawSquares();
+ window.setView(viewGui);
+ drawPlayersGui();
+ window.setView(viewTiles);
+}
+
void Game::render()
{
window.clear();
if (currentState==state_game)
{
- window.setView(viewTiles);
- window.draw(map);
- for (int i=0;i<4;i++)
- {
- window.draw(players[i].elems);
- }
- drawSquares();
- window.setView(viewGui);
- drawPlayersGui();
- window.setView(viewTiles);
- window.draw(guiSelectBuilding);
+ drawBaseGame();
} else if (currentState==state_gui_elem) {
- window.setView(viewTiles);
- window.draw(map);
- for (int i=0;i<4;i++)
- {
- window.draw(players[i].elems);
- }
- // window.draw(selector);
- window.setView(viewGui);
- drawPlayersGui();
- window.setView(viewTiles);
+ drawBaseGame();
window.draw(guiSelectBuilding);
} else if (currentState==state_menu) {
-window.draw(menuBackground);
+ window.draw(menuBackground);
window.draw(menuTxt);
+ } else if (currentState==state_gui_end_round){
+ drawBaseGame();
+ window.draw(guiRoundDice);
}
window.display();
}
void Game::command(std::string command){
- // std::cout << command << std::endl;
+ std::cout << command << std::endl;
if (command=="close_gui")
{
guiSelectBuilding.active = false;
currentState=state_game;
sfxClick.play();
-
-
-
-
}
if (command=="hide_gui_elem_description")
{
if (currentState==state_gui_elem) {
guiSelectBuilding.descriptionActive = false;
}
}
+ if (command.find("end_of_round")==0)
+ {
+ std::string subResult = command.substr(13);
+ std::cout << "SUB RESULT " << subResult << std::endl;
+ guiRoundDice.active = true;
+ guiRoundDice.setTitle(subResult);
+ currentState = state_gui_end_round;
+ }
+
if (command.find("elem_")==0)
{
if (currentState==state_gui_elem)
{
int buildingType = std::stoi(command.substr(5));
int cashUpd = textures.tilesDescription[buildingType][0];
int foodUpd = textures.tilesDescription[buildingType][2];
int enrgUpd = textures.tilesDescription[buildingType][4];
std::string descTxt = textures.tilesTxt[buildingType];
guiSelectBuilding.setDescriptionTxt("Cash:" + std::to_string(cashUpd) +"\n"+descTxt);
guiSelectBuilding.descriptionActive = true;
}
}
if (command=="end_turn")
nextPlayer();
if (command.find("build_")==0)
{
int buildingType = std::stoi(command.substr(11));
bool purchaseResult = players[turn].addElem(selectedPos, buildingType);
if (purchaseResult)
{
currentState = state_game;
setCurrentNeighbours();
guiSelectBuilding.active = false;
sfxClick.play();
}
}
}
sf::Vector2f Game::getMousePos(){
sf::Vector2i mousePosTmp(sf::Mouse::getPosition(window));
sf::Vector2f mousePosition(window.mapPixelToCoords(mousePosTmp,viewTiles));
return mousePosition;
}
}
diff --git a/game.h b/game.h
index cc5f706..f8077a6 100644
--- a/game.h
+++ b/game.h
@@ -1,103 +1,115 @@
#ifndef GAME_H
#define GAME_H
#include <stdlib.h>
#include <iostream>
#include <time.h> /* time */
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "tilemap.h"
#include "selector.h"
#include "playerhud.h"
#include "textureholder.h"
#include "hover.h"
#include "guichoosebuilding.h"
#include "guiwindow.h"
+#include "rounddice.h"
+#include "guirounddice.h"
namespace efc {
class Game
{
public:
Game();
sf::RenderWindow window;
sf::View viewTiles;
private:
void initBoard();
void loadAssets();
void drawPlayersGui();
void drawSquares();
void drawMenu();
sf::Vector2f getMousePos();
enum states {
state_init,
state_menu,
state_game,
state_gui_elem,
state_select_building,
+ state_gui_end_round,
state_quit
};
states currentState;
sf::Texture textureTiles;
sf::Texture textureFaces;
sf::Font gameFont;
sf::Font menuFont;
sf::Text menuTxt;
std::string gameTitle;
TileMap map;
PlayerHud players[4];
int mapSize;
int level[256];
int levelElems[256];
TextureHolder textures;
std::set<int> currentNeighbours;
void command(std::string command);
int selectedPos;
int turn;
void update();
void render();
sf::View viewGui;
Selector selector;
sf::View viewFull;
+ RoundDice roundDice;
+ int roundNumber;
+
+
GuiChooseBuilding guiSelectBuilding;
+ GuiRoundDice guiRoundDice;
void setCurrentNeighbours ();
void nextPlayer();
+ void nextRound();
+
sf::Sprite menuBackground;
sf::Music musicGame;
sf::Music musicBackground;
sf::Music musicMenu;
sf::SoundBuffer sfxClickBuffer;
sf::Sound sfxClick;
void showMenu();
void hideMenu();
void showGameBoard();
void hideGameBoard();
- bool showPlayerBoardElems;
+
+ bool showPlayerBoardElems;
+ void drawBaseGame();
};
}
#endif // GAME_H
diff --git a/guichoosebuilding.h b/guichoosebuilding.h
index d702a59..199f3f4 100644
--- a/guichoosebuilding.h
+++ b/guichoosebuilding.h
@@ -1,22 +1,21 @@
#ifndef GUICHOOSEBUILDING_H
#define GUICHOOSEBUILDING_H
#include "guiwindow.h"
class GuiChooseBuilding : public GuiWindow
{
public:
GuiChooseBuilding(TextureHolder *textures);
std::map<std::string, sf::Sprite> buildings;
void addBuilding(std::string name, int pos);
const sf::Texture &textureTiles;
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
std::string getElem(sf::Vector2f mousePosition);
- std::string description;
sf::Text guiElemTxt;
void updateDescription();
void setDescriptionTxt(std::string newDescription);
bool descriptionActive;
};
#endif // GUICHOOSEBUILDING_H
diff --git a/guirounddice.cpp b/guirounddice.cpp
new file mode 100644
index 0000000..9e38d0f
--- /dev/null
+++ b/guirounddice.cpp
@@ -0,0 +1,6 @@
+#include "guirounddice.h"
+
+GuiRoundDice::GuiRoundDice(TextureHolder *textures) : GuiWindow(textures)
+
+{
+}
diff --git a/guirounddice.h b/guirounddice.h
new file mode 100644
index 0000000..4f06243
--- /dev/null
+++ b/guirounddice.h
@@ -0,0 +1,11 @@
+#ifndef GUIROUNDDICE_H
+#define GUIROUNDDICE_H
+#include "guiwindow.h"
+
+class GuiRoundDice : public GuiWindow
+{
+public:
+ GuiRoundDice(TextureHolder *textures);
+};
+
+#endif // GUIROUNDDICE_H
diff --git a/guiwindow.cpp b/guiwindow.cpp
index 3cfaea0..b7caa09 100644
--- a/guiwindow.cpp
+++ b/guiwindow.cpp
@@ -1,70 +1,76 @@
#include "guiwindow.h"
+void GuiWindow::setTitle(std::string newTitle) {
+ title = newTitle;
+ guiTitleTxt.setString(title);
+}
+
GuiWindow::GuiWindow(TextureHolder *textures)
{
spriteClose.setTexture(textures->textureGui);
spriteClose.setTextureRect(sf::IntRect(0,0,16,16));
spriteClose.move(sf::Vector2f(150-16,0));
if (!guiElemFont.loadFromFile("assets/fnt/8bitOperatorPlus-Regular.ttf"))
{
std::exit(1);
};
title = "Choose building:";
guiTitleTxt.setFont(guiElemFont);
guiTitleTxt.setCharacterSize(10);
guiTitleTxt.setColor(sf::Color(0,0,0));
guiTitleTxt.move(2,0);
guiTitleTxt.setString(title);
+ setPosition(150, 100);
}
GuiWindow::GuiWindow(){
if (!guiElemFont.loadFromFile("assets/fnt/8bitOperatorPlus-Regular.ttf"))
{
std::exit(1);
};
+ setPosition(150, 100);
}
void GuiWindow::setTextureHolder(TextureHolder *textures)
{
spriteClose.setTexture(textures->textureGui);
}
void GuiWindow::init(){
}
void GuiWindow::draw(sf::RenderTarget& target, sf::RenderStates states) const{
sf::RenderStates states2 = states;
states2.transform *= getTransform();
Hover::draw(target, states);
- if (active)
- {
+
target.draw(spriteClose, states2);
target.draw(guiTitleTxt, states2);
- }
+
}
std::string GuiWindow::getElem(sf::Vector2f mousePosition)
{
sf::Vector2f hoverPos = getPosition();
sf::FloatRect spriteBounds = spriteClose.getLocalBounds();
sf::FloatRect closeRect;
closeRect.left = spriteClose.getPosition().x;
closeRect.top = spriteClose.getPosition().y;
closeRect.width = spriteBounds.width;
closeRect.height = spriteBounds.height;
if (closeRect.contains(mousePosition.x - hoverPos.x,mousePosition.y - hoverPos.y))
{
return "close_gui";
}
else
return "";
}
diff --git a/guiwindow.h b/guiwindow.h
index f498dc2..ebf7889 100644
--- a/guiwindow.h
+++ b/guiwindow.h
@@ -1,29 +1,32 @@
#ifndef GUIWINDOW_H
#define GUIWINDOW_H
#include <iostream>
#include "hover.h"
#include "textureholder.h"
class GuiWindow: public Hover
{
public:
GuiWindow(TextureHolder *textures);
GuiWindow();
void setTextureHolder(TextureHolder *textures);
- sf::Texture* textureGui;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
void init();
virtual std::string getElem(sf::Vector2f mousePosition);
sf::Font guiElemFont;
std::string title;
+ std::string description;
sf::Text guiTitleTxt;
+ sf::Texture* textureGui;
-
+ void setTitle(std::string newTitle);
private:
sf::Sprite spriteClose;
+
+
};
#endif // GUIWINDOW_H
diff --git a/rounddice.cpp b/rounddice.cpp
new file mode 100644
index 0000000..4577b1f
--- /dev/null
+++ b/rounddice.cpp
@@ -0,0 +1,38 @@
+#include "rounddice.h"
+
+RoundDice::RoundDice(PlayerHud (&players)[4])
+{
+ playersHud = players;
+ diceResult = 0;
+}
+
+std::string RoundDice::drawRound(){
+ throwDice();
+ if (diceResult<33)
+ {
+ return "end_of_round_extra_grow";
+ } else if (diceResult<66)
+ {
+ return "end_of_round_extra_energy";
+ } else
+ {
+ eventExtraCash();
+ return "end_of_round_extra_cash";
+ }
+ return "end_of_round_"+std::to_string(diceResult);
+}
+
+int RoundDice::throwDice(){
+ int result = random()%100;
+ diceResult = result;
+ return result;
+}
+
+void RoundDice::eventExtraCash(){
+
+ for (int i=0;i<4;i++)
+ {
+ playersHud[i].cash += 20;
+ playersHud[i].updateTxt();
+ }
+}
diff --git a/rounddice.h b/rounddice.h
new file mode 100644
index 0000000..cb9de66
--- /dev/null
+++ b/rounddice.h
@@ -0,0 +1,24 @@
+#ifndef ROUNDDICE_H
+#define ROUNDDICE_H
+#include <random>
+#include "playerhud.h"
+
+
+class RoundDice
+{
+public:
+ RoundDice(PlayerHud (&players)[4]);
+ PlayerHud *playersHud;
+
+
+ std::string drawRound();
+ int diceResult;
+
+private:
+ int throwDice();
+ void eventExtraCash();
+
+
+};
+
+#endif // ROUNDDICE_H

File Metadata

Mime Type
text/x-diff
Expires
Wed, Feb 4, 2:09 PM (2 h, 4 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55608
Default Alt Text
(22 KB)

Event Timeline