Skip to content

Commit

Permalink
Clients now remove shots when they collide (but damage dealing is sti…
Browse files Browse the repository at this point in the history
…ll up to the server). Also removed the superfluous Game::initGame(...).
  • Loading branch information
nitzel committed Apr 17, 2019
1 parent b3ebde9 commit 66a4ad6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ int client(std::string hostname) {
game->clearChanged();
game->update(dt + vdt, false); // dont update planets as client
game->generateTree();
//game->letCollide(); // todo activate later :)
game->letCollide(false); // todo activate later :) shots shouldn't make damage but vanish when hitting sth
vdt = 0;
}

Expand Down
18 changes: 6 additions & 12 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void Game::update(const double dt, const bool bUpdatePlanets) {
void Game::shootAndCollide() {
generateTree();
letShoot();
letCollide();
letCollide(true);
}

void Game::generateTree() {
Expand Down Expand Up @@ -143,7 +143,7 @@ void Game::letShoot() {
}
}

void Game::letCollide() {
void Game::letCollide(bool dealDamage) {
/////////////////
// CollisionTesting
////////////////
Expand All @@ -159,7 +159,8 @@ void Game::letCollide() {
if (target.health && distanceSQ(shots[i].x, shots[i].y, target.x, target.y) < SHIP_RADIUS * SHIP_RADIUS) {
// collision!!!
shots[i].timeToLive = -1;
takeDamage(target);
if (dealDamage)
takeDamage(target);
break;
}
}
Expand All @@ -171,7 +172,8 @@ void Game::letCollide() {
if (planets[j].party != party && distanceSQ(shots[i].x, shots[i].y, planets[j].x, planets[j].y) < PLANET_RADIUS * PLANET_RADIUS) {
// collision!!!
shots[i].timeToLive = -1;
takeDamage(planets[j], party);
if (dealDamage)
takeDamage(planets[j], party);
break;
}
}
Expand Down Expand Up @@ -768,14 +770,6 @@ void Game::updateShips(const double dt) {
}
}

void Game::initGame(saPlanet & planets, saShip * ships, saShot * shots, const size_t MAX_SHIPS) {
initPlanets(planets, 6);
const size_t MAX_SHOTS = (size_t)(MAX_SHIPS * (float)SHOT_LIFETIME / (float)SHIP_SHOOT_DELAY) + 1000; // + 1000 just to be sure
initShots(shots[PA], MAX_SHOTS);
initShots(shots[PB], MAX_SHOTS);
initShips(ships[PA], MAX_SHIPS);
initShips(ships[PB], MAX_SHIPS);
}
void Game::initPlanets(saPlanet & planets, const size_t size) {
planets.size = size;
planets.planets = new sPlanet[planets.size];
Expand Down
5 changes: 2 additions & 3 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <cstring>
#include <cstdio>


class Game {
public:
struct GameConfig {
Expand Down Expand Up @@ -62,11 +61,11 @@ class Game {

void generateTree();
void letShoot();
void letCollide();
/* dealDamage=false: Shots don't deal any damage but vanish (use on client) */
void letCollide(bool dealDamage);

private:
/// init list of game objects
void initGame(saPlanet& planets, saShip* ships, saShot* shots, const size_t MAX_SHIPS);
void initPlanets(saPlanet& planets, const size_t size);
void initShips(saShip& ships, const size_t size);
void initShots(saShot& shots, const size_t size);
Expand Down

0 comments on commit 66a4ad6

Please sign in to comment.