Changeset 30
- Timestamp:
- 12/14/07 12:16:26 (1 year ago)
- Files:
-
- trunk/guisterax/src/asteroid.d (modified) (2 diffs)
- trunk/guisterax/src/display.d (modified) (6 diffs)
- trunk/guisterax/src/energy.d (modified) (4 diffs)
- trunk/guisterax/src/game.d (modified) (5 diffs)
- trunk/guisterax/src/gold.d (modified) (4 diffs)
- trunk/guisterax/src/hole.d (modified) (2 diffs)
- trunk/guisterax/src/main.d (modified) (1 diff)
- trunk/guisterax/src/mine.d (modified) (2 diffs)
- trunk/guisterax/src/saucer.d (modified) (3 diffs)
- trunk/guisterax/src/status.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/guisterax/src/asteroid.d
r27 r30 33 33 import fire; 34 34 import ship; 35 import game; 35 36 36 37 import std.math : PI, sqrt; … … 122 123 if (cast(Fire)e || cast(Ship)e) { 123 124 m_sounds[m_type].play(); 125 if (cast(Fire)e) { 126 Game().score += 1; 127 } 128 124 129 universe.append(new Explosion(m_type, pos, speed), this); 125 130 trunk/guisterax/src/display.d
r13 r30 109 109 { 110 110 private: 111 SDL_Surface * m_screen;111 SDL_Surface *screen; 112 112 /// The singelton display 113 static MainDisplay m_display; 114 static void function()[] m_on_init; 115 113 static MainDisplay display; 116 114 public: 117 static void on_init(void function() func) {118 m_on_init ~= func;119 }120 121 115 static MainDisplay opCall() { 122 assert( m_display);123 return m_display;124 } 125 126 static MainDisplay opCall(MainDisplay disp) {127 m_display = disp;128 return m_display;129 }116 assert(display); 117 return display; 118 } 119 120 // static MainDisplay opCall(MainDisplay disp) { 121 // display = disp; 122 // return display; 123 // } 130 124 131 125 this(int w = 640, int h = 480) { 132 assert(! m_display);126 assert(!display); 133 127 134 128 DerelictSDL.load(); … … 149 143 150 144 // printf("set video mode to %d %d\n", w, h); 151 if ( ( m_screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == null )145 if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == null ) 152 146 { 153 147 throw new Error(format("Couldn't set %dx%d video mode", w, h)); … … 164 158 } 165 159 166 m_display = this; 167 168 foreach(func; m_on_init) { 169 func(); 170 } 160 // We set the singelton display 161 display = this; 171 162 } 172 163 … … 179 170 dest.x = cast(int)round(pos.x); 180 171 int h = (src)?src.h : surf.h; 181 dest.y = m_screen.h - cast(int)round(pos.y + h);172 dest.y = screen.h - cast(int)round(pos.y + h); 182 173 dest.w = surf.surf.w; 183 174 dest.h = surf.surf.h; 184 SDL_BlitSurface(surf.surf, src, m_screen, &dest);175 SDL_BlitSurface(surf.surf, src, screen, &dest); 185 176 } 186 177 … … 193 184 194 185 void flip() { 195 SDL_Flip( m_screen);186 SDL_Flip(screen); 196 187 } 197 188 198 189 void fill(int r, int g, int b) { 199 SDL_FillRect( m_screen, null, SDL_MapRGB(m_screen.format, r, g, b));190 SDL_FillRect(screen, null, SDL_MapRGB(screen.format, r, g, b)); 200 191 } 201 192 202 193 void fill(in Vect pos, Vect size, int r, int g, int b) { 203 194 SDL_Rect rect; 204 rect.x = cast(int)round(pos.x); rect.y = m_screen.h - cast(int)round(pos.y + size.y);195 rect.x = cast(int)round(pos.x); rect.y = screen.h - cast(int)round(pos.y + size.y); 205 196 rect.w = cast(int)round(size.x); rect.h = cast(int)round(size.y); 206 SDL_FillRect( m_screen, &rect, SDL_MapRGB(m_screen.format, r, g, b));197 SDL_FillRect(screen, &rect, SDL_MapRGB(screen.format, r, g, b)); 207 198 } 208 199 … … 225 216 226 217 static ~this() { 227 if ( m_display !is null) {228 delete m_display;218 if (display !is null) { 219 delete display; 229 220 } 230 221 } trunk/guisterax/src/energy.d
r24 r30 32 32 import timer; 33 33 import ship; 34 import game; 34 35 35 36 import utils; … … 38 39 { 39 40 private: 40 static Surface[] m_surfs;41 float m_rot_speed;41 static Surface[] surfs; 42 float rot_speed; 42 43 public: 43 44 static void s_init() { 44 m_surfs = Sprite.loadSurfs("data/energy/energy.png", 8, 4);45 surfs = Sprite.loadSurfs("data/energy/energy.png", 8, 4); 45 46 } 46 47 47 48 this(in Vect pos = Vect(0,0), in Vect speed = Vect(0,0)) { 48 super(new Sprite( m_surfs), pos);49 super(new Sprite(surfs), pos); 49 50 radius = 5; 50 51 mass = 2; 51 m_rot_speed = rand(-0.05, 0.05);52 rot_speed = rand(-0.05, 0.05); 52 53 this.speed = speed; 53 54 // we give a lifetime … … 56 57 57 58 override void iter(in float dt = 1) { 58 angle = angle + dt * m_rot_speed;59 angle = angle + dt * rot_speed; 59 60 super.iter(dt); 60 61 } … … 72 73 kill(); 73 74 s.hit(-0.1); 75 Game().score += 2; 74 76 } 75 77 } trunk/guisterax/src/game.d
r29 r30 127 127 class Game 128 128 { 129 private: 130 Ship m_ship; 129 public: 130 Ship ship; 131 int score = 0; 132 133 /// The singleton game 134 static Game game; 131 135 public: 132 136 static void s_init() { 133 137 Menu.s_init(); 134 138 } 135 136 139 140 this() { 141 // We set the singleton to this 142 game = this; 143 } 144 145 /// Return the game singleton 146 static Game opCall() { 147 assert(game); 148 return game; 149 } 137 150 138 151 void menu() { … … 152 165 153 166 void play() { 154 m_ship = new Ship();167 ship = new Ship(); 155 168 uint level = 1; 156 169 while(true) { … … 160 173 shop(); 161 174 wave(level); 162 if (! m_ship.alive) {175 if (!ship.alive) { 163 176 return; 164 177 } … … 168 181 169 182 void shop() { 170 Shop s = new Shop( m_ship);183 Shop s = new Shop(ship); 171 184 run(s); 172 185 } … … 174 187 void wave(uint level) { 175 188 writefln("level ", level); 176 Wave u = new Wave(level, m_ship);189 Wave u = new Wave(level, ship); 177 190 u.update(); 178 191 trunk/guisterax/src/gold.d
r24 r30 34 34 import sound; 35 35 import ship; 36 import game; 36 37 37 38 import utils; … … 40 41 { 41 42 private: 42 static Surface[] m_surfs;43 float m_rot_speed;43 static Surface[] surfs; 44 float rot_speed; 44 45 45 static Sound m_get_sound;46 static Sound get_sound; 46 47 public: 47 48 static void s_init() { 48 m_surfs = Sprite.loadSurfs("data/gold/gold.png", 8, 4);49 m_get_sound = new Sound("data/gold/get.wav");49 surfs = Sprite.loadSurfs("data/gold/gold.png", 8, 4); 50 get_sound = new Sound("data/gold/get.wav"); 50 51 } 51 52 52 53 this(in Vect pos = Vect(0,0), in Vect speed = Vect(0,0)) { 53 super(new Sprite( m_surfs), pos);54 super(new Sprite(surfs), pos); 54 55 radius = 5; 55 56 mass = 2; 56 m_rot_speed = rand(-0.05, 0.05);57 rot_speed = rand(-0.05, 0.05); 57 58 this.speed = speed; 58 59 // we give a lifetime … … 61 62 62 63 override void iter(in float dt = 1) { 63 angle = angle + dt * m_rot_speed;64 angle = angle + dt * rot_speed; 64 65 super.iter(dt); 65 66 } … … 74 75 if (Ship s = cast(Ship)e) { 75 76 kill(); 76 m_get_sound.play();77 get_sound.play(); 77 78 s.gold++; 79 Game().score += 2; 78 80 } 79 81 else if(cast(Fire) e) { trunk/guisterax/src/hole.d
r10 r30 34 34 import sound; 35 35 import ship; 36 import game; 36 37 37 38 import utils; … … 98 99 energy = 0; 99 100 s.hit(0.5); 101 } else if (cast(Fire) e) { 102 energy--; 103 if (energy <= 0) { 104 Game().score += 10; 105 } 100 106 } 101 energy--;102 107 } 103 108 trunk/guisterax/src/main.d
r25 r30 47 47 void main() 48 48 { 49 MainDisplay(new MainDisplay()); 49 // We create the main display 50 new MainDisplay(); 50 51 Clock(new Clock()); 51 52 trunk/guisterax/src/mine.d
r29 r30 33 33 import sound; 34 34 import ship; 35 import game; 35 36 36 37 import utils; … … 96 97 speed += 0.5 * e.speed * sqrt(e.mass / mass); 97 98 energy -= 0.1; 99 if (energy <= 0) 100 Game().score += 10; 98 101 } 99 102 else if (e is target) { trunk/guisterax/src/saucer.d
r22 r30 31 31 import sound; 32 32 import ship; 33 import game; 33 34 34 35 import utils; … … 79 80 m_explosion_sound.play(); 80 81 kill(); 82 Game().score += 10; 81 83 } 82 84 angle = angle + dt * m_rot_speed; … … 94 96 Vect f_target = target.pos + 20 * target.speed; 95 97 Vect speed = this.speed + 4 * (f_target - pos).normal(); 96 speed.rotate(rand(-0.2 , 0.2)); // we add a random angle to the fire98 speed.rotate(rand(-0.25, 0.25)); // we add a random angle to the fire 97 99 m_fire_sound.play(); 98 100 universe.append(new Saucer.Fire(pos, speed), this); trunk/guisterax/src/status.d
r16 r30 9 9 import display; 10 10 import font; 11 import game; 11 12 12 13 class Status : Actor … … 38 39 } 39 40 41 void draw_score(Display disp, in Vect pos) { 42 Font.font.draw(disp, format("SCORE:%d", Game().score), pos); 43 } 44 40 45 override void draw(Display disp, in Vect pos) { 41 46 draw_energy(disp, Vect(60,43)); 42 47 draw_gold(disp, Vect(10,15)); 48 draw_score(disp, Vect(150,15)); 43 49 super.draw(disp, pos); 44 50 }
