Changeset 30

Show
Ignore:
Timestamp:
12/14/07 12:16:26 (1 year ago)
Author:
charlie137
Message:

Added score

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/guisterax/src/asteroid.d

    r27 r30  
    3333import fire; 
    3434import ship; 
     35import game; 
    3536 
    3637import std.math : PI, sqrt; 
     
    122123        if (cast(Fire)e || cast(Ship)e) { 
    123124            m_sounds[m_type].play(); 
     125            if (cast(Fire)e) { 
     126                Game().score += 1; 
     127            } 
     128             
    124129            universe.append(new Explosion(m_type, pos, speed), this); 
    125130             
  • trunk/guisterax/src/display.d

    r13 r30  
    109109{ 
    110110private: 
    111     SDL_Surface *m_screen; 
     111    SDL_Surface *screen; 
    112112    /// The singelton display 
    113     static MainDisplay m_display; 
    114     static void function()[] m_on_init; 
    115  
     113    static MainDisplay display; 
    116114public: 
    117     static void on_init(void function() func) { 
    118         m_on_init ~= func; 
    119     } 
    120  
    121115    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//    } 
    130124     
    131125    this(int w = 640, int h = 480) { 
    132         assert(!m_display); 
     126        assert(!display); 
    133127         
    134128        DerelictSDL.load(); 
     
    149143         
    150144        // 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 ) 
    152146        { 
    153147            throw new Error(format("Couldn't set %dx%d video mode", w, h)); 
     
    164158        } 
    165159         
    166         m_display = this; 
    167          
    168         foreach(func; m_on_init) { 
    169             func(); 
    170         } 
     160        // We set the singelton display 
     161        display = this; 
    171162    } 
    172163     
     
    179170        dest.x = cast(int)round(pos.x); 
    180171        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); 
    182173        dest.w = surf.surf.w; 
    183174        dest.h = surf.surf.h; 
    184         SDL_BlitSurface(surf.surf, src, m_screen, &dest); 
     175        SDL_BlitSurface(surf.surf, src, screen, &dest); 
    185176    } 
    186177     
     
    193184     
    194185    void flip() { 
    195         SDL_Flip(m_screen); 
     186        SDL_Flip(screen); 
    196187    } 
    197188     
    198189    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)); 
    200191    } 
    201192     
    202193    void fill(in Vect pos, Vect size, int r, int g, int b) { 
    203194        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); 
    205196        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)); 
    207198    } 
    208199     
     
    225216     
    226217    static ~this() { 
    227         if (m_display !is null) { 
    228             delete m_display; 
     218        if (display !is null) { 
     219            delete display; 
    229220        } 
    230221    } 
  • trunk/guisterax/src/energy.d

    r24 r30  
    3232import timer; 
    3333import ship; 
     34import game; 
    3435 
    3536import utils; 
     
    3839{ 
    3940private: 
    40     static Surface[] m_surfs; 
    41     float m_rot_speed; 
     41    static Surface[] surfs; 
     42    float rot_speed; 
    4243public: 
    4344    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); 
    4546    } 
    4647  
    4748    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); 
    4950        radius = 5; 
    5051        mass = 2; 
    51         m_rot_speed = rand(-0.05, 0.05); 
     52        rot_speed = rand(-0.05, 0.05); 
    5253        this.speed = speed; 
    5354        // we give a lifetime 
     
    5657     
    5758    override void iter(in float dt = 1) { 
    58         angle = angle + dt * m_rot_speed; 
     59        angle = angle + dt * rot_speed; 
    5960        super.iter(dt); 
    6061    } 
     
    7273            kill(); 
    7374            s.hit(-0.1); 
     75            Game().score += 2; 
    7476        } 
    7577    } 
  • trunk/guisterax/src/game.d

    r29 r30  
    127127class Game 
    128128{ 
    129 private: 
    130     Ship m_ship; 
     129public: 
     130    Ship ship; 
     131    int score = 0; 
     132     
     133    /// The singleton game 
     134    static Game game; 
    131135public: 
    132136    static void s_init() { 
    133137        Menu.s_init(); 
    134138    } 
    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    } 
    137150     
    138151    void menu() { 
     
    152165     
    153166    void play() { 
    154         m_ship = new Ship(); 
     167        ship = new Ship(); 
    155168        uint level = 1; 
    156169        while(true) { 
     
    160173            shop(); 
    161174            wave(level); 
    162             if (!m_ship.alive) { 
     175            if (!ship.alive) { 
    163176                return; 
    164177            } 
     
    168181     
    169182    void shop() { 
    170         Shop s = new Shop(m_ship); 
     183        Shop s = new Shop(ship); 
    171184        run(s); 
    172185    } 
     
    174187    void wave(uint level) { 
    175188        writefln("level ", level); 
    176         Wave u = new Wave(level, m_ship); 
     189        Wave u = new Wave(level, ship); 
    177190        u.update(); 
    178191         
  • trunk/guisterax/src/gold.d

    r24 r30  
    3434import sound; 
    3535import ship; 
     36import game; 
    3637 
    3738import utils; 
     
    4041{ 
    4142private: 
    42     static Surface[] m_surfs; 
    43     float m_rot_speed; 
     43    static Surface[] surfs; 
     44    float rot_speed; 
    4445     
    45     static Sound m_get_sound; 
     46    static Sound get_sound; 
    4647public: 
    4748    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"); 
    5051    } 
    5152         
    5253    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); 
    5455        radius = 5; 
    5556        mass = 2; 
    56         m_rot_speed = rand(-0.05, 0.05); 
     57        rot_speed = rand(-0.05, 0.05); 
    5758        this.speed = speed; 
    5859        // we give a lifetime 
     
    6162     
    6263    override void iter(in float dt = 1) { 
    63         angle = angle + dt * m_rot_speed; 
     64        angle = angle + dt * rot_speed; 
    6465        super.iter(dt); 
    6566    } 
     
    7475        if (Ship s = cast(Ship)e) { 
    7576            kill(); 
    76             m_get_sound.play(); 
     77            get_sound.play(); 
    7778            s.gold++; 
     79            Game().score += 2; 
    7880        } 
    7981        else if(cast(Fire) e) { 
  • trunk/guisterax/src/hole.d

    r10 r30  
    3434import sound; 
    3535import ship; 
     36import game; 
    3637 
    3738import utils; 
     
    9899            energy = 0; 
    99100            s.hit(0.5); 
     101        } else if (cast(Fire) e) { 
     102            energy--; 
     103            if (energy <= 0) { 
     104                Game().score += 10; 
     105            } 
    100106        } 
    101         energy--; 
    102107    } 
    103108     
  • trunk/guisterax/src/main.d

    r25 r30  
    4747void main() 
    4848{ 
    49     MainDisplay(new MainDisplay()); 
     49    // We create the main display 
     50    new MainDisplay(); 
    5051    Clock(new Clock()); 
    5152 
  • trunk/guisterax/src/mine.d

    r29 r30  
    3333import sound; 
    3434import ship; 
     35import game; 
    3536 
    3637import utils; 
     
    9697            speed += 0.5 * e.speed * sqrt(e.mass / mass); 
    9798            energy -= 0.1; 
     99            if (energy <= 0) 
     100                Game().score += 10; 
    98101        } 
    99102        else if (e is target) { 
  • trunk/guisterax/src/saucer.d

    r22 r30  
    3131import sound; 
    3232import ship; 
     33import game; 
    3334 
    3435import utils; 
     
    7980            m_explosion_sound.play(); 
    8081            kill(); 
     82            Game().score += 10; 
    8183        } 
    8284        angle = angle + dt * m_rot_speed; 
     
    9496        Vect f_target = target.pos + 20 * target.speed; 
    9597        Vect speed = this.speed + 4 * (f_target - pos).normal(); 
    96         speed.rotate(rand(-0.2, 0.2));  // we add a random angle to the fire 
     98        speed.rotate(rand(-0.25, 0.25));  // we add a random angle to the fire 
    9799        m_fire_sound.play(); 
    98100        universe.append(new Saucer.Fire(pos, speed), this); 
  • trunk/guisterax/src/status.d

    r16 r30  
    99import display; 
    1010import font; 
     11import game; 
    1112 
    1213class Status : Actor 
     
    3839    } 
    3940     
     41    void draw_score(Display disp, in Vect pos) { 
     42        Font.font.draw(disp, format("SCORE:%d", Game().score), pos); 
     43    } 
     44     
    4045    override void draw(Display disp, in Vect pos) { 
    4146        draw_energy(disp, Vect(60,43)); 
    4247        draw_gold(disp, Vect(10,15)); 
     48        draw_score(disp, Vect(150,15)); 
    4349        super.draw(disp, pos); 
    4450    }