| 1 | // Copyright 2005 by Anthony Liekens [email protected] |
| 2 | #include <iostream> |
| 3 | |
| 4 | #include <SDL/SDL_gfxPrimitives.h> |
| 5 | |
| 6 | #include "universe.h" |
| 7 | #include "planets.h" |
| 8 | #include "players.h" |
| 9 | #include "stars.h" |
| 10 | #include "actions.h" |
| 11 | #include "animations.h" |
| 12 | #include "timer.h" |
| 13 | #include "settings.h" |
| 14 | #include "canvas.h" |
| 15 | |
| 16 | Universe::Universe() : currentSelectedPlanet(0) { |
| 17 | actionQueue = new ActionQueue(); |
| 18 | animationQueue = new AnimationQueue(); |
| 19 | |
| 20 | stars = new Stars( 100 ); |
| 21 | planets = new Planets( 15, 15 ); |
| 22 | planets->setUniverse( this ); |
| 23 | } |
| 24 | |
| 25 | Universe::Universe( int nrPlanets ) : currentSelectedPlanet(0) { |
| 26 | actionQueue = new ActionQueue(); |
| 27 | animationQueue = new AnimationQueue(); |
| 28 | |
| 29 | stars = new Stars( 100 ); |
| 30 | planets = new Planets( nrPlanets, nrPlanets ); |
| 31 | planets->setUniverse( this ); |
| 32 | } |
| 33 | |
| 34 | Universe::~Universe() { |
| 35 | delete stars; |
| 36 | delete planets; |
| 37 | delete actionQueue; |
| 38 | delete animationQueue; |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | Universe::update(Uint32 time) { |
| 43 | actionQueue->executeEventsBefore( time ); |
| 44 | animationQueue->executeEventsBefore( time ); |
| 45 | |
| 46 | planets->update(time); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | Universe::highlightNearestPlanet(int x, int y) |
| 51 | { |
| 52 | double pointerX = ( (double)x - Settings::getGameOffsetX() ) / Settings::getGameWidth(); |
| 53 | double pointerY = (double)y / Settings::getGameHeight(); |
| 54 | Planet* closestPlanet = planets->closestToCoordinate( Coordinate( pointerX, pointerY ), 1 ); |
| 55 | |
| 56 | if (currentSelectedPlanet) |
| 57 | currentSelectedPlanet->setNearestPlanet(false); |
| 58 | |
| 59 | currentSelectedPlanet = closestPlanet; |
| 60 | |
| 61 | if (currentSelectedPlanet) |
| 62 | currentSelectedPlanet->setNearestPlanet(true); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | Universe::renderBackground(Uint32 time) { |
| 68 | |
| 69 | stars->render( ); |
| 70 | |
| 71 | Canvas::drawRadar(); |
| 72 | |
| 73 | animationQueue->render(time); |
| 74 | |
| 75 | Canvas::drawSun(); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | Universe::renderForeground(Uint32 time) { |
| 80 | planets->render(time); |
| 81 | } |
| 82 | |
| 83 | /* Assigns unclaimed planets and moons to players. |
| 84 | * |
| 85 | * The algorithm tries to claim as many planets as |
| 86 | * given by planetCount. However if there are not |
| 87 | * enough unclaimed planets to fullfil the request |
| 88 | * a lower number of planets will be claimed. |
| 89 | * |
| 90 | * Each planet is given shipCount ships. |
| 91 | * |
| 92 | * Planets are claimed even if no ship is placed on |
| 93 | * it! |
| 94 | */ |
| 95 | void |
| 96 | Universe::claim(Uint32 time, Player *player, int planetCount, int shipCount) |
| 97 | { |
| 98 | Planets::iterator i = planets->begin(); |
| 99 | |
| 100 | while (i != planets->end()) |
| 101 | { |
| 102 | if (!((*i)->getMoon() || (*i)->getOwner())) |
| 103 | { |
| 104 | Planet *p = (*i); |
| 105 | |
| 106 | p->setOwner(player); |
| 107 | |
| 108 | for (int s = 0; s < shipCount; s++) |
| 109 | player->addShip(time, p); |
| 110 | |
| 111 | if (--planetCount == 0) |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | i++; |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | Universe::claimRemaining(Uint32 time, Player *player, int maxPlanetShipCount, int maxMoonShipCount) |
| 122 | { |
| 123 | Planets::iterator i = planets->begin(); |
| 124 | |
| 125 | while (i != planets->end()) |
| 126 | { |
| 127 | if (!(*i)->getOwner()) |
| 128 | { |
| 129 | Planet *p = (*i); |
| 130 | |
| 131 | p->setOwner(player); |
| 132 | |
| 133 | int ships = 1 + rand() % (p->getMoon() ? maxMoonShipCount : maxPlanetShipCount); |
| 134 | |
| 135 | for (; ships > 0; ships--) |
| 136 | player->addShip(time, p); |
| 137 | |
| 138 | } |
| 139 | i++; |
| 140 | } |
| 141 | } |