From fbfcea6bc99ac9e23f838faaf4de61a90dcd5cbe Mon Sep 17 00:00:00 2001 From: xethm55 Date: Mon, 8 Oct 2012 19:28:17 -0400 Subject: [PATCH] Switched to using the shot class on the server. Broadcast shots to all players --- src/client/Client-gl.cc | 10 +++----- src/client/Client.cc | 47 +++++++++++++++++++++------------ src/common/Player.cc | 6 +---- src/common/Player.h | 8 +++--- src/common/Types.h | 4 --- src/server/Server.cc | 57 +++++++++++++++++++++++------------------ 6 files changed, 70 insertions(+), 62 deletions(-) diff --git a/src/client/Client-gl.cc b/src/client/Client-gl.cc index 7b6d3e3..dff1c77 100644 --- a/src/client/Client-gl.cc +++ b/src/client/Client-gl.cc @@ -309,18 +309,16 @@ void Client::redraw() for(std::map >::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(); } diff --git a/src/client/Client.cc b/src/client/Client.cc index 285ed07..a25b5a1 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -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 = 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 = 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 = 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; } diff --git a/src/common/Player.cc b/src/common/Player.cc index b05c182..13ecef9 100644 --- a/src/common/Player.cc +++ b/src/common/Player.cc @@ -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; } diff --git a/src/common/Player.h b/src/common/Player.h index 315cb27..076e088 100644 --- a/src/common/Player.h +++ b/src/common/Player.h @@ -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 m_shot; Player(); }; diff --git a/src/common/Types.h b/src/common/Types.h index be49fbd..0e1be63 100644 --- a/src/common/Types.h +++ b/src/common/Types.h @@ -12,8 +12,4 @@ #define TILE_DAMAGED 0xA1u -// The speed of the projectile -#define PROJECTILE_VELOCITY 100.0 - - #endif diff --git a/src/server/Server.cc b/src/server/Server.cc index 447da9a..8a67ce7 100644 --- a/src/server/Server.cc +++ b/src/server/Server.cc @@ -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 = 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 p_tile = m_map.get_tile_at(tile_x, tile_y); + // Get tile at shot location. + refptr 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; } }