Exemplo n.º 1
0
        void DoEndSelectMode()
        {
            selectMode = false;

            List<Unit> areaObjs = new List<Unit>();
            {
                if( selectDraggedMouse )
                {
                    Rect rect = new Rect( selectStartPos );
                    rect.Add( EngineApp.Instance.MousePosition );

                    Map.Instance.GetObjectsByScreenRectangle( rect, GameFilterGroups.UnitFilterGroup,
                        delegate( MapObject obj )
                        {
                            Unit unit = (Unit)obj;
                            areaObjs.Add( unit );
                        } );
                }
                else
                {
                    Ray ray = RendererWorld.Instance.DefaultCamera.GetCameraToViewportRay(
                        EngineApp.Instance.MousePosition );

                    RayCastResult result = PhysicsWorld.Instance.RayCast( ray,
                        (int)ContactGroup.CastOnlyContact );
                    if( result.Shape != null )
                    {
                        Unit unit = MapSystemWorld.GetMapObjectByBody( result.Shape.Body ) as Unit;
                        if( unit != null )
                            areaObjs.Add( unit );
                    }
                }
            }

            //do select/unselect

            if( !EngineApp.Instance.IsKeyPressed( EKeys.Shift ) )
                ClearEntitySelection();

            if( areaObjs.Count == 0 )
                return;

            if( !selectDraggedMouse && EngineApp.Instance.IsKeyPressed( EKeys.Shift ) )
            {
                //unselect
                foreach( Unit obj in areaObjs )
                {
                    if( selectedUnits.Contains( obj ) )
                    {
                        SetEntitySelected( obj, false );
                        return;
                    }
                }
            }

            bool needFactionSetted = false;
            FactionType needFaction = null;

            if( selectedUnits.Count == 0 && playerFaction != null )
            {
                foreach( Unit obj in areaObjs )
                {
                    FactionType objFaction = null;
                    if( obj.Intellect != null )
                        objFaction = obj.Intellect.Faction;

                    if( playerFaction == objFaction )
                    {
                        needFactionSetted = true;
                        needFaction = playerFaction;
                        break;
                    }
                }
            }

            if( selectedUnits.Count != 0 )
            {
                needFactionSetted = true;
                needFaction = null;
                if( selectedUnits[ 0 ].Intellect != null )
                    needFaction = selectedUnits[ 0 ].Intellect.Faction;
            }

            if( !needFactionSetted && selectedUnits.Count == 0 )
            {
                needFactionSetted = true;
                needFaction = null;
                if( areaObjs[ 0 ].Intellect != null )
                    needFaction = areaObjs[ 0 ].Intellect.Faction;
            }

            foreach( Unit obj in areaObjs )
            {
                FactionType objFaction = null;
                if( obj.Intellect != null )
                    objFaction = obj.Intellect.Faction;

                if( needFaction != objFaction )
                    continue;

                if( selectedUnits.Count != 0 && needFaction != playerFaction )
                    break;

                SetEntitySelected( obj, true );
            }
        }
Exemplo n.º 2
0
        void DrawHUD( GuiRenderer renderer )
        {
            if( selectMode && selectDraggedMouse )
            {
                Rect rect = new Rect( selectStartPos );
                rect.Add( EngineApp.Instance.MousePosition );

                Vec2i windowSize = EngineApp.Instance.VideoMode.Size;
                Vec2 thickness = new Vec2( 1.0f / (float)windowSize.X, 1.0f / (float)windowSize.Y );

                renderer.AddQuad( new Rect( rect.Left, rect.Top + thickness.Y,
                    rect.Right, rect.Top + thickness.Y * 2 ), new ColorValue( 0, 0, 0, .5f ) );
                renderer.AddQuad( new Rect( rect.Left, rect.Bottom,
                    rect.Right, rect.Bottom + thickness.Y ), new ColorValue( 0, 0, 0, .5f ) );
                renderer.AddQuad( new Rect( rect.Left + thickness.X, rect.Top,
                    rect.Left + thickness.X * 2, rect.Bottom ), new ColorValue( 0, 0, 0, .5f ) );
                renderer.AddQuad( new Rect( rect.Right, rect.Top,
                    rect.Right + thickness.X, rect.Bottom ), new ColorValue( 0, 0, 0, .5f ) );

                renderer.AddQuad( new Rect( rect.Left, rect.Top,
                    rect.Right, rect.Top + thickness.Y ), new ColorValue( 0, 1, 0, 1 ) );
                renderer.AddQuad( new Rect( rect.Left, rect.Bottom - thickness.Y,
                    rect.Right, rect.Bottom ), new ColorValue( 0, 1, 0, 1 ) );
                renderer.AddQuad( new Rect( rect.Left, rect.Top,
                    rect.Left + thickness.X, rect.Bottom ), new ColorValue( 0, 1, 0, 1 ) );
                renderer.AddQuad( new Rect( rect.Right - thickness.X, rect.Top,
                    rect.Right, rect.Bottom ), new ColorValue( 0, 1, 0, 1 ) );
            }
        }
Exemplo n.º 3
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();
        }