File: universe.cpp

package info (click to toggle)
qonk 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,076 kB
  • ctags: 2,026
  • sloc: cpp: 14,978; sh: 3,464; makefile: 200
file content (141 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2005 by Anthony Liekens anthony@liekens.net
#include <iostream>

#include <SDL/SDL_gfxPrimitives.h>

#include "universe.h"
#include "planets.h"
#include "players.h"
#include "stars.h"
#include "actions.h"
#include "animations.h"
#include "timer.h"
#include "settings.h"
#include "canvas.h"

Universe::Universe() : currentSelectedPlanet(0) {
  actionQueue = new ActionQueue();
  animationQueue = new AnimationQueue();
  
  stars = new Stars( 100 );
  planets = new Planets( 15, 15 );
  planets->setUniverse( this );
}

Universe::Universe( int nrPlanets ) : currentSelectedPlanet(0) {
  actionQueue = new ActionQueue();
  animationQueue = new AnimationQueue();
  
  stars = new Stars( 100 );
  planets = new Planets( nrPlanets, nrPlanets );
  planets->setUniverse( this );
}

Universe::~Universe() {
	delete stars;
	delete planets;
	delete actionQueue;
	delete animationQueue;
}

void
Universe::update(Uint32 time) {
  actionQueue->executeEventsBefore( time );
  animationQueue->executeEventsBefore( time );
  
  planets->update(time);
}

void
Universe::highlightNearestPlanet(int x, int y)
{
	double pointerX = ( (double)x - Settings::getGameOffsetX() ) / Settings::getGameWidth();
	double pointerY = (double)y / Settings::getGameHeight();
	Planet* closestPlanet = planets->closestToCoordinate( Coordinate( pointerX, pointerY ), 1 );

  if (currentSelectedPlanet)
	  currentSelectedPlanet->setNearestPlanet(false);
		
	currentSelectedPlanet = closestPlanet;

	if (currentSelectedPlanet)
	  currentSelectedPlanet->setNearestPlanet(true);

}

void
Universe::renderBackground(Uint32 time) {

	stars->render( );
	
  Canvas::drawRadar();
	
	animationQueue->render(time);

  Canvas::drawSun();
}

void
Universe::renderForeground(Uint32 time) {
	planets->render(time);
}

/* Assigns unclaimed planets and moons to players.
 * 
 * The algorithm tries to claim as many planets as
 * given by planetCount. However if there are not
 * enough unclaimed planets to fullfil the request
 * a lower number of planets will be claimed.
 * 
 * Each planet is given shipCount ships.  
 * 
 * Planets are claimed even if no ship is placed on
 * it!
 */
void
Universe::claim(Uint32 time, Player *player, int planetCount, int shipCount)
{
  Planets::iterator i = planets->begin();

  while (i != planets->end())
  {
    if (!((*i)->getMoon() || (*i)->getOwner()))
    {
      Planet *p = (*i);
      
      p->setOwner(player);
      
      for (int s = 0; s < shipCount; s++)
        player->addShip(time, p);
        
      if (--planetCount == 0)
        return;
    }
    
    i++;
  }
  
}

void
Universe::claimRemaining(Uint32 time, Player *player, int maxPlanetShipCount, int maxMoonShipCount)
{
  Planets::iterator i = planets->begin();

  while (i != planets->end())
  {
    if (!(*i)->getOwner())
    {
      Planet *p = (*i);

      p->setOwner(player);
      
      int ships = 1 + rand() % (p->getMoon() ? maxMoonShipCount : maxPlanetShipCount);
      
      for (; ships > 0; ships--)
        player->addShip(time, p);
        
    }
    i++;
  }
}