Exemplo n.º 1
0
        void UpdateHUD()
        {
            Camera camera = RendererWorld.Instance.DefaultCamera;

            hudControl.Visible = Map.Instance.DrawGui;

            //Selected units bounds
            if( taskTargetBuildMeshObject == null )
            {
                Vec3 mouseMapPos = Vec3.Zero;
                Unit mouseOnObject = null;

                bool pickingSuccess = false;

                if( !EngineApp.Instance.MouseRelativeMode )
                {
                    Ray ray = camera.GetCameraToViewportRay( EngineApp.Instance.MousePosition );
                    if( !float.IsNaN( ray.Direction.X ) )
                    {
                        RayCastResult result = PhysicsWorld.Instance.RayCast( ray,
                            (int)ContactGroup.CastOnlyContact );
                        if( result.Shape != null )
                        {
                            pickingSuccess = true;
                            mouseOnObject = MapSystemWorld.GetMapObjectByBody( result.Shape.Body ) as Unit;
                            mouseMapPos = result.Position;
                        }
                    }

                    if( selectMode && selectDraggedMouse )
                    {
                        Rect rect = new Rect( selectStartPos );
                        rect.Add( EngineApp.Instance.MousePosition );

                        Map.Instance.GetObjectsByScreenRectangle( rect, GameFilterGroups.UnitFilterGroup,
                            delegate( MapObject obj )
                            {
                                Unit unit = (Unit)obj;

                                camera.DebugGeometry.Color = new ColorValue( 1, 1, 0 );
                                Bounds bounds = obj.MapBounds;
                                bounds.Expand( .1f );
                                camera.DebugGeometry.AddBounds( bounds );
                            } );
                    }
                    else
                    {
                        if( pickingSuccess && IsMouseInActiveArea() )
                        {
                            if( mouseOnObject != null )
                            {
                                camera.DebugGeometry.Color = new ColorValue( 1, 1, 0 );

                                Bounds bounds = mouseOnObject.MapBounds;
                                bounds.Expand( .1f );
                                camera.DebugGeometry.AddBounds( bounds );
                            }
                            else
                            {
                                camera.DebugGeometry.Color = new ColorValue( 1, 0, 0 );
                                camera.DebugGeometry.AddSphere( new Sphere( mouseMapPos, .4f ), 16 );
                            }
                        }
                    }
                }

                //objects selected
                foreach( Unit unit in selectedUnits )
                {
                    ColorValue color;

                    if( playerFaction == null || unit.Intellect == null || unit.Intellect.Faction == null )
                        color = new ColorValue( 1, 1, 0 );
                    else if( playerFaction == unit.Intellect.Faction )
                        color = new ColorValue( 0, 1, 0 );
                    else
                        color = new ColorValue( 1, 0, 0 );

                    camera.DebugGeometry.Color = color;
                    camera.DebugGeometry.AddBounds( unit.MapBounds );
                }
            }

            //taskTargetBuild
            if( taskTargetBuildMeshObject != null )
            {
                taskTargetBuildSceneNode.Visible = false;

                Ray ray = new Ray( Vec3.Zero, Vec3.Zero );

                //pick on active area
                if( IsMouseInActiveArea() )
                    ray = camera.GetCameraToViewportRay( EngineApp.Instance.MousePosition );

                //pick on minimap
                if( minimapControl.GetScreenRectangle().IsContainsPoint( MousePosition ) )
                {
                    Vec2 p = GetMapPositionByMouseOnMinimap();
                    ray = new Ray( new Vec3( p.X, p.Y, 1000 ), new Vec3( .001f, .001f, -2000 ) );
                }

                if( ray.Direction != Vec3.Zero )
                {
                    RayCastResult result = PhysicsWorld.Instance.RayCast( ray,
                        (int)ContactGroup.CastOnlyCollision );
                    if( result.Shape != null )
                    {
                        Vec3 mouseMapPos = result.Position;

                        //snap
                        mouseMapPos += new Vec3( .5f, .5f, 0 );
                        mouseMapPos = new Vec3( (int)mouseMapPos.X, (int)mouseMapPos.Y, (int)mouseMapPos.Z );

                        //RTSMine specific
                        bool mineFound = false;
                        if( taskTargetBuildingType is RTSMineType )
                        {
                            Bounds bounds = new Bounds( mouseMapPos - new Vec3( 2, 2, 2 ),
                                mouseMapPos + new Vec3( 2, 2, 2 ) );
                            Map.Instance.GetObjects( bounds, delegate( MapObject obj )
                            {
                                if( obj.Type.Name == "RTSGeyser" )
                                {
                                    mineFound = true;
                                    mouseMapPos = obj.Position;
                                }
                            } );
                        }

                        taskTargetBuildSceneNode.Position = mouseMapPos;
                        taskTargetBuildSceneNode.Visible = true;

                        //check free for build
                        bool free = IsFreeForBuildTaskTargetBuild( mouseMapPos );

                        //RTSMine specific
                        if( taskTargetBuildingType is RTSMineType )
                            if( !mineFound )
                                free = false;

                        foreach( MeshObject.SubObject subMesh in taskTargetBuildMeshObject.SubObjects )
                            subMesh.MaterialName = free ? "Green" : "Red";
                    }
                }
            }

            //Selected units HUD
            {
                string text = "";

                if( selectedUnits.Count > 1 )
                {
                    foreach( Unit unit in selectedUnits )
                        text += unit.ToString() + "\n";
                }

                if( selectedUnits.Count == 1 )
                {
                    Unit unit = selectedUnits[ 0 ];

                    text += unit.ToString() + "\n";
                    text += "\n";
                    text += string.Format( "Life: {0}/{1}\n", unit.Life, unit.Type.LifeMax );

                    text += "Intellect:\n";
                    if( unit.Intellect != null )
                    {
                        text += string.Format( "- {0}\n", unit.Intellect.ToString() );
                        FactionType faction = unit.Intellect.Faction;
                        text += string.Format( "- Faction: {0}\n", faction != null ? faction.ToString() : "null" );

                        RTSUnitAI rtsUnitAI = unit.Intellect as RTSUnitAI;
                        if( rtsUnitAI != null )
                        {
                            text += string.Format( "- CurrentTask: {0}\n", rtsUnitAI.CurrentTask.ToString() );
                        }
                    }
                    else
                        text += string.Format( "- null\n" );

                }

                hudControl.Controls[ "SelectedUnitsInfo" ].Text = text;

                UpdateControlPanel();
            }

            //RTSFactionManager
            {
                string text = "";

                if( RTSFactionManager.Instance != null )
                {
                    foreach( RTSFactionManager.FactionItem item in RTSFactionManager.Instance.Factions )
                    {
                        string s = "  " + item.ToString();
                        s += ", Money " + ( (int)item.Money ).ToString();
                        if( item.FactionType == playerFaction )
                            s += " (Player)";
                        text += s + "\n";
                    }
                }
                else
                    text += "RTSFactionManager not exists\n";

                hudControl.Controls[ "DebugText" ].Text = text;
            }

            UpdateHUDControlIcon();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates objects on which the player can to operate.
        /// Such as which the player can supervise switches, ingameGUI or control units.
        /// </summary>
        void UpdateCurrentPlayerUseObjects()
        {
            Camera camera = RendererWorld.Instance.DefaultCamera;

            Unit playerUnit = GetPlayerUnit();

            float maxDistance = ( GetRealCameraType() == CameraType.FPS ) ?
                playerUseDistance : playerUseDistanceTPS;

            Ray ray = camera.GetCameraToViewportRay( new Vec2( .5f, .5f ) );
            ray.Direction = ray.Direction.GetNormalize() * maxDistance;

            //currentAttachedGuiObject
            {
                MapObjectAttachedGui attachedGuiObject = null;
                Vec2 screenPosition = Vec2.Zero;

                if( GetRealCameraType() != CameraType.Free && !IsCutSceneEnabled() &&
                    EntitySystemWorld.Instance.Simulation )
                {
                    Map.Instance.GetObjectsAttachedGuiObject( ray,
                        out attachedGuiObject, out screenPosition );
                }

                //ignore empty gui objects
                if( attachedGuiObject != null )
                {
                    In3dControlManager manager = attachedGuiObject.ControlManager;

                    if( manager.Controls.Count == 0 ||
                        ( manager.Controls.Count == 1 && !manager.Controls[ 0 ].Enable ) )
                    {
                        attachedGuiObject = null;
                    }
                }

                if( attachedGuiObject != currentAttachedGuiObject )
                {
                    if( currentAttachedGuiObject != null )
                        currentAttachedGuiObject.ControlManager.LostManagerFocus();
                    currentAttachedGuiObject = attachedGuiObject;
                }

                if( currentAttachedGuiObject != null )
                    currentAttachedGuiObject.ControlManager.DoMouseMove( screenPosition );
            }

            //currentFloatSwitch
            {
                GameEntities.Switch overSwitch = null;

                Map.Instance.GetObjects( ray, delegate( MapObject obj, float scale )
                {
                    GameEntities.Switch s = obj as GameEntities.Switch;
                    if( s != null )
                    {
                        if( s.UseAttachedMesh != null )
                        {
                            Bounds bounds = ( (MapObjectAttachedMesh)s.UseAttachedMesh ).SceneNode.
                                GetWorldBounds();

                            if( bounds.RayIntersection( ray ) )
                            {
                                overSwitch = s;
                                return false;
                            }
                        }
                        else
                        {
                            overSwitch = s;
                            return false;
                        }
                    }

                    return true;
                } );

                //draw border
                if( overSwitch != null )
                {
                    camera.DebugGeometry.Color = new ColorValue( 1, 1, 1 );
                    if( overSwitch.UseAttachedMesh != null )
                    {
                        camera.DebugGeometry.AddBounds( overSwitch.UseAttachedMesh.SceneNode.
                            GetWorldBounds() );
                    }
                    else
                        camera.DebugGeometry.AddBounds( overSwitch.MapBounds );
                }

                if( overSwitch != currentSwitch )
                {
                    FloatSwitch floatSwitch = currentSwitch as FloatSwitch;
                    if( floatSwitch != null )
                        floatSwitch.UseEnd();

                    currentSwitch = overSwitch;
                }
            }

            //Use player control unit
            if( playerUnit != null )
            {
                currentSeeUnitAllowPlayerControl = null;

                if( PlayerIntellect.Instance != null &&
                    PlayerIntellect.Instance.MainNotActiveUnit == null &&
                    GetRealCameraType() != CameraType.Free )
                {
                    Ray unitFindRay = ray;

                    //special ray for TPS camera
                    if( GetRealCameraType() == CameraType.TPS )
                    {
                        unitFindRay = new Ray( playerUnit.Position,
                            playerUnit.Rotation * new Vec3( playerUseDistance, 0, 0 ) );
                    }

                    Map.Instance.GetObjects( unitFindRay, delegate( MapObject obj, float scale )
                    {
                        Dynamic dynamic = obj as Dynamic;

                        if( dynamic == null )
                            return true;

                        if( !dynamic.Visible )
                            return true;

                        Unit u = dynamic.GetParentUnit();
                        if( u == null )
                            return true;

                        if( u == GetPlayerUnit() )
                            return true;

                        if( !u.Type.AllowPlayerControl )
                            return true;

                        if( u.Intellect != null )
                            return true;

                        if( !u.MapBounds.RayIntersection( unitFindRay ) )
                            return true;

                        currentSeeUnitAllowPlayerControl = u;

                        return false;
                    } );
                }

                //draw border
                if( currentSeeUnitAllowPlayerControl != null )
                {
                    camera.DebugGeometry.Color = new ColorValue( 1, 1, 1 );
                    camera.DebugGeometry.AddBounds( currentSeeUnitAllowPlayerControl.MapBounds );
                }
            }

            //draw "Press Use" text
            if( currentSwitch != null || currentSeeUnitAllowPlayerControl != null )
            {
                ColorValue color;
                if( ( Time % 2 ) < 1 )
                    color = new ColorValue( 1, 1, 0 );
                else
                    color = new ColorValue( 0, 1, 0 );

                string text;
                {
                    text = "Press \"Use\"";

                    //get binded keyboard key or mouse button
                    GameControlsManager.GameControlItem controlItem = GameControlsManager.Instance.
                        GetItemByControlKey( GameControlKeys.Use );
                    if( controlItem != null && controlItem.DefaultKeyboardMouseValues.Length != 0 )
                    {
                        GameControlsManager.SystemKeyboardMouseValue value =
                            controlItem.DefaultKeyboardMouseValues[ 0 ];
                        text += string.Format( " ({0})", value.ToString() );
                    }
                }

                EngineApp.Instance.ScreenGuiRenderer.AddText( text, new Vec2( .5f, .9f ),
                    HorizontalAlign.Center, VerticalAlign.Center, color );
            }
        }