Utworzenie klasy gry
This commit is contained in:
@@ -37,6 +37,9 @@ public:
|
||||
// Rysuj planszę wraz z pionkami
|
||||
void draw(sf::RenderWindow& window);
|
||||
|
||||
// Zaznacz pionek o zadanej pozycji
|
||||
Pawn* selectPawn(Vector position);
|
||||
|
||||
// Destruktor
|
||||
~Board() {}
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
/* Plik zawiera definicje stałych globalnych oraz przedefiniowanie istniejących typów. */
|
||||
|
||||
// Tytuł gry (widoczny m.in. na pasku z tytułem okna)
|
||||
//const char* GAME_TITLE = "Warcaby";
|
||||
|
||||
// Ilość pól w każdym wymiarze
|
||||
const int TILES_COUNT = 10;
|
||||
|
||||
|
||||
70
inc/game.hh
Normal file
70
inc/game.hh
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef GAME_HH
|
||||
#define GAME_HH
|
||||
|
||||
/* Plik implementuje klasę gry, w której znajduje się m.in. główna pętla gry oraz która determinuje całą rozgrywkę. */
|
||||
|
||||
#include <SFML/Window.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "def.hh"
|
||||
#include "misc.hh"
|
||||
#include "object.hh"
|
||||
#include "pawn.hh"
|
||||
#include "board.hh"
|
||||
|
||||
// Typ wyliczeniowy rozróżniający gracza od sztucznej inteligencji
|
||||
enum Player
|
||||
{
|
||||
PL_HUMAN,
|
||||
PL_AI
|
||||
};
|
||||
|
||||
// Klasa gry
|
||||
class Game
|
||||
{
|
||||
private:
|
||||
// Okno naszego programu
|
||||
sf::RenderWindow window;
|
||||
|
||||
// Plansza do gry w warcaby
|
||||
Board board;
|
||||
|
||||
// Zaznaczony pionek
|
||||
Pawn* selected; // może lepiej Vector???
|
||||
|
||||
// Kolor gracza
|
||||
Color player_color;
|
||||
|
||||
// Tura(czy gracz, czy komputer)
|
||||
|
||||
// punkty gracza i komputera
|
||||
|
||||
// stan gry
|
||||
|
||||
public:
|
||||
|
||||
// Konstruktor inicjalizujący parametry gry i wyzwalający pętlę główną gry
|
||||
Game();
|
||||
|
||||
// Zabij pionka (doliczając do tego punkty)
|
||||
void killPawn(Vector position);
|
||||
|
||||
// Do kogo należy pionek
|
||||
Player getPlayer(Vector position);
|
||||
|
||||
// Zwróć kolor gracza
|
||||
Color getPlayerColor() const { return player_color; }
|
||||
|
||||
// Zwróć kolor przeciwnika
|
||||
Color getAIColor() const { return (player_color==CL_WHITE)?CL_BLACK:CL_WHITE; }
|
||||
|
||||
// Przesuń pionek na określoną pozycję uwzględniając już
|
||||
// przy tym zasady gry w warcaby
|
||||
bool movePawn(Vector position, Vector target);
|
||||
|
||||
// Główna pętla gry
|
||||
void loop();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -90,3 +90,24 @@ void Board::draw(sf::RenderWindow& window)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Pawn* Board::selectPawn(Vector position)
|
||||
{
|
||||
// Jeżeli pionek na tym polu istnieje
|
||||
if(board[position.x][position.y])
|
||||
{
|
||||
// Odznaczamy wszystkie pionki
|
||||
for(int x = 0; x<TILES_COUNT; ++x)
|
||||
for(int y = 0; y<TILES_COUNT; ++y)
|
||||
if(board[x][y]) board[x][y]->deselect();
|
||||
|
||||
// Zaznaczamy właściwy
|
||||
board[position.x][position.y]->select();
|
||||
|
||||
// Zwracamy zaznaczony
|
||||
return board[position.x][position.y];
|
||||
}
|
||||
|
||||
// Zwracamy NULL, bo pionek nie istnieje
|
||||
return NULL;
|
||||
}
|
||||
|
||||
32
src/game.cpp
Normal file
32
src/game.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "../inc/game.hh"
|
||||
|
||||
Game::Game()
|
||||
:
|
||||
window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "Warcaby", sf::Style::Close),
|
||||
selected(NULL),
|
||||
player_color(CL_WHITE)
|
||||
{
|
||||
// Kod inicjalizujący
|
||||
// Pętla główna
|
||||
loop();
|
||||
}
|
||||
|
||||
void Game::killPawn(Vector position)
|
||||
{
|
||||
}
|
||||
|
||||
Player Game::getPlayer(Vector position)
|
||||
{
|
||||
//
|
||||
return PL_HUMAN;
|
||||
}
|
||||
|
||||
bool Game::movePawn(Vector position, Vector target)
|
||||
{
|
||||
//
|
||||
return true;
|
||||
}
|
||||
|
||||
void Game::loop()
|
||||
{
|
||||
}
|
||||
15
src/main.cpp
15
src/main.cpp
@@ -6,23 +6,34 @@
|
||||
#include "../inc/object.hh"
|
||||
#include "../inc/pawn.hh"
|
||||
#include "../inc/board.hh"
|
||||
#include "../inc/game.hh"
|
||||
|
||||
int main()
|
||||
{
|
||||
sf::ContextSettings settings;
|
||||
/* sf::ContextSettings settings;
|
||||
settings.antialiasingLevel = 8;
|
||||
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "Warcaby", sf::Style::Close, settings);
|
||||
Board board;
|
||||
Pawn* selected;
|
||||
while(window.isOpen())
|
||||
{
|
||||
sf::Event event;
|
||||
while(window.pollEvent(event))
|
||||
{
|
||||
if(event.type == sf::Event::Closed) window.close();
|
||||
if(event.type == sf::Event::MouseButtonPressed)
|
||||
{
|
||||
if(event.mouseButton.button == sf::Mouse::Left)
|
||||
{
|
||||
Pawn* tmp = board.selectPawn(Vector(sf::Vector2f(event.mouseButton.x, event.mouseButton.y)));
|
||||
if(tmp) selected = tmp;
|
||||
else board.movePawn(selected->getPosition(), Vector(sf::Vector2f(event.mouseButton.x, event.mouseButton.y)));
|
||||
}
|
||||
}
|
||||
}
|
||||
window.clear(sf::Color(255, 255, 255));
|
||||
board.draw(window);
|
||||
window.display();
|
||||
}
|
||||
}*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user