Changeset 31

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

Some code cleanning

Files:

Legend:

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

    r23 r31  
    3737{ 
    3838protected: 
    39      
    40     Actor[] m_next_children; 
    41      
     39    /// The children as they will be in the next iteration 
     40    Actor[] next_children; 
    4241public: 
    4342    /// The parent of this actor. 
     
    4746    /// Defines if the actor has to be deleted or not. 
    4847    bool alive = true; 
    49      
    5048public: 
    5149    /// Return all the children that are of type T. 
     
    8179    void append(Actor a, bool end = true) { 
    8280        if (end) { 
    83             m_next_children ~= a; 
     81            next_children ~= a; 
    8482        } else { 
    85             m_next_children = [a] ~ m_next_children; 
     83            next_children = [a] ~ next_children; 
    8684        } 
    8785        a.parent = this; 
     
    9492    */ 
    9593    void append(Actor a, Actor pos) { 
    96         m_next_children = insert(m_next_children, a, pos); 
     94        next_children = insert(next_children, a, pos); 
    9795        a.parent = this; 
    9896    } 
     
    116114        // update actor list 
    117115        bool isalive(in Actor a) {return a.alive;} 
    118         children = filter!(Actor)(m_next_children, &isalive); 
    119         m_next_children = children.dup; 
     116        children = filter!(Actor)(next_children, &isalive); 
     117        next_children = children.dup; 
    120118        foreach(a; children) { 
    121119            a.update(); 
  • trunk/guisterax/src/asteroid.d

    r30 r31  
    3535import game; 
    3636 
     37 
    3738import std.math : PI, sqrt; 
    3839 
     
    4243    enum {SMALL = 0, NORMAL, BIG}; 
    4344private: 
    44     static Surface[][][3] m_surfs; 
    45     static Sound[3] m_sounds; 
     45    static Surface[][][3] surfs; 
     46    static Sound[3] sounds; 
    4647     
    47     static float[3] m_radii = [15, 30, 64]; 
     48    static float[3] radii = [15, 30, 64]; 
    4849     
    49     Element[] m_inside; 
    50     float m_rot_speed; 
    51     uint m_type = NORMAL; 
    52     uint m_nb_gold = 3;  
    53     uint m_nb_energy = 1; 
     50    Element[] inside; 
     51    float rot_speed; 
     52    uint type = NORMAL; 
     53    uint nb_gold = 3;  
     54    uint nb_energy = 1; 
    5455public: 
    5556 
    56     override float danger() {return 0.1 * (m_type + 1);}  
     57    override float danger() {return 0.1 * (type + 1);}  
    5758 
    5859    static void s_init() { 
    59         m_surfs[SMALL] = [ 
     60        surfs[SMALL] = [ 
    6061            Sprite.loadSurfs("data/asteroid/small/small1/small1.png", 8, 8), 
    6162            Sprite.loadSurfs("data/asteroid/small/small2/small2.png", 8, 8) 
    6263        ]; 
    63         m_surfs[NORMAL] = [ 
     64        surfs[NORMAL] = [ 
    6465            Sprite.loadSurfs("data/asteroid/normal/normal1/normal1.png", 8, 8), 
    6566            Sprite.loadSurfs("data/asteroid/normal/normal2/normal2.png", 8, 8) 
    6667        ]; 
    67         m_surfs[BIG] = [ 
     68        surfs[BIG] = [ 
    6869            Sprite.loadSurfs("data/asteroid/big/big1/big1.png", 16, 8) 
    6970        ]; 
    7071         
    71         m_sounds[SMALL] = new Sound("data/asteroid/small/explosion/explosion.wav"); 
    72         m_sounds[NORMAL] = new Sound("data/asteroid/normal/explosion/explosion.wav"); 
    73         m_sounds[BIG] = new Sound("data/asteroid/big/explosion/explosion.wav"); 
     72        sounds[SMALL] = new Sound("data/asteroid/small/explosion/explosion.wav"); 
     73        sounds[NORMAL] = new Sound("data/asteroid/normal/explosion/explosion.wav"); 
     74        sounds[BIG] = new Sound("data/asteroid/big/explosion/explosion.wav"); 
    7475         
    7576        Explosion.s_init(); 
     
    7778         
    7879    this(int type, Vect pos = Vect(0,0), Vect speed = Vect(0,0)) {        
    79         m_type = type; 
     80        this.type = type; 
    8081        super( 
    81             new Sprite(m_surfs[type][randint(0, m_surfs[type].length)]), 
     82            new Sprite(surfs[type][randint(0, surfs[type].length)]), 
    8283            pos 
    8384        ); 
    84         radius = m_radii[type]; 
     85        radius = radii[type]; 
    8586        mass = type + 1; 
    8687        angle = rand(0, 2 * PI); 
    87         m_rot_speed = rand(-0.05, 0.05); 
     88        rot_speed = rand(-0.05, 0.05); 
    8889        this.speed = speed; 
    8990        max_speed = 5; 
    9091         
    9192        if (type > SMALL) { 
    92             for(int i = 0; i < 3; ++i) {m_inside ~= new Asteroid(type - 1);} 
     93            for(int i = 0; i < 3; ++i) {inside ~= new Asteroid(type - 1);} 
    9394        } 
    9495    } 
    95  
     96     
     97    /** 
     98        Add an element inside the asteroid. 
     99         
     100        The element will appear after the asteroid is destroyed. 
     101    */ 
    96102    void put_inside(Element e) { 
    97         assert(!contains!(Element)(m_inside, e)); 
     103        assert(!contains!(Element)(inside, e)); 
    98104         
    99105        bool is_asteroid(in Element ee) { 
    100106            return cast(Asteroid)ee !is null; 
    101107        } 
    102         Asteroid[] asteroids = cast(Asteroid[])filter!(Element)(m_inside, &is_asteroid); 
     108        Asteroid[] asteroids = cast(Asteroid[])filter!(Element)(inside, &is_asteroid); 
    103109         
    104110        int r = randint(-1, asteroids.length); 
    105111        if (r == -1) { 
    106             m_inside ~= e; 
     112            inside ~= e; 
    107113        } 
    108114        else { 
     
    112118     
    113119    override void iter(float dt = 1) { 
    114         angle = angle + dt * m_rot_speed; 
     120        angle = angle + dt * rot_speed; 
    115121        super.iter(dt); 
    116122    } 
     
    122128    override void collide(Element e) { 
    123129        if (cast(Fire)e || cast(Ship)e) { 
    124             m_sounds[m_type].play(); 
     130            sounds[type].play(); 
    125131            if (cast(Fire)e) { 
    126132                Game().score += 1; 
    127133            } 
    128134             
    129             universe.append(new Explosion(m_type, pos, speed), this); 
     135            universe.append(new Explosion(type, pos, speed), this); 
    130136             
    131137            // Add the elements inside 
    132             foreach(ee; m_inside) { 
     138            foreach(ee; inside) { 
    133139                Vect speed = this.speed + Vect.from_angle(rand(0, 4 * PI)) * 1 / sqrt(ee.mass); 
    134140                speed += rand(0.1, 0.2) * sqrt(e.mass / ee.mass) * e.speed; 
     
    149155private: 
    150156    static int NBIMGS = 64; 
    151     static Surface[][3] m_surfs; 
    152     float m_rot_speed; 
     157    static Surface[][3] surfs; 
     158    float rot_speed; 
    153159 
    154160public: 
    155161    static void s_init() { 
    156         m_surfs[Asteroid.SMALL] = Sprite.loadSurfs("data/asteroid/small/explosion/explosion.png", 8, 8); 
    157         m_surfs[Asteroid.NORMAL] = Sprite.loadSurfs("data/asteroid/normal/explosion/explosion.png", 8, 8); 
    158         m_surfs[Asteroid.BIG] = Sprite.loadSurfs("data/asteroid/big/explosion/explosion.png", 8, 8); 
     162        surfs[Asteroid.SMALL] = Sprite.loadSurfs("data/asteroid/small/explosion/explosion.png", 8, 8); 
     163        surfs[Asteroid.NORMAL] = Sprite.loadSurfs("data/asteroid/normal/explosion/explosion.png", 8, 8); 
     164        surfs[Asteroid.BIG] = Sprite.loadSurfs("data/asteroid/big/explosion/explosion.png", 8, 8); 
    159165    } 
    160166 
    161167     
    162168    this(int type, Vect pos, Vect speed = Vect(0,0)) { 
    163         super(new Sprite(m_surfs[type]), pos); 
     169        super(new Sprite(surfs[type]), pos); 
    164170        this.speed = speed; 
    165         m_rot_speed = 0.6 / (type + 1); 
     171        rot_speed = 0.6 / (type + 1); 
    166172    } 
    167173     
    168174    override void iter(float dt = 1) { 
    169         angle = angle + dt * m_rot_speed; 
     175        angle = angle + dt * rot_speed; 
    170176        if (angle >= 2 * PI) {kill();} 
    171177         
  • trunk/guisterax/src/display.d

    r30 r31  
    118118    } 
    119119     
    120 //    static MainDisplay opCall(MainDisplay disp) { 
    121 //        display = disp; 
    122 //        return display; 
    123 //    } 
    124      
    125120    this(int w = 640, int h = 480) { 
    126121        assert(!display); 
  • trunk/guisterax/src/gold.d

    r30 r31  
    5555        radius = 5; 
    5656        mass = 2; 
     57        max_speed = 5; 
    5758        rot_speed = rand(-0.05, 0.05); 
    5859        this.speed = speed; 
     
    8485    } 
    8586}; 
     87 
     88class GoldMagnet : Actor 
     89{ 
     90public: 
     91    float force = 0.1; 
     92    float friction = 0.02; 
     93public: 
     94    override void iter(float dt) { 
     95        Element ship = parent_as!(Element)(); 
     96        Universe universe = universe(); 
     97         
     98        foreach(g; universe.children_as!(Gold)()) { 
     99            g.attract(ship.pos, force, false); 
     100            g.friction = friction; 
     101        } 
     102    } 
     103}; 
     104 
  • trunk/guisterax/src/ship.d

    r29 r31  
    4646{ 
    4747private: 
    48     static Surface[] m_normal_surfs; 
    49     Sprite m_normal_sprite; 
    50     static Surface[] m_power_surfs; 
    51     Sprite m_power_sprite; 
     48    static Surface[] normal_surfs; 
     49    Sprite normal_sprite; 
     50    static Surface[] power_surfs; 
     51    Sprite power_sprite; 
    5252     
    53     float m_angle_speed = 0.1; 
    54     float m_power = 0.1; 
     53    float angle_speed = 0.1; 
     54    float power = 0.1; 
    5555     
    5656public: 
     
    6464 
    6565    static void s_init() { 
    66         m_normal_surfs = Sprite.loadSurfs("data/ship/normal/ship.png", 8, 8); 
    67         m_power_surfs = Sprite.loadSurfs("data/ship/power/power.png", 8, 8); 
     66        normal_surfs = Sprite.loadSurfs("data/ship/normal/ship.png", 8, 8); 
     67        power_surfs = Sprite.loadSurfs("data/ship/power/power.png", 8, 8); 
    6868         
    6969        Debris.s_init(); 
     
    7171 
    7272    this(in Vect pos = Vect()) { 
    73         m_normal_sprite = new Sprite(m_normal_surfs); 
    74         m_power_sprite = new Sprite(m_power_surfs); 
    75         super(m_normal_sprite, pos); 
     73        normal_sprite = new Sprite(normal_surfs); 
     74        power_sprite = new Sprite(power_surfs); 
     75        super(normal_sprite, pos); 
    7676        max_speed = 5; 
    7777        radius = 12; 
     
    111111        if (universe) { 
    112112            if (universe.key(SDLK_UP)) { 
    113                 force += m_power * normal(); 
    114                 sprite = m_power_sprite; 
     113                force += power * normal(); 
     114                sprite = power_sprite; 
    115115            } 
    116116            else { 
    117                 sprite = m_normal_sprite; 
     117                sprite = normal_sprite; 
    118118            } 
    119119             
    120120            if (universe.key(SDLK_LEFT)) { 
    121                 angle = angle + dt * m_angle_speed; 
     121                angle = angle + dt * angle_speed; 
    122122            } 
    123123            if (universe.key(SDLK_RIGHT)) { 
    124                 angle = angle - dt * m_angle_speed; 
     124                angle = angle - dt * angle_speed; 
    125125            } 
    126126             
     
    154154{ 
    155155private: 
    156     static Surface[][2] m_surfs; 
    157     float m_rot_speed; 
     156    static Surface[][2] surfs; 
     157    float rot_speed; 
    158158 
    159159public: 
    160160    static void s_init() { 
    161         m_surfs[0] = Sprite.loadSurfs("data/ship/debris0/debris0.png", 8, 4); 
    162         m_surfs[1] = Sprite.loadSurfs("data/ship/debris1/debris1.png", 8, 4); 
     161        surfs[0] = Sprite.loadSurfs("data/ship/debris0/debris0.png", 8, 4); 
     162        surfs[1] = Sprite.loadSurfs("data/ship/debris1/debris1.png", 8, 4); 
    163163    } 
    164164     
    165165    this(int type, in Vect pos, in Vect speed = Vect(0,0)) { 
    166         super(new Sprite(m_surfs[type]), pos); 
     166        super(new Sprite(surfs[type]), pos); 
    167167        this.speed = speed; 
    168         m_rot_speed = 0.1; 
     168        rot_speed = 0.1; 
    169169    } 
    170170     
    171171    void iter(in float dt = 1) { 
    172         angle = angle + dt * m_rot_speed; 
     172        angle = angle + dt * rot_speed; 
    173173        super.iter(dt); 
    174174    } 
  • trunk/guisterax/src/shop.d

    r16 r31  
    3333import status; 
    3434import menu; 
     35static import gold; 
    3536 
    3637class Product : Entry 
     
    110111}; 
    111112 
     113class GoldMagnet : Product { 
     114public: 
     115    this(in Vect pos = Vect()){super(pos, "GOLD MAGNET", 10);} 
     116    override void on_buy() { 
     117        ship.append(new gold.GoldMagnet()); 
     118    } 
     119} 
     120 
    112121class Energy : Product { 
    113122public: 
     
    151160        menu.append(new LongFire(Vect(0, -dist * 2))); 
    152161        menu.append(new GoldFire(Vect(0, -dist * 3))); 
    153         menu.append(new Energy(Vect(0, -dist * 4))); 
     162        menu.append(new GoldMagnet(Vect(0, -dist * 4))); 
     163        menu.append(new Energy(Vect(0, -dist * 5))); 
    154164        append(menu); 
    155165         
  • trunk/guisterax/src/surface.d

    r23 r31  
    3737class Surface 
    3838{ 
     39public: 
    3940    SDL_Surface* surf = null; 
    4041     
     
    4647    this() {} 
    4748 
    48      
     49    /** 
     50        Create a surface from an image file. 
     51        Params: 
     52            file = the path to the image  
     53    */ 
    4954    this(in char[] file) { 
    5055        // to ensure that the display is created