Switched to using the shot class on the server. Broadcast shots to all players

This commit is contained in:
xethm55 2012-10-08 19:28:17 -04:00
parent cc8d1a05ce
commit fbfcea6bc9
6 changed files with 70 additions and 62 deletions

View File

@ -309,18 +309,16 @@ void Client::redraw()
for(std::map<sf::Uint8, refptr<Player> >::iterator piter = m_players.begin(); piter != m_players.end(); piter++)
{
draw_player(piter->second);
if(!(piter->second->m_shot.isNull()))
{
draw_shot(piter->second->m_shot);
}
}
draw_map();
draw_sky();
draw_lava();
draw_shot_ring();
for (m_shots_iterator_t it = m_shots.begin(); it != m_shots.end(); it++)
{
draw_shot(*it);
}
draw_overlay();
}

View File

@ -258,6 +258,33 @@ void Client::update(double elapsed_time)
// This will set a death flag in the player struct.
break;
}
case PLAYER_SHOT:
{
sf::Uint8 pindex;
double x,y;
double direction;
double distance;
client_packet >> pindex;
client_packet >> x;
client_packet >> y;
client_packet >> direction;
client_packet >> distance;
// Ensure that the player who shot exists
if(m_players.end() != m_players.find(pindex))
{
// Perhaps sometime in the future, the shots will
// be different colors depending on the player
// or different power ups and what not.
refptr<Shot> shot = new Shot(sf::Vector2f(x, y),
direction,
distance);
m_players[pindex]->m_shot = shot;
}
break;
}
case TILE_DAMAGED:
{
@ -276,6 +303,7 @@ void Client::update(double elapsed_time)
if(m_players.end() != m_players.find(pindex))
{
m_players[pindex]->m_shot_allowed = true;
m_players[pindex]->m_shot = NULL;
if(pindex == m_current_player)
{
m_shot_fired = false;
@ -292,15 +320,6 @@ void Client::update(double elapsed_time)
}
}
for (m_shots_iterator_t it = m_shots.begin(); it != m_shots.end(); )
{
sf::Vector3f shot_position = (*it)->get_position();
if (shot_position.z <= 0.0)
it = m_shots.erase(it);
else
it++;
}
// For now, we are going to do a very crude shove data into
// packet from keyboard and mouse events.
// TODO: Clean this up and make it more robust
@ -413,8 +432,7 @@ void Client::create_shot()
if (m_players.size() == 0)
return;
sf::Packet client_packet;
refptr<Player> player = m_players[m_current_player];
float shot_distance = m_drawing_shot_distance + SHOT_RING_WIDTH / 2.0;
double shot_distance = m_drawing_shot_distance + SHOT_RING_WIDTH / 2.0;
sf::Uint8 packet_type = PLAYER_SHOT;
client_packet.clear();
client_packet << packet_type;
@ -422,10 +440,5 @@ void Client::create_shot()
client_packet << shot_distance;
m_net_client->sendData(client_packet, true);
m_drawing_shot_distance = 0;
player->m_shot_allowed = false;
refptr<Shot> shot = new Shot(
sf::Vector2f(player->x, player->y),
player->direction,
shot_distance);
m_shots.push_back(shot);
m_players[m_current_player]->m_shot_allowed = false;
}

View File

@ -15,11 +15,7 @@ Player::Player()
rel_mouse_movement = 0.0;
updated = false;
m_client = NULL;
m_shot_distance = 0.0;
m_shot_start_time = 0.0;
m_shot_start_x = 0.0;
m_shot_start_y = 0.0;
m_shot_direction = 0.0;
m_shot = NULL;
m_shot_allowed = true;
}

View File

@ -5,6 +5,8 @@
#include "Types.h"
#include "SFML/Config.hpp"
#include "Network.h"
#include "Shot.h"
#include "refptr.h"
class Player
{
@ -21,12 +23,8 @@ class Player
sf::Int32 rel_mouse_movement;
bool updated;
Client_t* m_client;
float m_shot_distance;
double m_shot_start_x;
double m_shot_start_y;
double m_shot_direction;
double m_shot_start_time;
bool m_shot_allowed;
refptr<Shot> m_shot;
Player();
};

View File

@ -12,8 +12,4 @@
#define TILE_DAMAGED 0xA1u
// The speed of the projectile
#define PROJECTILE_VELOCITY 100.0
#endif

View File

@ -153,22 +153,31 @@ void Server::update( double elapsed_time )
case PLAYER_SHOT:
{
sf::Uint8 pindex;
float shot_distance;
double shot_distance;
server_packet >> pindex;
server_packet >> shot_distance;
// start the shot process if a player is allowed to shoot and exits
if((m_players.end() != m_players.find(pindex)) &&
(m_players[pindex]->m_shot_allowed))
{
m_players[pindex]->m_shot_distance = shot_distance;
m_players[pindex]->m_shot_start_time = Timer::GetTimeDouble();
m_players[pindex]->m_shot_allowed = false;
{
// Perhaps sometime in the future, the shots will
// be different colors depending on the player
// or different power ups and what not.
refptr<Shot> shot = new Shot(sf::Vector2f(m_players[pindex]->x, m_players[pindex]->y),
m_players[pindex]->direction,
shot_distance);
m_players[pindex]->m_shot = shot;
m_players[pindex]->m_shot_allowed = false;
// 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;
// Send a packet to all players that a shot has been fired
server_packet.clear();
server_packet << ptype;
server_packet << pindex;
server_packet << m_players[pindex]->x;
server_packet << m_players[pindex]->y;
server_packet << m_players[pindex]->direction;
server_packet << shot_distance;
m_net_server->sendData(server_packet, true);
}
break;
}
@ -202,33 +211,28 @@ void Server::update( double elapsed_time )
}
m_players[pindex]->updated = true;
}
if((m_players[pindex]->m_shot_distance > 0.0) &&
(m_players[pindex]->m_shot_start_time > 0.0))
if(!(m_players[pindex]->m_shot.isNull()))
{
// Calculate the distance the projectile travelled so far
float distance_so_far = 0.0;
distance_so_far = (Timer::GetTimeDouble() - m_players[pindex]->m_shot_start_time) * PROJECTILE_VELOCITY;
if(distance_so_far > m_players[pindex]->m_shot_distance)
// if the position is below tiles, take the current position
// and calculate the tile location.
sf::Vector3f shot_pos = m_players[pindex]->m_shot->get_position();
if(0.0 > shot_pos.z)
{
float player_dir_x = cos(m_players[pindex]->m_shot_direction);
float player_dir_y = sin(m_players[pindex]->m_shot_direction);
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);
refptr<HexTile> p_tile = m_map.get_tile_at(tile_x, tile_y);
// Get tile at shot location.
refptr<HexTile> p_tile = m_map.get_tile_at(shot_pos.x, shot_pos.y);
// Send a message to all clients letting them know a tile was damaged
// always send message since it will reenable the player shot ability.
sf::Uint8 ptype = TILE_DAMAGED;
server_packet.clear();
server_packet << ptype;
server_packet << tile_x;
server_packet << tile_y;
server_packet << shot_pos.x;
server_packet << shot_pos.y;
server_packet << pindex; // Needed to alert the client that the player can now shoot again.
m_net_server->sendData(server_packet, true);
// Reset the shot logic
m_players[pindex]->m_shot_allowed = true;
m_players[pindex]->m_shot_distance = 0.0;
m_players[pindex]->m_shot_start_time = 0.0;
m_players[pindex]->m_shot_allowed = true;
// If tile exists, damage the tile.
if((!p_tile.isNull()) &&
@ -236,6 +240,9 @@ void Server::update( double elapsed_time )
{
p_tile->shot();
}
// Destroy the shot
m_players[pindex]->m_shot = NULL;
}
}