- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/DXGfxLib/SceneObject.cs

Go to the documentation of this file.

    00001 //BSD License
    00002 //DXGfx® - http://www.eteractions.com
    00003 //Copyright (c) 2005
    00004 //by Guillaume Randon
    00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    00006 //and associated documentation files (the "Software"), to deal in the Software without restriction,
    00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    00009 //subject to the following conditions:
    00010 //The above copyright notice and this permission notice shall be included in all copies or substantial
    00011 //portions of the Software.
    00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    00017 using System;
    00018 using System.Collections.Generic;
    00019 using System.Text;
    00020 using System.Diagnostics;
    00021 using System.ComponentModel;
    00022 using System.Drawing;
    00023 using System.Drawing.Design;
    00024 using System.Windows.Forms;
    00025 using Microsoft.DirectX;
    00026 using Microsoft.DirectX.Direct3D;
    00027 using Microsoft.Samples.DirectX.UtilityToolkit;
    00028
    00029 namespace DXGfxLib
    00030 {
    00031     public class SceneObject : SceneNode, IDisposable, IComparable, IDrawable, IAmScriptable
    00032     {
    00037         private bool hasBeenClicked = false;
    00038
    00043         public bool HasBeenClicked
    00044         {
    00045             get { return hasBeenClicked; }
    00046             set { hasBeenClicked = value; }
    00047         }
    00048
    00054         private bool colliding = false;
    00055
    00060         private SceneObject collidingObject = null;
    00061
    00065         protected DrawableBBox drawBox;
    00066
    00070         protected bool drawBoundingBox = false;
    00071
    00075         public bool DrawBoundingBox
    00076         {
    00077             get { return drawBoundingBox; }
    00078             set { drawBoundingBox = value; }
    00079         }
    00080
    00084         protected bool canBeClicked = false;
    00085
    00089         public bool CanBeClicked
    00090         {
    00091             get { return canBeClicked; }
    00092             set { canBeClicked = value; }
    00093         }
    00094
    00100         protected bool checkForCollision = false;
    00101
    00107         public bool CheckForCollision
    00108         {
    00109             get { return checkForCollision; }
    00110             set { checkForCollision = value; }
    00111         }
    00112
    00119         protected bool solid = false;
    00120
    00127         public bool Solid
    00128         {
    00129             get { return solid; }
    00130             set { solid = value; }
    00131         }
    00132
    00137         public string fileName;
    00138
    00139         //[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
    00140         public string Name
    00141         {
    00142             get { return fileName; }
    00143         }
    00144
    00149         protected EffectGroup associatedEffectGroup = null;
    00150
    00154         [System.ComponentModel.BrowsableAttribute(false)]
    00155         public EffectGroup AssociatedEffectGroup
    00156         {
    00157             get { return associatedEffectGroup; }
    00158             set { associatedEffectGroup = value; }
    00159         }
    00160
    00164         protected bool hide = false;
    00165
    00169         public bool Hide
    00170         {
    00171             get { return hide; }
    00172             set { hide = value; }
    00173         }
    00174
    00175
    00182         public BBox localBoundingBox = null;
    00183
    00189         public SceneObject()
    00190         {
    00191             Initialize();
    00192         }
    00193
    00194         public override void Initialize()
    00195         {
    00196             base.Initialize();
    00197
    00198             localBoundingBox = new BBox();
    00199
    00200             associatedEffectGroup = DXGfxManager.GetGlobalInstance().defaultEffectGroup;
    00201
    00202             events = new List<Event>();
    00203         }
    00204
    00213         public override void Dispose()
    00214         {
    00215         }
    00216
    00224         public virtual void LoadFromFile(Device d3ddevice, string fileName)
    00225         {
    00226         }
    00227
    00235         public override void Update(Frustrum frustrum, double appTime, float elapsedTime)
    00236         {
    00237             if (moved)
    00238             {
    00239                 worldAABBox = new AABBox(BBox.Transform(localBoundingBox, WorldMat));
    00240                 moved = false;
    00241             }
    00242
    00243             foreach (SceneNode child in children)
    00244             {
    00245                 child.Update(frustrum, appTime, elapsedTime);
    00246                 worldAABBox.BoxFromBoxes(child.worldAABBox);
    00247             }
    00248         }
    00249
    00257         public override void AttachForRendering(Frustrum frustrum, List<IDrawable> objectsToBeRendered)
    00258         {
    00259             // If we are in the frustrum we attach for rendering an let's our children decide for
    00260             // themselves.
    00261             if (useFrustrumCulling)
    00262             {
    00263                 if (!frustrum.IsInFrustrum(worldAABBox))
    00264                     return;
    00265             }
    00266
    00267             objectsToBeRendered.Add(this);
    00268
    00269             foreach (SceneNode child in children)
    00270             {
    00271                 child.AttachForRendering(frustrum, objectsToBeRendered);
    00272             }
    00273         }
    00274
    00280         public BBox CalculateLocalBBox(Mesh meshToUse)
    00281         {
    00282             return AABBox.CalculateLocalBBox(meshToUse);
    00283         }
    00284
    00289         public override void DetachAll()
    00290         {
    00291             base.DetachAll();
    00292         }
    00293
    00294         public virtual void SetEffectsValues(Device d3ddevice, Effect eff, int pass)
    00295         {
    00296         }
    00297
    00303         public virtual void Draw(Device device)
    00304         {
    00305             device.SetTransform(TransformType.World, worldMat);
    00306
    00307             if (drawBoundingBox)
    00308             {
    00309                 if (drawBox == null)
    00310                 {
    00311                     drawBox = new DrawableBBox(device, localBoundingBox);
    00312                 }
    00313
    00314                 drawBox.Draw(device);
    00315             }
    00316         }
    00317
    00327         public virtual bool Intersect(Vector3 rayPos, Vector3 rayDir, out IntersectInformation closestHit)
    00328         {
    00329             closestHit = new IntersectInformation() ;
    00330             return false;
    00331         }
    00332
    00341         public override bool Intersect(Vector3 rayOri, Vector3 rayDir, ref List<SceneObject> collidingSceneObjects)
    00342         {
    00343             return false;
    00344         }
    00345
    00355         public override bool Intersect(Vector3 rayOri, Vector3 rayDir, ref List<SceneObject> collidingSceneObjects, System.Type desiredType)
    00356         {
    00357             return false;
    00358         }
    00359
    00367         public virtual int CompareTo(Object obj)
    00368         {
    00369             int ret = 1;
    00370             IDrawable drawable = obj as IDrawable;
    00371
    00372             if (drawable != null)
    00373             {
    00374                 ret = AssociatedEffectGroup.CompareTo(drawable.AssociatedEffectGroup);
    00375             }
    00376             return ret;
    00377         }
    00378
    00386         public virtual int CompareTo(SceneObject sceneObj)
    00387         {
    00388             return AssociatedEffectGroup.CompareTo(sceneObj.AssociatedEffectGroup);
    00389         }
    00390
    00398         public virtual int CompareTo(IDrawable sceneObj)
    00399         {
    00400             return AssociatedEffectGroup.CompareTo(sceneObj.AssociatedEffectGroup);
    00401         }
    00402
    00406         protected Matrix lastValidWorldMat = Matrix.Identity;
    00407
    00417         public override void OnMove()
    00418         {
    00419             base.OnMove();
    00420
    00421             if (checkForCollision)
    00422             {
    00423                 List<SceneObject> collidingObjects = new List<SceneObject>();
    00424
    00425                 if (scene.CollideSolid(this, ref collidingObjects))
    00426                 {
    00427                     double appTime = FrameworkTimer.GetTime();
    00428                     float elapsedTime = (float)FrameworkTimer.GetElapsedTime();
    00429
    00430                     foreach (SceneObject sceneObj in collidingObjects)
    00431                     {
    00432                         Collision(appTime, elapsedTime, sceneObj);
    00433                         sceneObj.Collision(appTime, elapsedTime, this);
    00434                     }
    00435
    00436                     if (solid)
    00437                     {
    00438                         MathUtil.CopyMatrixValues(worldMat, lastValidWorldMat);
    00439                     }
    00440                     else
    00441                     {
    00442                         MathUtil.CopyMatrixValues(lastValidWorldMat, worldMat);
    00443                     }
    00444                 }
    00445                 else
    00446                 {
    00447                     MathUtil.CopyMatrixValues(lastValidWorldMat, worldMat);
    00448                 }
    00449             }
    00450             else
    00451             {
    00452                 MathUtil.CopyMatrixValues(lastValidWorldMat, worldMat);
    00453             }
    00454         }
    00455
    00460         internal Script script = null;
    00461
    00465         internal Script OurScript
    00466         {
    00467             get { return script; }
    00468         }
    00469
    00474         protected TimeLine timeline = null;
    00475
    00480         public void SetScript(Script script)
    00481         {
    00482             this.script = script;
    00483             this.script.SetParent(this);
    00484             this.script.SetScene(scene);
    00485         }
    00486
    00491         public Script GetScript()
    00492         {
    00493             if (script == null)
    00494             {
    00495                 SetScript(new Script());
    00496             }
    00497             return script;
    00498         }
    00499
    00504         public void SetTimeLine(TimeLine timeLine)
    00505         {
    00506             this.timeline = timeLine;
    00507             this.timeline.SetParent(this);
    00508             this.timeline.SetScene(scene);
    00509         }
    00510
    00515         public TimeLine GetTimeLine()
    00516         {
    00517             if (timeline == null)
    00518             {
    00519                 SetTimeLine(new TimeLine());
    00520             }
    00521             return timeline;
    00522         }
    00523
    00534         public virtual void Evaluate(double appTime, float elapsedTime)
    00535         {
    00536             if (timeline != null)
    00537             {
    00538                 timeline.Evaluate(appTime, elapsedTime);
    00539             }
    00540
    00541             if (script == null)
    00542             {
    00543                 return;
    00544             }
    00545
    00546
    00547             // Also here we should check a flag to know whether we have been clicked or not and if we have
    00548             // we should also trigger the appropriate scripts !
    00549             if(hasBeenClicked)
    00550             {
    00551                 script.Clicked(appTime, elapsedTime);
    00552             }
    00553             // Ok Now check for collision and execute appropriate script if there are any collision.
    00554             //if (colliding)
    00555             //{
    00556             //    if (collidingObject != null)
    00557             //    {
    00558             //        script.Collision(appTime, elapsedTime, collidingObject);
    00559             //    }
    00560             //}
    00561
    00562             script.Evaluate(appTime, elapsedTime);
    00563         }
    00564
    00569         public void Reset()
    00570         {
    00571             if (script != null)
    00572             {
    00573                 script.Reset();
    00574             }
    00575             if (timeline != null)
    00576             {
    00577                 timeline.Reset();
    00578             }
    00579         }
    00580
    00581
    00587         private List<Event> events = null;
    00588
    00593         public void AddEvent(Event evt)
    00594         {
    00595             events.Add(evt);
    00596         }
    00597
    00602         public void RemoveEvent(Event evt)
    00603         {
    00604             events.Remove(evt);
    00605         }
    00606
    00613         public virtual string[] ListFileRessources()
    00614         {
    00615             // By default return fileName and the effect files associated with the effectGroup
    00616             string[] list = new string[2];
    00617
    00618             list[0] = fileName;
    00619             list[1] = associatedEffectGroup.DefaultFile;
    00620
    00621             return list;
    00622         }
    00623
    00629         public virtual void Clicked(double appTime, float elapsedTime)
    00630         {
    00631             if (script != null)
    00632             {
    00633                 script.Clicked(appTime, elapsedTime);
    00634             }
    00635         }
    00636
    00644         public virtual void Collision(double appTime, float elapsedTime, SceneObject collidingObject)
    00645         {
    00646             if (script != null)
    00647             {
    00648                 script.Collision(appTime, elapsedTime, collidingObject);
    00649             }
    00650         }
    00651
    00663         public Event[] popEventsByName(string name)
    00664         {
    00665             List<Event> ret = new List<Event>();
    00666
    00667             foreach (Event evt in events)
    00668             {
    00669                 if (evt.name.Equals(name))
    00670                 {
    00671                     ret.Add(evt);
    00672                     events.Remove(evt);
    00673                 }
    00674             }
    00675
    00676             return ret.ToArray();
    00677         }
    00678
    00690         public Event[] popEventsByType(string type)
    00691         {
    00692             List<Event> ret = new List<Event>();
    00693
    00694             foreach (Event evt in events)
    00695             {
    00696                 if (evt.type.Equals(type))
    00697                 {
    00698                     ret.Add(evt);
    00699                     events.Remove(evt);
    00700                 }
    00701             }
    00702
    00703             return ret.ToArray();
    00704         }
    00705
    00717         public Event[] peekEventsByName(string name)
    00718         {
    00719             List<Event> ret = new List<Event>();
    00720
    00721             foreach (Event evt in events)
    00722             {
    00723                 if (evt.name.Equals(name))
    00724                 {
    00725                     ret.Add(evt);
    00726                 }
    00727             }
    00728
    00729             return ret.ToArray();
    00730         }
    00731
    00743         public Event[] peekEventsByType(string type)
    00744         {
    00745             List<Event> ret = new List<Event>();
    00746
    00747             foreach (Event evt in events)
    00748             {
    00749                 if (evt.type.Equals(type))
    00750                 {
    00751                     ret.Add(evt);
    00752                 }
    00753             }
    00754
    00755             return ret.ToArray();
    00756         }
    00757     }
    00758 }