Changeset 16

Show
Ignore:
Timestamp:
12/08/07 05:30:38 (1 year ago)
Author:
charlie137
Message:

Fixed bug that triggered a segfault in a very special case

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/guisterax/Makefile

    r10 r16  
    88     
    99debug: src/*.d 
    10     gdc -fdebug -o guisterax src/*.d src/derelict/util/*.d src/derelict/sdl/*.d -Isrc/ -Isrc/derelict -ldl 
     10    gdc -fdebug -g -o guisterax src/*.d src/derelict/util/*.d src/derelict/sdl/*.d -Isrc/ -Isrc/derelict -ldl 
    1111     
    1212clean: 
  • trunk/guisterax/src/game.d

    r10 r16  
    117117        uint level = 1; 
    118118        while(true) { 
    119             m_ship.parent = null; 
    120119            shop(); 
    121120            wave(level); 
  • trunk/guisterax/src/init.d

    r10 r16  
    3939import display : MainDisplay; 
    4040import vect : Vect; 
     41import status; 
    4142 
    4243void init() { 
     
    5556    Energy.s_init(); 
    5657    Ship.s_init(); 
     58    Status.s_init(); 
    5759    Shop.s_init(); 
    5860} 
  • trunk/guisterax/src/ship.d

    r10 r16  
    4343import utils; 
    4444 
    45  
    46 class EnergyLevel : Actor 
    47 { 
    48 private: 
    49     // float m_value = 1; 
    50     float m_target_value; 
    51     float m_speed = 0.05; 
    52     float m_max = 1; 
    53 public: 
    54     float value; 
    55      
    56 public: 
    57     //float value() {return m_value;} 
    58      
    59     this(float v = 1) { 
    60         super(); 
    61         value = m_target_value = v; 
    62     } 
    63      
    64     override void iter(float dt) { 
    65         if (abs(m_target_value - value) < m_speed * dt) { 
    66             value = m_target_value; 
    67             return; 
    68         } 
    69         value += (m_target_value > value)? m_speed*dt: -m_speed*dt; 
    70     } 
    71      
    72     EnergyLevel opAddAssign(float v) { 
    73         m_target_value += v; 
    74         if (m_target_value < 0) {m_target_value = 0;} 
    75         if (m_target_value > m_max) {m_target_value = m_max;} 
    76         return this; 
    77     } 
    78     EnergyLevel opSubAssign(float value) {return (this += -value);} 
    79      
    80     invariant() { 
    81         assert(value >= 0); 
    82         assert(value <= m_max); 
    83     } 
    84      
    85 }; 
    86  
    87 class Status : Actor 
    88 { 
    89 private:     
    90     static Surface[] m_surfs; 
    91     Sprite m_sprite; 
    92 public: 
    93     Ship ship; 
    94      
    95     static void s_init() { 
    96         m_surfs = Sprite.loadSurfs("data/status/life.png", 8, 16); 
    97     } 
    98  
    99     this(Ship ship) { 
    100         m_sprite = new Sprite(m_surfs); 
    101         m_sprite.index = cast(int)(ship.energy * (m_sprite.length - 1)); 
    102         this.ship = ship; 
    103         super(); 
    104     } 
    105      
    106     void draw_energy(Display disp, in Vect pos) { 
    107         m_sprite.draw(disp, pos); 
    108     } 
    109      
    110     void draw_gold(Display disp, in Vect pos) { 
    111         uint gold = ship.gold; 
    112         Font.font.draw(disp, format("GOLD:%d", gold), pos); 
    113     } 
    114      
    115     override void draw(Display disp, in Vect pos) { 
    116         draw_energy(disp, Vect(60,43)); 
    117         draw_gold(disp, Vect(10,15)); 
    118         super.draw(disp, pos); 
    119     } 
    120      
    121     override void iter(float dt) { 
    122         m_sprite.index = cast(int)(ship.energy * (m_sprite.length - 1)); 
    123     } 
    124 }; 
    125  
    126  
    12745class Ship : Element 
    12846{ 
     
    13654    float m_power = 0.1; 
    13755     
    138     EnergyLevel m_energy; 
    139      
    14056public: 
    14157    // public attributes 
    14258    Gun gun; 
    14359    uint gold = 0; 
     60    float energy = 1; 
    14461     
    14562public: 
    14663    alias fire.Fire Fire; 
    147  
    148     float energy() {return m_energy.value;} 
    14964 
    15065    static void s_init() { 
     
    15368         
    15469        Debris.s_init(); 
    155         Status.s_init(); 
    15670    } 
    15771 
     
    16579        gun = new Gun(); 
    16680        append(gun); 
    167          
    168         m_energy = new EnergyLevel(); 
    169         append(m_energy); 
    17081    } 
    17182     
     
    18596     
    18697    override void hit(float v) { 
    187         m_energy -= v; 
     98        energy -= v; 
     99        if (energy < 0) {energy = 0;} 
     100        if (energy > 1) {energy = 1;} 
    188101    } 
    189102     
    190103    override void collide(Element e) { 
    191104        if (cast(Asteroid)e || cast(Metal)e || cast(Mine)e) { 
    192             m_energy -= e.radius / 100
     105            hit(e.radius / 100)
    193106            repeal(e.pos, e.mass, true); 
    194107        } 
     108        // TODO: put this into Energy class 
    195109        if (cast(Energy)e) { 
    196             m_energy += 0.1
     110            hit(-0.1)
    197111        } 
    198112    } 
     
    222136            } 
    223137             
    224             if (m_energy.value <= 0) { 
     138            if (energy <= 0) { 
    225139                for (int type=0; type < 2; ++type) { 
    226140                    Vect speed = rand(0, 1) * Vect.from_angle(rand(0, 2 * PI)) + this.speed; 
     
    234148    } 
    235149 
     150    invariant { 
     151        assert(energy >= 0); 
     152        assert(energy <= 1); 
     153    } 
    236154}; 
    237155 
  • trunk/guisterax/src/shop.d

    r10 r16  
    3131import font; 
    3232import timer; 
     33import status; 
    3334import menu; 
    3435 
     
    155156        append(new Status(ship)); 
    156157    } 
    157      
    158     override void iter(float dt) { 
    159         super.iter(dt); 
    160         ship.iter(dt); 
    161     } 
    162158 
    163159    void ok() { 
  • trunk/guisterax/src/surface.d

    r13 r16  
    6565    ~this() { 
    6666        if (surf) SDL_FreeSurface(surf); 
     67        surf = null; 
    6768    } 
    6869}; 
  • trunk/guisterax/src/wave.d

    r14 r16  
    3333import saucer; 
    3434import timer; 
     35import status; 
    3536import utils; 
    3637