Page MenuHomePhabricator (Chris)

No OneTemporary

Size
18 KB
Referenced Files
None
Subscribers
None
diff --git a/boardelem.cpp b/boardelem.cpp
index 2b3a50e..bb44141 100644
--- a/boardelem.cpp
+++ b/boardelem.cpp
@@ -1,50 +1,50 @@
#include "boardelem.h"
namespace efc {
sf::RectangleShape createNeighbour(int pos) {
sf::RectangleShape rectangle(sf::Vector2f(efc::TILE_SIZE, efc::TILE_SIZE));
sf::Vector2i cords(efc::transPosition(pos));
rectangle.setFillColor(sf::Color(240, 240, 240,168));
rectangle.setOutlineColor(sf::Color(24,24,24));
rectangle.setPosition(cords.x*efc::TILE_SIZE, cords.y*efc::TILE_SIZE);
rectangle.setOutlineThickness(1);
return rectangle;
}
}
-efc::BoardElem::BoardElem(TextureHolder *textures,int pos, int type, const sf::Texture *textureElem)
+efc::BoardElem::BoardElem(TextureHolder *textures,int pos, int type)
{
textureTiles = &textures->textureTiles;
this->pos = pos;
this->type = type;
sf::Vector2i cords = efc::transPosition(pos);
int y_type = (int) type / efc::TILE_BOARD_SIZE;
int x_type = type % efc::TILE_BOARD_SIZE;
setPosition(cords.x*efc::TILE_SIZE,cords.y*efc::TILE_SIZE);
setTexture(textures->textureTiles);
setTextureRect(sf::IntRect(x_type*efc::TILE_SIZE, y_type*efc::TILE_SIZE, efc::TILE_SIZE, efc::TILE_SIZE));
}
diff --git a/boardelem.h b/boardelem.h
index fbb05d7..e09e7b9 100644
--- a/boardelem.h
+++ b/boardelem.h
@@ -1,35 +1,35 @@
#ifndef BOARDELEM_H
#define BOARDELEM_H
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "tilemap.h"
#include "textureholder.h"
namespace efc {
sf::RectangleShape createNeighbour(int pos);
class BoardElem: public sf::Sprite
{
public:
BoardElem();
- BoardElem(TextureHolder *textures,int pos, int type, const sf::Texture *textureElem);
+ BoardElem(TextureHolder *textures,int pos, int type);
int pos;
int type;
sf::Texture* textureTiles;
private:
const sf::Texture* textureElem;
static const sf::Texture& textureElem2;
TextureHolder *textures;
TextureHolder myHolder;
};
}
#endif // BOARDELEM_H
diff --git a/boardelems.cpp b/boardelems.cpp
index 9d9bb22..b6fefc3 100644
--- a/boardelems.cpp
+++ b/boardelems.cpp
@@ -1,32 +1,36 @@
#include "boardelems.h"
BoardElems::BoardElems()
{
active = false;
}
void BoardElems::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
- for (efc::BoardElem i: items)
- {
- target.draw(i, states);
+
+ for (const efc::BoardElem &i: items)
+ {
+ target.draw(i, states);
std::vector<int> neighbours(efc::getNeighbours(i.pos));
if (active==true)
{
for (int j: neighbours)
{
- sf::RectangleShape sprite(efc::createNeighbour(j));
- target.draw(sprite, states);
+ if (items_map.count(j)==0)
+ {
+ sf::RectangleShape sprite(efc::createNeighbour(j));
+ target.draw(sprite, states);
+ }
}
}
// std::cout << "POS: "<< i.getPosition().x << " " << i.getPosition().y << std::endl;
}
}
diff --git a/game.cpp b/game.cpp
index c6633df..40d79a6 100644
--- a/game.cpp
+++ b/game.cpp
@@ -1,257 +1,275 @@
#include <stdlib.h>
#include <iostream>
#include "game.h"
#include "guiwindow.h"
#include <time.h> /* time */
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);
- currentNeighbours = players[0].getNeighbours();
+ 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);
}
}
Game::Game():
window(sf::VideoMode(512, 400), "EnFuCraft"),
viewTiles(sf::FloatRect(00, 00, 400, 400)),
+ viewGui(sf::FloatRect(00, 00, 112, 400)),
+ selector(efc::TILE_SIZE),
+ guiSelectBuilding(&textures),
turn(0)
+
{
+ window.setVerticalSyncEnabled(true);
Hover hover;
GuiWindow guiWindow(&textures);
- GuiChooseBuilding guiSelectBuilding(&textures);
currentState = state_init;
std::srand (time(NULL));
loadAssets();
- sf::View viewGui(sf::FloatRect(00, 00, 112, 400));
viewGui.setViewport(sf::FloatRect(0.8f,0, 1.0f, 1.0f));
- sf::View viewTiles(sf::FloatRect(00, 00, 400, 400));
viewTiles.setViewport(sf::FloatRect(0,0, 0.8f, 1.0f));
- Selector selector(efc::TILE_SIZE);
selector.changeColor(turn); //This is only for the test TODO: remove
initBoard();
currentState = state_game;
// run the main loop
while (window.isOpen())
{
// handle events
sf::Event event;
sf::Vector2i localPositionTmp = sf::Mouse::getPosition(window);
sf::Vector2f localPosition = window.mapPixelToCoords(localPositionTmp,viewTiles);
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;
while (window.pollEvent(event))
{
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 (currentNeighbours.find(mousePos) != currentNeighbours.end())
{
}
if (event.type == sf::Event::MouseButtonReleased)
{
sf::Vector2i mTP = sf::Mouse::getPosition(window);
if (event.mouseButton.button == sf::Mouse::Left)
{
if (currentState==state_game)
{
if (currentNeighbours.find(mousePos) != currentNeighbours.end())
{
std::cout << "SUPER" << std::endl;
if (!guiSelectBuilding.active)
{
selectedPos = mousePos;
guiSelectBuilding.setPosition(hover_x, hover_y);
guiSelectBuilding.active = true;
currentState = state_gui_elem;
}
break;
}
turn++;
if (turn==4)
turn = 0;
selector.changeColor(turn);
for (int i=0;i<4;i++)
{
if (i==turn)
{
players[i].setActive(true);
if (currentNeighbours.find(mousePos) != currentNeighbours.end())
{
- std::cout << "SUPER" << std::endl;
+// std::cout << "SUPER" << std::endl;
}
currentNeighbours = players[i].getNeighbours();
}
else
players[i].setActive(false);
}
}
if (currentState==state_gui_elem)
{
std::string result = guiSelectBuilding.getElem(localPosition);
command(result);
}
}
}
}
+
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);
}
- window.clear();
-
- if ((currentState==state_game) || (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);
- window.draw(guiSelectBuilding);
- window.display();
+ render();
}
}
void Game::drawPlayersGui(){
for (int i=0;i<4;i++)
{
window.draw(players[i]);
}
}
+
+void Game::update()
+{
+
+}
+
+void Game::render()
+{
+ window.clear();
+ if ((currentState==state_game) || (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);
+ window.draw(guiSelectBuilding);
+ window.display();
+}
+
void Game::command(std::string command){
- std::cout << "RUNNING:" + command << std::endl;
+// std::cout << "RUNNING:" + command << std::endl;
if (command=="close")
currentState=state_game;
if (command.find("build_")==0)
{
int buildingType = std::stoi(command.substr(6));
- std::cout << "building:" + buildingType << " at " << selectedPos << std::endl;
players[turn].addElem(selectedPos, buildingType);
- currentState=state_game;
+ currentState = state_game;
+ setCurrentNeighbours();
}
}
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 0c7941b..c4ad3a8 100644
--- a/game.h
+++ b/game.h
@@ -1,55 +1,65 @@
#ifndef GAME_H
#define GAME_H
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "tilemap.h"
#include "selector.h"
#include "playerhud.h"
#include "textureholder.h"
#include "hover.h"
#include "guichoosebuilding.h"
namespace efc {
class Game
{
public:
Game();
sf::RenderWindow window;
sf::View viewTiles;
private:
void initBoard();
void loadAssets();
void drawPlayersGui();
sf::Vector2f getMousePos();
enum states {
state_init,
state_menu,
state_game,
state_gui_elem,
state_select_building,
state_quit
};
states currentState;
sf::Texture textureTiles;
sf::Texture textureFaces;
sf::Font gameFont;
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;
+ GuiChooseBuilding guiSelectBuilding;
+ void setCurrentNeighbours ();
+
+
+
+
};
}
#endif // GAME_H
diff --git a/main.cpp b/main.cpp
index 425a908..3bfd19e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,14 +1,10 @@
#include <iostream>
#include "game.h"
using namespace std;
int main()
{
- cout << "Hello World!" << endl;
-
efc::Game game;
return 0;
-
-
}
diff --git a/playerhud.cpp b/playerhud.cpp
index 13eed61..27f18d5 100644
--- a/playerhud.cpp
+++ b/playerhud.cpp
@@ -1,135 +1,142 @@
#include "playerhud.h"
#include "textureholder.h"
#include "boardelem.h"
void PlayerHud::addElem(int pos, int type) {
- efc::BoardElem startElem(textures, pos,type, &this->textures->textureTiles);
+ efc::BoardElem startElem(textures, pos,type);
elems.items.push_back(startElem);
elems.items_map.insert({pos, startElem});
}
std::set<int> PlayerHud::getNeighbours(){
std::set<int> neighbours;
for (std::pair<int, efc::BoardElem> i: elems.items_map)
{
std::cout << "adding " << i.first << std::endl;
std::vector<int> neighboursVector(efc::getNeighbours(i.second.pos));
for (int j: neighboursVector)
{
- neighbours.insert(j);
+
+ if (elems.items_map.count(j) == 0)
+ {
+ neighbours.insert(j);
+ }
+
+
+
}
}
return neighbours;
}
PlayerHud::PlayerHud()
{
active = false;
}
void PlayerHud::setActive(bool newState){
active = newState;
elems.active = newState;
}
PlayerHud::PlayerHud(TextureHolder *textures, int faceNumber, sf::Font *gameFont, int faceSize, int pos)
{
active = false;
int startPositions[4] = {0,15,240, 255};
this->textures = textures;
- efc::BoardElem startElem(textures, startPositions[pos],441, &this->textures->textureTiles);
+ efc::BoardElem startElem(textures, startPositions[pos],441);
elems.items.push_back(startElem);
elems.items_map.insert({pos, startElem});
this->faceSize = faceSize;
spriteFace.setTexture(textures->textureFaces);
this->pos = pos;
food = 0;
cash = 0;
energy = 0;
int x = faceNumber % 10;
int y = (int) faceNumber /10;
txtCash.setFont(*gameFont);
txtFood.setFont(*gameFont);
txtEnergy.setFont(*gameFont);
txtCash.setPosition(1,(pos*100)+40);
txtCash.setString("Cash: " + std::to_string(cash));
txtCash.setCharacterSize(10);
txtCash.setScale(sf::Vector2f(0.25f, 1.f));
txtFood.setPosition(1,(pos*100)+55);
txtFood.setString("Food: " + std::to_string(food));
txtFood.setCharacterSize(10);
txtFood.setScale(sf::Vector2f(0.25f, 1.f));
txtEnergy.setPosition(1,(pos*100)+70);
txtEnergy.setString("Enrg: " + std::to_string(energy));
txtEnergy.setString("Enrg: " + std::to_string(energy));
txtEnergy.setCharacterSize(10);
txtEnergy.setScale(sf::Vector2f(0.25f, 1.f));
std::cout << "playerHud" << faceNumber << " " << x << " " << y << std::endl;
spriteFace.setTextureRect(sf::IntRect(x*faceSize, y*faceSize, faceSize, faceSize));
spriteFace.setScale(sf::Vector2f(0.25f, 1.f));
spriteFace.setPosition(0,pos*100);
}
void PlayerHud::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// // apply the transform
states.transform *= getTransform();
// Color rectangles making the gui on the right side
sf::RectangleShape rectangle(sf::Vector2f(faceSize, faceSize));
sf::RectangleShape rectangle2(sf::Vector2f(faceSize, (faceSize*2)+3));
if (pos==0)
{
rectangle.setFillColor(sf::Color(50, 50, 150,168));
rectangle2.setFillColor(sf::Color(100, 100, 150,168));
rectangle.setOutlineColor(sf::Color(0,0,128));
}
else if (pos==1)
{
rectangle.setFillColor(sf::Color(50, 150, 50,168));
rectangle2.setFillColor(sf::Color(100, 150,100,168));
rectangle.setOutlineColor(sf::Color(0,128,0));
}
else if (pos==2)
{
rectangle.setFillColor(sf::Color(150, 50, 50,168));
rectangle2.setFillColor(sf::Color(150, 100,100,168));
rectangle.setOutlineColor(sf::Color(128,0,0));
}
else if (pos==3)
{
rectangle.setFillColor(sf::Color(150, 150, 150,168));
rectangle2.setFillColor(sf::Color(200, 200,200,168));
rectangle.setOutlineColor(sf::Color(128,128,128));
}
rectangle.setOutlineThickness(1);
rectangle.setPosition(0, pos*100);
rectangle2.setPosition(0, (pos*100)+faceSize+1);
target.draw(rectangle, states);
target.draw(rectangle2, states);
target.draw(txtCash, states);
target.draw(txtFood, states);
target.draw(txtEnergy, states);
target.draw(spriteFace, states);
}
diff --git a/tilemap.h b/tilemap.h
index 692cc77..9285f22 100644
--- a/tilemap.h
+++ b/tilemap.h
@@ -1,38 +1,38 @@
-#ifndef TILEMAP_H
+ #ifndef TILEMAP_H
#define TILEMAP_H
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "textureholder.h"
namespace efc {
enum {
TILE_SIZE = 25,
BOARD_SIZE = 16,
TILE_BOARD_SIZE = 40
};
sf::Vector2i transPosition(int pos);
int transCords(sf::Vector2i cords);
std::vector<int> getNeighbours(int pos);
sf::IntRect transPosIntoRect(int pos);
sf::Vector2i transTilePosition(int pos);
}
class TileMap : public sf::Drawable, public sf::Transformable
{
public:
bool load(TextureHolder *textures, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);
TileMap();
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
sf::VertexArray m_vertices;
sf::Texture *m_tileset;
TextureHolder *textures;
};
#endif // TILEMAP_H

File Metadata

Mime Type
text/x-diff
Expires
Mon, Feb 2, 9:10 PM (1 d, 19 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55584
Default Alt Text
(18 KB)

Event Timeline