Changeset 66

Show
Ignore:
Timestamp:
05/18/08 14:24:40 (4 months ago)
Author:
JoeCoder
Message:

Modified nodes to keep track of their children in a self-indexed associative array. This prevents them from having to store their own index in their parent's array.
Made the smoke from the ship in demo1 gradually fade.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/demo1/gameobj.d

    r65 r66  
    3131class Asteroid : GameObject 
    3232{ 
     33    ModelNode rock; 
     34     
    3335    float radius; // cached 
    3436 
    3537    this (BaseNode parent) 
    3638    {   super(parent); 
    37         ModelNode rock = new ModelNode(this); 
     39        rock = new ModelNode(this); 
    3840        rock.setModel(Resource.model("space/asteroid1.ms3d")); 
    3941    } 
     
    4547    void setMass(float mass) 
    4648    {   this.mass = mass; 
    47         children[0].setScale(Vec3f(pow(mass, .33333)/2)); 
     49        rock.setScale(Vec3f(pow(mass, .33333)/2)); 
    4850        radius = pow(mass, .3333)*.75*4; 
    4951    } 
  • trunk/src/demo1/ship.d

    r65 r66  
    7272        { 
    7373            accelerate(Vec3f(0, 0, -speed).rotate(pitch.getTransform()).rotate(getTransform())); 
    74             Vec3f vel = Vec3f(0, 0, -.8).rotate(pitch.getAbsoluteRotation()).scale(getVelocity().length()); 
    7574 
    7675            // Engine smoke 
    7776            SpriteNode puff = new SpriteNode(ship.getScene()); 
    7877            puff.setMaterial(Resource.material("fx/smoke.xml")); 
    79             puff.setLifetime(1); 
     78            puff.setLifetime(5); 
    8079            puff.setScale(Vec3f(.4)); 
    81             //puff.setVelocity(vel); 
     80            //puff.setVelocity(getVelocity() - Vec3f(0, 0, -10).rotate(ship.getAbsoluteTransform())); 
    8281            puff.setPosition(ship.getAbsolutePosition()+Vec3f(.8, 0, 2.5).rotate(ship.getAbsoluteTransform())); 
     82             
     83            void fade(BaseNode self) 
     84            {   SpriteNode node = cast(SpriteNode)self; 
     85                node.setColor(1, 1, 1, node.getLifetime()/5); 
     86                float scale = std.math.sqrt(5.0f)-std.math.sqrt(node.getLifetime()) + .4; 
     87                node.setScale(scale); 
     88                node.setVelocity(node.getVelocity().scale(max(1-delta*ldamp*2, 0.0f))); 
     89            } 
     90            puff.onUpdate(&fade); 
    8391 
    8492            puff = new SpriteNode(ship.getScene(), puff); 
    8593            puff.setPosition(ship.getAbsolutePosition()+Vec3f(-.8, 0, 2.5).rotate(ship.getAbsoluteTransform())); 
    86  
     94             
     95             
     96             
     97             
    8798            sound.play(); 
    8899        } 
  • trunk/src/demo2/gameobj.d

    r65 r66  
    3131class Asteroid : GameObject 
    3232{ 
     33    ModelNode rock; 
     34     
    3335    float radius; // cached 
    3436 
    3537    this (BaseNode parent) 
    3638    {   super(parent); 
    37         ModelNode rock = new ModelNode(this); 
     39        rock = new ModelNode(this); 
    3840        rock.setModel(Resource.model("space/asteroid1.ms3d")); 
    3941    } 
     
    4547    void setMass(float mass) 
    4648    {   this.mass = mass; 
    47         children[0].setScale(Vec3f(pow(mass, .33333)/2)); 
     49        rock.setScale(Vec3f(pow(mass, .33333)/2)); 
    4850        radius = pow(mass, .3333)*.75*4; 
    4951    } 
  • trunk/src/yage/node/base.d

    r64 r66  
    2525    Scene       scene;          // The Scene that this node belongs to. 
    2626    BaseNode    parent; 
    27     Node[]      children; 
    28     int         index = -1;     // index of this node in parent array 
     27    Node[Node]  children;    
    2928 
    3029    protected void delegate(BaseNode self) on_update = null;    // called on update 
     
    4948 
    5049    /// Get an array of this Node's children 
    51     Node[] getChildren() 
     50    Node[Node] getChildren() 
    5251    {   return children; 
    53     } 
    54  
    55     /** 
    56      * Get the index of this Node in its parent's array. 
    57      * Returns -1 if this node is a Scene.*/ 
    58     int getIndex() 
    59     {   return index; 
    6052    } 
    6153 
     
    137129     */  
    138130    void update(float delta) 
    139     {   debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    140      
    141         // Cache the current relative and absolute position/rotation for rendering. 
     131    {   // Cache the current relative and absolute position/rotation for rendering. 
    142132        // This prevents rendering a halfway-updated scenegraph. 
    143133        cache[scene.transform_write].transform = transform; 
     
    148138            on_update(this); 
    149139 
    150         // Iterate in reverse to ensure we hit all of them, since the last item 
    151         // is moved over the current item when removing from an array. 
    152         foreach_reverse(Node c; children) 
     140        foreach(Node c; children) 
    153141            c.update(delta); 
    154142    } 
     
    174162        // could this function be called by two different threads on the same Node? 
    175163        // and then the path gets messed up? 
    176         debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    177  
    178164        BaseNode path[16384] = void; // surely no one will have a scene graph deeper than this! 
    179165        BaseNode node = this; 
  • trunk/src/yage/node/light.d

    r59 r66  
    7575        super.setParent(parent); 
    7676        if (old !is scene) 
    77         {   if ((old !is null) && (index != -1)
     77        {   if ((old !is null)
    7878                old.removeLight(light_index); 
    7979            scene.addLight(this); 
  • trunk/src/yage/node/node.d

    r65 r66  
    7070    /// Construct this Node as a child of parent. 
    7171    this(BaseNode parent) 
    72     {   debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    73         visible = false; 
     72    {   visible = false; 
    7473        scale = Vec3f(1); 
    7574        color = Color("white"); 
     
    8382     * original = This Node will be an exact copy of original.*/ 
    8483    this(BaseNode parent, Node original) 
    85     { 
    86         debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    87         this(parent); 
     84    {   this(parent); 
    8885 
    8986        visible = original.visible; 
     
    9188        color = original.color; 
    9289        lifetime = original.lifetime; 
     90        on_update = original.on_update; 
    9391 
    9492        // From BaseNode 
     
    178176    /// Remove this Node.  This function should be used instead of delete. 
    179177    void remove() 
    180     {   debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    181  
    182         if (index != -1) 
    183         {   yage.core.all.remove(parent.children, index, false); 
    184             if (index < parent.children.length) 
    185                 parent.children[index].index = index; 
    186             index = -1; // so remove can't be called twice. 
    187         } 
     178    {   if (parent && this in parent.children) 
     179            parent.children.remove(this); 
     180         
    188181        // this needs to happen because some children (like lights) may need to do more in their remove() function. 
    189182        foreach(Node c; children) 
     
    198191    in { assert(_parent !is null); 
    199192    }body 
    200     {   debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    201  
    202         if (index!=-1) 
    203         {   yage.core.array.remove(children, index, false); 
    204             if (index < parent.children.length) // if not removed from the end. 
    205                 parent.children[index].index = index; // update external index. 
    206         }// Add to new parent 
     193    {   if (parent && this in parent.children) 
     194            parent.children.remove(this); 
     195         
     196        // Add to new parent 
    207197        parent = _parent; 
    208         parent.children ~= this; 
    209         index = parent.children.length-1; 
     198        parent.children[this] = this; 
    210199        scene = parent.scene; 
    211200        setTransformDirty(); 
     
    219208     * It normally doesn't need to be called manually.*/ 
    220209    void update(float delta) 
    221     {   debug scope( failure ) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    222  
    223         lifetime-= delta; 
     210    {   lifetime-= delta; 
    224211 
    225212        // Move by linear velocity if not zero. 
     
    248235     * Also perhaps use axis sorting for faster calculations. */ 
    249236    void enableLights(ubyte number=8) 
    250     {   debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    251  
     237    {    
    252238        if (number>Device.getLimit(DEVICE_MAX_LIGHTS)) 
    253239            number = Device.getLimit(DEVICE_MAX_LIGHTS);