Commit b7c127cc authored by Dennis Payne's avatar Dennis Payne

Add initial two player mode.

parent f96cf08f
......@@ -14,26 +14,50 @@
<image-file file = "misc/landscape.png"/>
</image>
<!-- Zone top represents the zone where are the pieces you control -->
<integer name = "zone_top" value = "74" />
<!-- Coords for score displaying -->
<integer name = "score_top" value = "32" />
<integer name = "score_right" value = "112" />
<integer name = "high_score_top" value = "290" />
<integer name = "high_score_right" value ="750" />
<!-- Coords of the game elements -->
<section name = "game">
<!-- Coords of the game area -->
<integer name = "top" value = "112" />
<integer name = "left" value = "17" />
<section name = "player1">
<!-- Zone top represents the zone where are the pieces you control -->
<integer name = "zone_top" value = "74" />
<!-- Coords for score displaying -->
<integer name = "score_top" value = "32" />
<integer name = "score_right" value = "112" />
<integer name = "high_score_top" value = "290" />
<integer name = "high_score_right" value ="750" />
<!-- Coords of the two next pieces -->
<integer name = "next_top" value = "64" />
<integer name = "next_left" value = "122" />
<!-- Coords of the game elements -->
<section name = "game">
<!-- Coords of the game area -->
<integer name = "top" value = "112" />
<integer name = "left" value = "17" />
<!-- Coords of the two next pieces -->
<integer name = "next_top" value = "64" />
<integer name = "next_left" value = "122" />
</section>
</section>
<section name = "player2">
<!-- Zone top represents the zone where are the pieces you control -->
<integer name = "zone_top" value = "74" />
<!-- Coords for score displaying -->
<integer name = "score_top" value = "32" />
<integer name = "score_right" value = "240" />
<integer name = "high_score_top" value = "290" />
<integer name = "high_score_right" value ="750" />
<!-- Coords of the game elements -->
<section name = "game">
<!-- Coords of the game area -->
<integer name = "top" value = "112" />
<integer name = "left" value = "145" />
<!-- Coords of the two next pieces -->
<integer name = "next_top" value = "129" />
<integer name = "next_left" value = "122" />
</section>
</section>
<!-- Font sprites to display score -->
<sprite name="font_glyph">
<image file="misc/font.png">
......
......@@ -17,10 +17,6 @@
<sprite name = "little_color_blind">
<image file = "piece1/normal-cb.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "60" />
</section>
......@@ -36,12 +32,7 @@
<sprite name = "little_color_blind">
<image file = "piece2/normal-cb.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "105" />
</section>
<!-- About the piece 3 -->
......@@ -56,12 +47,7 @@
<sprite name = "little_color_blind">
<image file = "piece3/normal-cb.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "150" />
</section>
<!-- About the piece 4 -->
......@@ -82,10 +68,6 @@
<image file ="piece4/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "195" />
</section>
......@@ -107,10 +89,6 @@
<image file ="piece5/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "240" />
</section>
......@@ -131,10 +109,6 @@
<image file ="piece6/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "285" />
</section>
......@@ -156,10 +130,6 @@
<image file ="piece7/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "330" />
</section>
......@@ -180,10 +150,6 @@
<image file ="piece8/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "375" />
</section>
......@@ -204,10 +170,6 @@
<image file ="piece9/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "420" />
</section>
......@@ -228,10 +190,6 @@
<image file ="piece10/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "465" />
</section>
......@@ -253,10 +211,6 @@
<image file ="piece11/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "510" />
</section>
......@@ -277,10 +231,6 @@
<image file ="piece12/hidder.png" />
</sprite>
<!-- Coords of the sprite in the preview area -->
<integer name = "left" value = "160" />
<integer name = "top" value = "555" />
</section>
</section>
......
......@@ -38,6 +38,7 @@ static CommonResources* p_instance = NULL;
CommonResources::CommonResources()
{
p_current_player = NULL;
player2_support = false;
normal_color.r = 255;
normal_color.g = 255;
normal_color.b = 255;
......@@ -85,7 +86,11 @@ void CommonResources::load_gfx(SDL_Renderer *gc, std::string skin)
pieces_width = CL_Integer_to_int("pieces/width", &gfx_pieces);
pieces_height = CL_Integer_to_int("pieces/height", &gfx_pieces);
player1.load_gfx(gc, skin);
player1.load_gfx(gc, skin, "player1/");
if (gfx.resource_exists("player2/game/top"))
{
player2_support = true;
}
front_layer.load_gfx(gc, skin);
}
......
......@@ -48,8 +48,12 @@ public:
/** Human player */
HumanPlayer player1;
std::unique_ptr<Player> player2;
/** Common player */
Player* p_current_player;
bool player2_support;
/** Front layer */
FrontLayer front_layer;
......
......@@ -277,7 +277,7 @@ void GameEngine::toggle_colorblind()
_p_loading_screen -> set_progression(0.0f);
if(_running)
{
p_common_resources->player1.load_gfx(_window.get_gc(), p_pref -> skin);
p_common_resources->player1.load_gfx(_window.get_gc(), p_pref -> skin, "player1/");
_p_loading_screen -> set_progression(1.0f / 2.0f);
}
if(_running)
......
......@@ -27,6 +27,7 @@
class HumanPlayer : public Player{
public:
HumanPlayer(int player = 1) : Player(player) {}
/** Checking key events */
void events();
......
......@@ -21,6 +21,7 @@
#include "Board.h"
#include "GameEngine.h"
#include "AudioManager.h"
#include <physfs.h>
/************************************************************************/
/* Constants */
......@@ -33,11 +34,11 @@ static const float PIECE_ROTATION_SPEED = 0.45f;
/************************************************************************/
/* Constructor */
/************************************************************************/
Player::Player():
_key_change_angle(SDL_SCANCODE_UP , true),
_key_left (SDL_SCANCODE_LEFT , true),
_key_right (SDL_SCANCODE_RIGHT, true),
_key_falling (SDL_SCANCODE_DOWN , false)
Player::Player(int player):
_key_change_angle(((player == 1) ? SDL_SCANCODE_UP : SDL_SCANCODE_R), true),
_key_left (((player == 1) ? SDL_SCANCODE_LEFT : SDL_SCANCODE_D) , true),
_key_right (((player == 1) ? SDL_SCANCODE_RIGHT : SDL_SCANCODE_G), true),
_key_falling (((player == 1) ? SDL_SCANCODE_DOWN : SDL_SCANCODE_F), false)
{
// Setting objects to NULL
_p_next_piece1 = NULL;
......@@ -188,7 +189,7 @@ void Player::term_game()
/************************************************************************/
/* Load GFX */
/************************************************************************/
void Player::load_gfx(SDL_Renderer *gc, std::string skin)
void Player::load_gfx(SDL_Renderer *gc, std::string skin, std::string basename)
{
unload_gfx();
......@@ -200,6 +201,10 @@ void Player::load_gfx(SDL_Renderer *gc, std::string skin)
CL_ResourceManager gfx_preview_pieces("pieces_preview.xml");
CL_ResourceManager gfx("general.xml");
if ((basename == "player1/") && (!gfx.resource_exists("player1/game/top")))
{
basename = "";
}
// Getting preferences (to know if colorbling is activated)
Preferences* p_pref = pref_get_instance();
......@@ -219,8 +224,8 @@ void Player::load_gfx(SDL_Renderer *gc, std::string skin)
else
_pieces_mini[i-1] = CL_Sprite(gc, "pieces_preview/piece_"+to_string(i)+"/little", &gfx_preview_pieces);
_pieces_preview_x[i-1] = CL_Integer_to_int("pieces_preview/piece_"+to_string(i)+"/left", &gfx_preview_pieces);
_pieces_preview_y[i-1] = CL_Integer_to_int("pieces_preview/piece_"+to_string(i)+"/top", &gfx_preview_pieces);
_pieces_preview_x[i-1] = CL_Integer_to_int("pieces_preview/piece_"+to_string(i)+"/left", &gfx_preview_pieces, -1);
_pieces_preview_y[i-1] = CL_Integer_to_int("pieces_preview/piece_"+to_string(i)+"/top", &gfx_preview_pieces, -1);
if(i>3)
{
......@@ -229,20 +234,20 @@ void Player::load_gfx(SDL_Renderer *gc, std::string skin)
}
// Getting sprites position
_next_left = CL_Integer_to_int("game/next_left", &gfx);
_next_top = CL_Integer_to_int("game/next_top", &gfx);
_next_left = CL_Integer_to_int(basename + "game/next_left", &gfx);
_next_top = CL_Integer_to_int(basename + "game/next_top", &gfx);
// Getting game zone position
_board.game_top = CL_Integer_to_int("game/top", &gfx);
_board.game_left = CL_Integer_to_int("game/left", &gfx);
_board.zone_top = CL_Integer_to_int("zone_top", &gfx);
_board.game_top = CL_Integer_to_int(basename + "game/top", &gfx);
_board.game_left = CL_Integer_to_int(basename + "game/left", &gfx);
_board.zone_top = CL_Integer_to_int(basename + "zone_top", &gfx);
_board.score_top = CL_Integer_to_int("score_top", &gfx);
_board.score_right = CL_Integer_to_int("score_right", &gfx);
_board.bonus_top = CL_Integer_to_int("bonus_score_top", &gfx, -1);
_board.bonus_right = CL_Integer_to_int("bonus_score_right", &gfx, -1);
_board.hightscore_top = CL_Integer_to_int("high_score_top", &gfx);
_board.hightscore_right = CL_Integer_to_int("high_score_right", &gfx);
_board.score_top = CL_Integer_to_int(basename + "score_top", &gfx);
_board.score_right = CL_Integer_to_int(basename + "score_right", &gfx);
_board.bonus_top = CL_Integer_to_int(basename + "bonus_score_top", &gfx, -1);
_board.bonus_right = CL_Integer_to_int(basename + "bonus_score_right", &gfx, -1);
_board.hightscore_top = CL_Integer_to_int(basename + "high_score_top", &gfx);
_board.hightscore_right = CL_Integer_to_int(basename + "high_score_right", &gfx);
// Calculating c² = a²+b³
_current_pieces_r = p_resources->pieces_width/2;
......@@ -310,10 +315,13 @@ void Player::draw(SDL_Renderer *gc)
// Drawing unlocked pieces
for(int i=0; i<NUMBER_OF_PIECES; ++i)
{
if(i >= _board.visible_pieces)
_pieces_hidden[i-3].draw(gc, _pieces_preview_x[i], _pieces_preview_y[i]);
else
_pieces_mini[i].draw(gc, _pieces_preview_x[i], _pieces_preview_y[i]);
if (_pieces_preview_y[i] != -1)
{
if(i >= _board.visible_pieces)
_pieces_hidden[i-3].draw(gc, _pieces_preview_x[i], _pieces_preview_y[i]);
else
_pieces_mini[i].draw(gc, _pieces_preview_x[i], _pieces_preview_y[i]);
}
}
// Drawing board
......
......@@ -36,13 +36,13 @@ class Player{
public:
/** Constructor */
Player();
Player(int player = 1);
/** Destructor */
~Player();
virtual ~Player();
/** Load GFX */
void load_gfx(SDL_Renderer *gc, std::string skin);
void load_gfx(SDL_Renderer *gc, std::string skin, std::string basename);
/** Unload GFX */
void unload_gfx();
......
......@@ -85,6 +85,8 @@ void CommonState::draw(SDL_Renderer *gc)
if(_p_common_resources -> p_current_player)
_p_common_resources -> p_current_player -> draw(gc);
if(_p_common_resources->player2.get())
_p_common_resources->player2->draw(gc);
}
/************************************************************************/
......
......@@ -216,11 +216,14 @@ void GameOverState::action_performed(int selection, ActionType action_type)
_p_common_resources -> p_engine -> stop_current_state();
_p_common_resources -> p_engine -> set_state_ingame();
_p_common_resources -> player1.new_game();
if (_p_common_resources->player2.get())
_p_common_resources -> player2->new_game();
}
else
{
_p_common_resources -> p_engine -> stop_current_state();
_p_common_resources -> p_engine -> set_state_title();
_p_common_resources -> player2 = NULL;
}
break;
default:
......
......@@ -76,6 +76,8 @@ void InGameState::draw(SDL_Renderer *gc)
void InGameState::update(SDL_Renderer *gc)
{
_p_common_resources -> player1.update();
if (_p_common_resources->player2.get())
_p_common_resources -> player2->update();
}
/************************************************************************/
......@@ -84,6 +86,8 @@ void InGameState::update(SDL_Renderer *gc)
void InGameState::events(GameWindow& window)
{
_p_common_resources -> player1.events();
if (_p_common_resources->player2.get())
_p_common_resources->player2->events();
if(_p_common_resources -> key.escape.get() || _p_common_resources ->key.pause.get())
{
......
......@@ -102,6 +102,7 @@ void QuitMenuState::action_performed(int selection, ActionType action_type)
case QUITMENU_RETRY:
_p_common_resources -> player1.new_game();
_p_common_resources -> p_engine -> stop_current_state();
_p_common_resources -> player2 = NULL;
break;
}
}
......
......@@ -81,7 +81,7 @@ void TitleState::load_gfx(SDL_Renderer *gc, std::string skin)
_keyup_y = 50;
_keyright_x = 150;
_demo_player.load_gfx(gc, skin);
_demo_player.load_gfx(gc, skin, "player1/");
}
/************************************************************************/
......@@ -279,11 +279,19 @@ void TitleState::update(SDL_Renderer *gc)
void TitleState::events(GameWindow& window)
{
const Uint8 *state = SDL_GetKeyboardState(NULL);
if(_p_common_resources -> key.enter.get() || state[SDL_SCANCODE_SPACE])
if(_p_common_resources -> key.enter.get() || state[SDL_SCANCODE_SPACE] || state[SDL_SCANCODE_1])
{
_p_common_resources -> p_engine -> set_state_ingame();
_p_common_resources -> player1.new_game();
}
if(_p_common_resources->player2_support && state[SDL_SCANCODE_2])
{
_p_common_resources -> p_engine -> set_state_ingame();
_p_common_resources -> player1.new_game();
_p_common_resources->player2 = std::unique_ptr<Player>(new HumanPlayer(2));
_p_common_resources->player2->load_gfx(_p_common_resources->p_gc, _p_common_resources->skin, "player2/");
_p_common_resources->player2->new_game();
}
if(_p_common_resources-> key.escape.get() || _p_common_resources -> key.pause.get())
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment