Only allow player to shoot if player does not have any projectiles pending

This commit is contained in:
xethm55 2012-10-06 10:57:05 -04:00
parent 3e22ad70e1
commit cf3f64fed9
4 changed files with 58 additions and 24 deletions

View File

@ -125,11 +125,20 @@ void Client::run(bool fullscreen, int width, int height, std::string pname)
case sf::Event::MouseButtonPressed: case sf::Event::MouseButtonPressed:
if((event.mouseButton.button == sf::Mouse::Left) && if((event.mouseButton.button == sf::Mouse::Left) &&
(m_shot_fired == false) && // Don't allow shots ontop of each other (m_shot_fired == false) && // Don't allow shots ontop of each other
// The server needs to allow player to shoot, so that
// multiple shots cannot be fired at the same time
(m_players[m_current_player]->m_shot_allowed) &&
(m_client_has_focus)) (m_client_has_focus))
{
m_left_button_pressed = true; m_left_button_pressed = true;
}
break; break;
case sf::Event::MouseButtonReleased: case sf::Event::MouseButtonReleased:
if((event.mouseButton.button == sf::Mouse::Left) && if((event.mouseButton.button == sf::Mouse::Left) &&
// Prevents a shot from being fired upon release
// while another shot is currently being fired.
(m_players[m_current_player]->m_shot_allowed) &&
(m_left_button_pressed) &&
(m_client_has_focus)) (m_client_has_focus))
{ {
m_drawing_shot = false; m_drawing_shot = false;
@ -253,12 +262,24 @@ void Client::update(double elapsed_time)
{ {
float x; float x;
float y; float y;
sf::Uint8 pindex;
client_packet >> x; client_packet >> x;
client_packet >> y; client_packet >> y;
client_packet >> pindex;
// Damage the tile if it exists
if((!m_map.get_tile_at(x, y).isNull())) if((!m_map.get_tile_at(x, y).isNull()))
{ {
m_map.get_tile_at(x, y)->shot(); m_map.get_tile_at(x, y)->shot();
} }
// Allow player to shoot again
if(m_players.end() != m_players.find(pindex))
{
m_players[pindex]->m_shot_allowed = true;
if(pindex == m_current_player)
{
m_shot_fired = false;
}
}
break; break;
} }
@ -360,9 +381,9 @@ void Client::update(double elapsed_time)
client_packet << packet_type; client_packet << packet_type;
client_packet << m_current_player; client_packet << m_current_player;
client_packet << (float)(m_drawing_shot_distance + SHOT_RING_WIDTH / 2.0); client_packet << (float)(m_drawing_shot_distance + SHOT_RING_WIDTH / 2.0);
m_net_client->sendData(client_packet, true); m_net_client->sendData(client_packet, true);
m_shot_fired = false;
m_drawing_shot_distance = 0; m_drawing_shot_distance = 0;
m_players[m_current_player]->m_shot_allowed = false;
} }
} }
else if(!registered_player) else if(!registered_player)

View File

@ -20,5 +20,6 @@ Player::Player()
m_shot_start_x = 0.0; m_shot_start_x = 0.0;
m_shot_start_y = 0.0; m_shot_start_y = 0.0;
m_shot_direction = 0.0; m_shot_direction = 0.0;
m_shot_allowed = true;
} }

View File

@ -26,7 +26,7 @@ class Player
double m_shot_start_y; double m_shot_start_y;
double m_shot_direction; double m_shot_direction;
double m_shot_start_time; double m_shot_start_time;
bool m_shot_allowed;
Player(); Player();
}; };

View File

@ -156,14 +156,20 @@ void Server::update( double elapsed_time )
float shot_distance; float shot_distance;
server_packet >> pindex; server_packet >> pindex;
server_packet >> shot_distance; server_packet >> shot_distance;
m_players[pindex]->m_shot_distance = shot_distance; // start the shot process if a player is allowed to shoot and exits
m_players[pindex]->m_shot_start_time = Timer::GetTimeDouble(); if((m_players.end() != m_players.find(pindex)) &&
(m_players[pindex]->m_shot_allowed))
// Need to store off the current player location so that the {
// correct tile can be calculated later. m_players[pindex]->m_shot_distance = shot_distance;
m_players[pindex]->m_shot_start_x = m_players[pindex]->x; m_players[pindex]->m_shot_start_time = Timer::GetTimeDouble();
m_players[pindex]->m_shot_start_y = m_players[pindex]->y; m_players[pindex]->m_shot_allowed = false;
m_players[pindex]->m_shot_direction = m_players[pindex]->direction;
// Need to store off the current player location so that the
// correct tile can be calculated later.
m_players[pindex]->m_shot_start_x = m_players[pindex]->x;
m_players[pindex]->m_shot_start_y = m_players[pindex]->y;
m_players[pindex]->m_shot_direction = m_players[pindex]->direction;
}
break; break;
} }
@ -209,21 +215,27 @@ void Server::update( double elapsed_time )
float tile_x = m_players[pindex]->m_shot_start_x + (player_dir_x * m_players[pindex]->m_shot_distance); float tile_x = m_players[pindex]->m_shot_start_x + (player_dir_x * m_players[pindex]->m_shot_distance);
float tile_y = m_players[pindex]->m_shot_start_y + (player_dir_y * m_players[pindex]->m_shot_distance); float tile_y = m_players[pindex]->m_shot_start_y + (player_dir_y * m_players[pindex]->m_shot_distance);
refptr<HexTile> p_tile = m_map.get_tile_at(tile_x, tile_y); refptr<HexTile> p_tile = m_map.get_tile_at(tile_x, tile_y);
if((!p_tile.isNull()) && // Send a message to all clients letting them know a tile was damaged
(p_tile->get_damage_state() < HexTile::DESTROYED)) // always send message since it will reenable the player shot ability.
{ sf::Uint8 ptype = TILE_DAMAGED;
// Send a message to all clients letting them know a tile was damaged server_packet.clear();
sf::Uint8 ptype = TILE_DAMAGED; server_packet << ptype;
server_packet << tile_x;
server_packet.clear(); server_packet << tile_y;
server_packet << ptype; server_packet << pindex; // Needed to alert the client that the player can now shoot again.
server_packet << p_tile->get_x(); m_net_server->sendData(server_packet, true);
server_packet << p_tile->get_y();
m_net_server->sendData(server_packet, true); // Reset the shot logic
m_map.get_tile_at(tile_x, tile_y)->shot(); m_players[pindex]->m_shot_allowed = true;
}
m_players[pindex]->m_shot_distance = 0.0; m_players[pindex]->m_shot_distance = 0.0;
m_players[pindex]->m_shot_start_time = 0.0; m_players[pindex]->m_shot_start_time = 0.0;
// If tile exists, damage the tile.
if((!p_tile.isNull()) &&
(p_tile->get_damage_state() < HexTile::DESTROYED))
{
p_tile->shot();
}
} }
} }