improving aspect ratio and window resize/scaling. done for game objects.
This commit is contained in:
parent
1562dbe8a7
commit
4353609580
14 changed files with 209 additions and 109 deletions
|
|
@ -10,7 +10,6 @@
|
|||
#include "object.hpp"
|
||||
#include "missile.hpp"
|
||||
#include "player.hpp"
|
||||
#include "planet.hpp"
|
||||
#include "ship.hpp"
|
||||
#include "commands.hpp"
|
||||
#include "trace.hpp"
|
||||
|
|
@ -21,6 +20,14 @@
|
|||
namespace game {
|
||||
void State::init(int numPlanets, bool devMode)
|
||||
{
|
||||
for (Planet *planet : planets) {
|
||||
delete(planet);
|
||||
}
|
||||
planets.clear();
|
||||
|
||||
//for (Player *player : players) {
|
||||
//}
|
||||
|
||||
m_nextId = 0;
|
||||
m_time = 0.0;
|
||||
m_shipRadius = 0.02;
|
||||
|
|
@ -30,33 +37,59 @@ namespace game {
|
|||
m_maxNumTraces = 10;
|
||||
m_developerMode = devMode;
|
||||
|
||||
Planet::Material mat = Planet::Material::Rock;
|
||||
setPlayingFieldCenter(0, 0);
|
||||
|
||||
for (int i=0; i<numPlanets; i++) {
|
||||
switch(i) {
|
||||
case 0:
|
||||
mat = Planet::Material::Sun;
|
||||
break;
|
||||
// TODO: need aspect ratio or data!
|
||||
//setPlayingFieldSize(1000, 300);
|
||||
|
||||
case 1:
|
||||
case 2:
|
||||
mat = Planet::Material::Water;
|
||||
break;
|
||||
setupPlanets(numPlanets);
|
||||
}
|
||||
|
||||
case 3:
|
||||
case 4:
|
||||
mat = Planet::Material::Sand;
|
||||
break;
|
||||
bool State::findPlanetSpawnPosition(bool planetIsSun, float radius, glm::vec2 *pos)
|
||||
{
|
||||
(void) planetIsSun;
|
||||
|
||||
case 5:
|
||||
mat = Planet::Material::Metal;
|
||||
break;
|
||||
bool tooNearToCenter = true;
|
||||
bool collidesWithOtherPlanet = true;
|
||||
|
||||
default:
|
||||
mat = Planet::Material::Rock;
|
||||
const glm::vec2 spawnArea = 0.9f * (m_playingFieldSize/std::max(m_playingFieldSize.x, m_playingFieldSize.y));
|
||||
|
||||
// distribute but not in the center and not next to other planets
|
||||
int tries = 0;
|
||||
do {
|
||||
*pos = spawnArea * util::randv2_m1_1();
|
||||
|
||||
collidesWithOtherPlanet = false;
|
||||
tooNearToCenter = glm::length(*pos) < 0.1;
|
||||
|
||||
if (!tooNearToCenter) {
|
||||
for (const Planet *other : planets) {
|
||||
float d = glm::distance(other->position, *pos);
|
||||
|
||||
float extraDist = (other->material == Planet::Material::Sun)
|
||||
? 4.0
|
||||
: 1.0;
|
||||
|
||||
if (d < extraDist*other->radius + radius) {
|
||||
collidesWithOtherPlanet = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tries++ > 1000) {
|
||||
return false;
|
||||
}
|
||||
|
||||
glm::vec2 pos;
|
||||
} while(collidesWithOtherPlanet || tooNearToCenter);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void State::setupPlanets(int numPlanets)
|
||||
{
|
||||
for (int i=0; i<numPlanets; i++) {
|
||||
Planet::Material mat = materialForStandardPlanetDistribution(i);
|
||||
|
||||
float radius = 0.03 + 0.07*util::randf_0_1();
|
||||
if (i == 0) {
|
||||
// sun is bigger but not too big
|
||||
|
|
@ -67,35 +100,33 @@ namespace game {
|
|||
|
||||
}
|
||||
|
||||
bool tooNearToCenter = true;
|
||||
bool collidesWithOtherPlanet = true;
|
||||
glm::vec2 pos;
|
||||
if (findPlanetSpawnPosition(mat == Planet::Material::Sun, radius, &pos)) {
|
||||
planets.push_back(new Planet(pos, i, radius, mat));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// distribute but not in the center and not next to other planets
|
||||
int tries = 0;
|
||||
do {
|
||||
pos = util::randv2_m1_1();
|
||||
Planet::Material State::materialForStandardPlanetDistribution(int index)
|
||||
{
|
||||
// a few sun/water/sand/metall planents and the rest rocks
|
||||
switch(index) {
|
||||
case 0:
|
||||
return Planet::Material::Sun;
|
||||
|
||||
collidesWithOtherPlanet = false;
|
||||
tooNearToCenter = glm::length(pos) < 0.1;
|
||||
case 1:
|
||||
case 2:
|
||||
return Planet::Material::Water;
|
||||
|
||||
if (!tooNearToCenter) {
|
||||
for (const Planet *other : planets) {
|
||||
float d = glm::distance(other->position, pos);
|
||||
case 3:
|
||||
case 4:
|
||||
return Planet::Material::Sand;
|
||||
|
||||
float extraDist = (other->material == Planet::Material::Sun)
|
||||
? 4.0
|
||||
: 1.0;
|
||||
case 5:
|
||||
return Planet::Material::Metal;
|
||||
|
||||
if (d < extraDist*other->radius + radius) {
|
||||
collidesWithOtherPlanet = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} while((collidesWithOtherPlanet || tooNearToCenter) && tries++ < 1000);
|
||||
|
||||
planets.push_back(new Planet(pos, i, radius, mat));
|
||||
default:
|
||||
return Planet::Material::Rock;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -430,4 +461,14 @@ namespace game {
|
|||
deleteTrace(trace);
|
||||
}
|
||||
}
|
||||
|
||||
void State::setPlayingFieldCenter(int center_x, int center_y)
|
||||
{
|
||||
m_playingFieldCenter = glm::vec2((float) center_x, (float) center_y);
|
||||
}
|
||||
|
||||
void State::setPlayingFieldSize(int width, int height)
|
||||
{
|
||||
m_playingFieldSize = glm::vec2(width, height);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
#include <glm/vec2.hpp>
|
||||
|
||||
#include "missile.hpp"
|
||||
#include "planet.hpp"
|
||||
|
||||
// TODO:
|
||||
// give points for equipment / better weapons / more energy when:
|
||||
|
|
@ -27,7 +28,7 @@ namespace game {
|
|||
// forward declarations
|
||||
class Command;
|
||||
class Player;
|
||||
class Planet;
|
||||
|
||||
class Ship;
|
||||
class Trace;
|
||||
class Explosion;
|
||||
|
|
@ -91,6 +92,11 @@ namespace game {
|
|||
m_developerMode = on;
|
||||
}
|
||||
|
||||
// called when the rendering window changes
|
||||
// TODO: give dpi as information too
|
||||
void setPlayingFieldSize(int width, int height);
|
||||
void setPlayingFieldCenter(int width, int height);
|
||||
|
||||
/*************************************************************************/
|
||||
/* Rendering */
|
||||
/*************************************************************************/
|
||||
|
|
@ -111,12 +117,15 @@ namespace game {
|
|||
void playerKillsPlayer(Player *killer, Player *victim);
|
||||
|
||||
void addExplosionFromHit(const Missile::Event *evt);
|
||||
bool findPlanetSpawnPosition(bool planetIsSun, float radius, glm::vec2 *pos);
|
||||
|
||||
void advanceTraceAges(float dt);
|
||||
void advanceExplosions(float dt);
|
||||
void advancePlayerShipSpawns(float dt);
|
||||
void advancePlayerCommands(float dt);
|
||||
void advancePlayerMissiles(float dt);
|
||||
void setupPlanets(int numPlanets);
|
||||
Planet::Material materialForStandardPlanetDistribution(int index);
|
||||
|
||||
// try to spawn a ship for this player.
|
||||
// return true on success, false on failure to find a spot.
|
||||
|
|
@ -134,5 +143,8 @@ namespace game {
|
|||
int m_maxNumTraces;
|
||||
float m_time;
|
||||
bool m_developerMode;
|
||||
|
||||
glm::vec2 m_playingFieldCenter;
|
||||
glm::vec2 m_playingFieldSize;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue