Initial shot logic to remove tiles

This commit is contained in:
xethm55 2012-10-02 00:34:08 -04:00
parent 0a34f6044f
commit fd04772828
10 changed files with 129 additions and 8 deletions

View File

@ -8,6 +8,8 @@
#include "ccfs.h" #include "ccfs.h"
#include "HexTile.h" #include "HexTile.h"
#include <iostream>
using namespace std; using namespace std;
#define LEN(arr) (sizeof(arr)/sizeof(arr[0])) #define LEN(arr) (sizeof(arr)/sizeof(arr[0]))
@ -341,7 +343,8 @@ void Client::draw_map()
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
if (m_map.tile_present(x, y)) if ((m_map.tile_present(x, y)) &&
(m_map.get_tile_at(x,y)->get_damage_state() == HexTile::UNDAMAGED))
{ {
refptr<HexTile> tile = m_map.get_tile(x, y); refptr<HexTile> tile = m_map.get_tile(x, y);
float cx = tile->get_x(); float cx = tile->get_x();
@ -410,7 +413,8 @@ void Client::draw_overlay()
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
if (m_map.tile_present(x, y)) if ((m_map.tile_present(x, y)) &&
(m_map.get_tile_at(x,y)->get_damage_state() == HexTile::UNDAMAGED))
{ {
refptr<HexTile> tile = m_map.get_tile(x, y); refptr<HexTile> tile = m_map.get_tile(x, y);
float cx = tile->get_x(); float cx = tile->get_x();

View File

@ -16,6 +16,7 @@ Client::Client()
m_current_player = 0; m_current_player = 0;
m_left_button_pressed = false; m_left_button_pressed = false;
m_drawing_shot = false; m_drawing_shot = false;
m_shot_fired = false;
} }
Client::~Client() Client::~Client()
@ -121,16 +122,19 @@ void Client::run(bool fullscreen, int width, int height, std::string pname)
break; break;
} }
break; break;
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_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) &&
(m_client_has_focus))
{ {
m_drawing_shot = false; m_drawing_shot = false;
m_left_button_pressed = false; m_left_button_pressed = false;
/* TODO: trigger shot network message */ m_shot_fired = true;
} }
break; break;
case sf::Event::Resized: case sf::Event::Resized:
@ -244,6 +248,20 @@ void Client::update(double elapsed_time)
// This will set a death flag in the player struct. // This will set a death flag in the player struct.
break; break;
} }
case TILE_DAMAGED:
{
float x;
float y;
client_packet >> x;
client_packet >> y;
if((!m_map.get_tile_at(x, y).isNull()))
{
m_map.get_tile_at(x, y)->shot();
}
break;
}
default : default :
{ {
// Eat the packet // Eat the packet
@ -334,6 +352,18 @@ void Client::update(double elapsed_time)
m_players[m_current_player]->d_pressed = d_pressed; m_players[m_current_player]->d_pressed = d_pressed;
m_players[m_current_player]->rel_mouse_movement = rel_mouse_movement; m_players[m_current_player]->rel_mouse_movement = rel_mouse_movement;
} }
if(m_shot_fired)
{
sf::Uint8 packet_type = PLAYER_SHOT;
client_packet.clear();
client_packet << packet_type;
client_packet << m_current_player;
client_packet << m_drawing_shot_distance;
m_net_client->sendData(client_packet, true);
m_shot_fired = false;
m_drawing_shot_distance = 0;
}
} }
else if(!registered_player) else if(!registered_player)
{ {

View File

@ -67,6 +67,7 @@ class Client
bool m_left_button_pressed; bool m_left_button_pressed;
bool m_drawing_shot; bool m_drawing_shot;
float m_drawing_shot_distance; float m_drawing_shot_distance;
bool m_shot_fired;
}; };
#endif #endif

View File

@ -11,6 +11,7 @@ HexTile::HexTile(float x, float y, float size)
m_x = x; m_x = x;
m_y = y; m_y = y;
m_size = size; m_size = size;
m_damage_state = UNDAMAGED;
} }
bool HexTile::point_within(float x, float y) bool HexTile::point_within(float x, float y)
@ -39,3 +40,12 @@ bool HexTile::point_within(float x, float y)
return false; return false;
return true; return true;
} }
void HexTile::shot()
{
if(m_damage_state < DESTROYED)
{
// Note: This only works because these are in order in the ENUM.
m_damage_state = (Tile_Damage_States_t)((int)(m_damage_state) + 1);
}
}

View File

@ -6,15 +6,25 @@
class HexTile class HexTile
{ {
public: public:
typedef enum{
UNDAMAGED,
DAMAGED,
DESTROYED
}Tile_Damage_States_t;
HexTile(float x, float y, float size); HexTile(float x, float y, float size);
bool point_within(float x, float y); bool point_within(float x, float y);
float get_x() { return m_x; } float get_x() { return m_x; }
float get_y() { return m_y; } float get_y() { return m_y; }
float get_size() { return m_size; } float get_size() { return m_size; }
Tile_Damage_States_t get_damage_state() { return m_damage_state; }
void shot();
protected: protected:
float m_x; float m_x;
float m_y; float m_y;
float m_size; float m_size;
Tile_Damage_States_t m_damage_state;
}; };
#endif #endif

View File

@ -40,7 +40,7 @@ Map::Map(int width, int height, float tile_size)
|| (outer_out.point_within(y, x) || (outer_out.point_within(y, x)
&& !outer_in.point_within(y, x))) && !outer_in.point_within(y, x)))
{ {
refptr<HexTile> get_tile_at(float x, float y); refptr<HexTile> get_tile_at(float x, float y);
ht = new HexTile(x, y, tile_size); ht = new HexTile(x, y, tile_size);
} }

View File

@ -12,7 +12,13 @@ Player::Player()
a_pressed = KEY_NOT_PRESSED; a_pressed = KEY_NOT_PRESSED;
s_pressed = KEY_NOT_PRESSED; s_pressed = KEY_NOT_PRESSED;
d_pressed = KEY_NOT_PRESSED; d_pressed = KEY_NOT_PRESSED;
rel_mouse_movement = 0; rel_mouse_movement = 0.0;
updated = false; updated = false;
m_client = NULL; 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;
} }

View File

@ -21,6 +21,12 @@ class Player
sf::Int32 rel_mouse_movement; sf::Int32 rel_mouse_movement;
bool updated; bool updated;
Client_t* m_client; 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;
Player(); Player();
}; };

View File

@ -8,5 +8,12 @@
#define PLAYER_DISCONNECT 0x2Bu #define PLAYER_DISCONNECT 0x2Bu
#define PLAYER_DEATH 0x3Cu #define PLAYER_DEATH 0x3Cu
#define PLAYER_UPDATE 0x4Du #define PLAYER_UPDATE 0x4Du
#define PLAYER_SHOT 0x5Eu
#define TILE_DAMAGED 0xA1u
// The speed of the projectile
#define PROJECTILE_VELOCITY 100.0
#endif #endif

View File

@ -149,6 +149,24 @@ void Server::update( double elapsed_time )
// playing field. // playing field.
break; break;
} }
case PLAYER_SHOT:
{
sf::Uint8 pindex;
float shot_distance;
server_packet >> pindex;
server_packet >> shot_distance;
m_players[pindex]->m_shot_distance = shot_distance;
m_players[pindex]->m_shot_start_time = Timer::GetTimeDouble();
// 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;
}
default: default:
{ {
// Just eat the packet // Just eat the packet
@ -177,6 +195,35 @@ void Server::update( double elapsed_time )
} }
m_players[pindex]->updated = true; m_players[pindex]->updated = true;
} }
if((m_players[pindex]->m_shot_distance > 0.0) &&
(m_players[pindex]->m_shot_start_time > 0.0))
{
// 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)
{
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);
if((!m_map.get_tile_at(tile_x, tile_y).isNull()) &&
(m_map.get_tile_at(tile_x, tile_y)->get_damage_state() < HexTile::DESTROYED))
{
// Send a message to all clients letting them know a tile was damaged
sf::Uint8 ptype = TILE_DAMAGED;
server_packet.clear();
server_packet << ptype;
server_packet << tile_x;
server_packet << tile_y;
m_net_server->sendData(server_packet, true);
m_map.get_tile_at(tile_x, tile_y)->shot();
}
m_players[pindex]->m_shot_distance = 0.0;
m_players[pindex]->m_shot_start_time = 0.0;
}
}
if (KEY_PRESSED == m_players[pindex]->a_pressed) if (KEY_PRESSED == m_players[pindex]->a_pressed)
{ {
double direction = m_players[pindex]->direction + M_PI_2; double direction = m_players[pindex]->direction + M_PI_2;