protected override void OnRenderUI( GuiRenderer renderer )
        {
            base.OnRenderUI( renderer );

            Vec2 size = new Vec2( 232, 335 );
            //size *= 1.0f + Time * .015f;
            size /= new Vec2( 768.0f * renderer.AspectRatio, 768.0f );

            Rect rectangle = new Rect( -size / 2, size / 2 ) + new Vec2( .3f, .5f );

            float alpha = 0;

            if( Time > 1 && Time <= 2 )
                alpha = Time - 1;
            else if( Time > 2 && Time <= lifeTime - 2 - 2 )
                alpha = 1;
            else if( Time >= lifeTime - 2 - 2 && Time < lifeTime - 1 )
                alpha = 1 - ( Time - ( lifeTime - 2 - 2 ) ) / 3;

            if( alpha != 0 )
            {
                renderer.AddQuad( rectangle, new Rect( 0, 0, 1, 1 ), productTexture,
                    new ColorValue( 1, 1, 1, alpha ) );
            }
        }
        public void DrawDebug(Rect r, GuiRenderer renderer)
        {
            Rect[] rs = new Rect[5];
            rs[0] = new Rect(Utils.TR(r, new Vec2(0.05f, 0.375f)), Utils.TR(r, new Vec2(0.30f, 0.625f)));
            rs[4] = new Rect(Utils.TR(r, new Vec2(0.05f, 0.05f)), Utils.TR(r, new Vec2(0.30f, 0.30f)));
            rs[2] = new Rect(Utils.TR(r, new Vec2(0.05f, 0.70f)), Utils.TR(r, new Vec2(0.30f, 0.95f)));
            rs[3] = new Rect(Utils.TR(r, new Vec2(0.375f, 0.375f)), Utils.TR(r, new Vec2(0.625f, 0.625f)));
            rs[1] = new Rect(Utils.TR(r, new Vec2(0.70f, 0.375f)), Utils.TR(r, new Vec2(0.95f, 0.625f)));

            for (int i = 0; i < aerofoils.Length; i++)
                aerofoils[i].DrawDebug(rs[i], renderer);

            float speed = awesomeAircraft.mainBody.LinearVelocity.Length();
            renderer.AddText("speed:" + speed.ToString(), r.LeftTop);
        }
Exemple #3
0
        protected override void OnRenderUI( GuiRenderer renderer )
        {
            base.OnRenderUI( renderer );

            Vec2 size = new Vec2( 523.0f / 1024.0f, 235.0f / 768.0f );

            Rect rectangle = new Rect( -size / 2, size / 2 ) + new Vec2( .5f, .5f );

            float alpha = 0;

            if( Time > 2 && Time <= 3 )
                alpha = Time - 2;
            else if( Time > 3 && Time <= lifeTime - 2 - 2 )
                alpha = 1;
            else if( Time >= lifeTime - 2 - 2 && Time < lifeTime - 1 )
                alpha = 1 - ( Time - ( lifeTime - 2 - 2 ) ) / 3;

            if( alpha != 0 )
                renderer.AddQuad( rectangle, new Rect( 0, 0, 1, 1 ), engineTexture,
                    new ColorValue( 1, 1, 1, alpha ) );
        }
        protected override void OnRenderUI( GuiRenderer renderer )
        {
            base.OnRenderUI( renderer );

            Vec2 size = new Vec2( 512, 215.0f );
            size *= 1.0f + Time * .015f;
            size /= new Vec2( 768.0f * renderer.AspectRatio, 768.0f );

            Rect rectangle = new Rect( -size , size  ) + new Vec2( .5f, .5f );

            float alpha = 0;

            if( Time > 2 && Time <= 3 )
                alpha = Time - 2;
            else if( Time > 3 && Time <= lifeTime - 2 - 2 )
                alpha = 1;
            else if( Time >= lifeTime - 2 - 2 && Time < lifeTime - 1 )
                alpha = 1 - ( Time - ( lifeTime - 2 - 2 ) ) / 3;

            if( alpha != 0 )
                renderer.AddQuad( rectangle, new Rect( 0, 0, 1, 1 ), engineTexture,
                    new ColorValue( 1, 1, 1, alpha ) );
        }
		RectI GetMapMotionRectangle( Rect rect )
		{
			Vec2 minf = ( rect.Minimum - mapMotionPosition ) * gridCellSizeInv;
			Vec2 maxf = ( rect.Maximum - mapMotionPosition ) * gridCellSizeInv;
			Vec2I min = new Vec2I( (int)minf.X, (int)minf.Y );
			Vec2I max = new Vec2I( (int)maxf.X, (int)maxf.Y );
			min.Clamp( new Vec2I( 0, 0 ), mapMaxIndex );
			max.Clamp( new Vec2I( 0, 0 ), mapMaxIndex );
			return new RectI( min, max );
		}
        public void DoRenderUI( GuiRenderer renderer )
        {
            if( transparency == 0.0f )
                return;

            if( firstRender )
            {
                texture = TextureManager.Instance.Load( "Gui\\Various\\Console.dds",
                    Texture.Type.Type2D, 0 );
                font = FontManager.Instance.LoadFont( "Default", .025f );

                firstRender = false;
            }

            if( font == null )
                return;

            Rect textureRect = new Rect( 0, 0, 1, 1 );
            textureRect += textureOffset;

            renderer.AddQuad( new Rect( 0, 0, 1, .5f ), textureRect, texture,
                new ColorValue( 1, 1, 1, transparency ), false, new Rect( 0, 0, 1, 1 ) );
            renderer.AddQuad( new Rect( 0, .495f, 1, .5f ),
                new ColorValue( .8f, .8f, .8f, transparency ) );

            string staticText = "Version " + EngineVersionInformation.Version;
            if( staticText != null && staticText != "" )
            {
                renderer.AddText( font, staticText, new Vec2( .99f, .5f - font.Height ),
                    HorizontalAlign.Right, VerticalAlign.Center, new ColorValue( 1, 1, 1, transparency ) );
            }

            float fontheight = font.Height;

            float x = .01f;

            float y = .5f - fontheight;

            string str;
            if( stringDownPosition != strings.Count - 1 )
            {
                str = "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" +
                    " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" +
                    " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" +
                    " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" +
                    " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";
            }
            else
                str = currentString + "_";

            renderer.AddText( font, str, new Vec2( x, y ), HorizontalAlign.Left,
                VerticalAlign.Center, new ColorValue( 1, 1, 1, transparency ) );

            y -= fontheight + fontheight * .5f;

            int startpos = stringDownPosition;
            if( startpos > strings.Count - 1 )
                startpos = strings.Count - 1;
            for( int n = startpos; n >= 0 && y - fontheight > 0; n-- )
            {
                renderer.AddText( font, strings[ n ].text, new Vec2( x, y ), HorizontalAlign.Left,
                    VerticalAlign.Center, strings[ n ].color * new ColorValue( 1, 1, 1, transparency ) );
                y -= fontheight;
            }
        }
Exemple #7
0
        void TickMove()
        {
            //path find control
            {
                if( pathFindWaitTime != 0 )
                {
                    pathFindWaitTime -= TickDelta;
                    if( pathFindWaitTime < 0 )
                        pathFindWaitTime = 0;
                }

                if( pathFoundedToPosition != MovePosition.ToVec2() && pathFindWaitTime == 0 )
                    path.Clear();

                if( path.Count == 0 )
                {
                    if( pathFindWaitTime == 0 )
                    {
                        if( DoPathFind() )
                        {
                            pathFoundedToPosition = MovePosition.ToVec2();
                            pathFindWaitTime = .5f;
                        }
                        else
                        {
                            pathFindWaitTime = 1.0f;
                        }
                    }
                }
            }

            if( path.Count == 0 )
                return;

            //line movement to path[ 0 ]
            {
                Vec2 destPoint = path[ 0 ];

                Vec2 diff = destPoint - Position.ToVec2();

                if( diff == Vec2.Zero )
                {
                    path.RemoveAt( 0 );
                    return;
                }

                Radian dir = MathFunctions.ATan16( diff.Y, diff.X );

                float halfAngle = dir * 0.5f;
                Quat rot = new Quat( new Vec3( 0, 0, MathFunctions.Sin16( halfAngle ) ),
                    MathFunctions.Cos16( halfAngle ) );
                Rotation = rot;

                Vec2 dirVector = diff.GetNormalizeFast();
                Vec2 dirStep = dirVector * ( Type.MaxVelocity * TickDelta );

                Vec2 newPos = Position.ToVec2() + dirStep;

                if( Math.Abs( diff.X ) <= Math.Abs( dirStep.X ) && Math.Abs( diff.Y ) <= Math.Abs( dirStep.Y ) )
                {
                    //unit at point
                    newPos = path[ 0 ];
                    path.RemoveAt( 0 );
                }

                GridPathFindSystem.Instance.RemoveObjectFromMotionMap( this );

                bool free;
                {
                    float radius = Type.Radius;
                    Rect targetRect = new Rect( newPos - new Vec2( radius, radius ), newPos + new Vec2( radius, radius ) );

                    free = GridPathFindSystem.Instance.IsFreeInMapMotion( targetRect );
                }

                GridPathFindSystem.Instance.AddObjectToMotionMap( this );

                if( free )
                {
                    float newZ = GridPathFindSystem.Instance.GetMotionMapHeight( newPos ) + Type.Height * .5f;
                    Position = new Vec3( newPos.X, newPos.Y, newZ );
                }
                else
                    path.Clear();
            }
        }
Exemple #8
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 ) );
            }
        }
        bool DoPathFind()
        {
            Dynamic targetObj = null;
            {
                //not true because use Intellect
                AntUnitAI ai = Intellect as AntUnitAI;
                if (ai != null)
                    targetObj = ai.CurrentTask.Entity;
            }

            if (GridPathFindSystem.Instance == null)
                return false;

            GridPathFindSystem.Instance.RemoveObjectFromMotionMap(this);

            Bounds bounds = PhysicsModel.GetGlobalBounds();

            float radius = Type.Radius;
            Rect targetRect = new Rect(MovePosition.ToVec2() - new Vec2(radius, radius),
                MovePosition.ToVec2() + new Vec2(radius, radius));

            if (targetObj != null && targetObj != this)
                GridPathFindSystem.Instance.RemoveObjectFromMotionMap(targetObj);

            GridPathFindSystem.Instance.DoTempClearMotionMap(targetRect);

            const int maxFieldsDistance = 1000;
            const int maxFieldsToCheck = 100000;
            // Find a path to the specified position
            bool found = GridPathFindSystem.Instance.DoFind(Type.Radius * 2 * 1.1f, Position.ToVec2(),
                MovePosition.ToVec2(), maxFieldsDistance, maxFieldsToCheck, true, false, path);

            GridPathFindSystem.Instance.RestoreAllTempClearedMotionMap();

            if (targetObj != null && targetObj != this)
                GridPathFindSystem.Instance.AddObjectToMotionMap(targetObj);

            GridPathFindSystem.Instance.AddObjectToMotionMap(this);

            return found;
        }
Exemple #10
0
        bool IsFreeForBuildTaskTargetBuild( Vec3 pos )
        {
            Bounds bounds;
            {
                PhysicsModel physicsModel = PhysicsWorld.Instance.LoadPhysicsModel( taskTargetBuildingType.PhysicsModel );
                if( physicsModel == null )
                {
                    Log.Fatal( string.Format( "No physics model for \"{0}\"", taskTargetBuildingType.ToString() ) );
                    return false;
                }
                bounds = physicsModel.GetGlobalBounds();
                physicsModel.Dispose();

                bounds += pos;
            }

            Rect rect = new Rect( bounds.Minimum.ToVec2(), bounds.Maximum.ToVec2() );
            return GridPathFindSystem.Instance.IsFreeInMapMotion( rect );
        }
Exemple #11
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();
        }
Exemple #12
0
        protected virtual void TickTasks()
        {
            RTSUnit controlledObj = ControlledObject;
            if( controlledObj == null )
                return;

            switch( currentTask.Type )
            {

            //Stop
            case Task.Types.Stop:
                controlledObj.Stop();
                break;

            //Move
            case Task.Types.Move:
            case Task.Types.BreakableMove:
                if( currentTask.Entity != null )
                {
                    controlledObj.Move( currentTask.Entity.Position );
                }
                else
                {
                    Vec3 pos = currentTask.Position;

                    if( ( controlledObj.Position.ToVec2() - pos.ToVec2() ).LengthFast() < 1.5f &&
                        Math.Abs( controlledObj.Position.Z - pos.Z ) < 3.0f )
                    {
                        //get to
                        DoNextTask();
                    }
                    else
                        controlledObj.Move( pos );
                }
                break;

            //Attack, Repair
            case Task.Types.Attack:
            case Task.Types.BreakableAttack:
            case Task.Types.Repair:
            case Task.Types.BreakableRepair:
                {
                    //healed
                    if( ( currentTask.Type == Task.Types.Repair ||
                        currentTask.Type == Task.Types.BreakableRepair )
                        && currentTask.Entity != null )
                    {
                        if( currentTask.Entity.Life == currentTask.Entity.Type.LifeMax )
                        {
                            DoNextTask();
                            break;
                        }
                    }

                    float needDistance = controlledObj.Type.OptimalAttackDistanceRange.Maximum;

                    Vec3 targetPos;
                    if( currentTask.Entity != null )
                        targetPos = currentTask.Entity.Position;
                    else
                        targetPos = currentTask.Position;

                    float distance = ( controlledObj.Position - targetPos ).LengthFast();

                    if( distance != 0 )
                    {
                        bool lineVisibility = false;
                        {
                            if( distance < needDistance )
                            {
                                lineVisibility = true;

                                //direct line visibility check

                                Vec3 start = initialWeapons[ 0 ].Position;
                                Ray ray = new Ray( start, targetPos - start );

                                RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                                    ray, (int)ContactGroup.CastOnlyContact );

                                foreach( RayCastResult result in piercingResult )
                                {
                                    MapObject obj = MapSystemWorld.GetMapObjectByBody( result.Shape.Body );

                                    if( obj != null && obj == currentTask.Entity )
                                        break;

                                    if( obj != controlledObj )
                                    {
                                        lineVisibility = false;
                                        break;
                                    }
                                }
                            }
                        }

                        //movement control
                        if( lineVisibility )
                        {
                            //stop
                            controlledObj.Stop();

                            RTSCharacter character = controlledObj as RTSCharacter;
                            if( character != null )
                                character.SetLookDirection( targetPos );
                        }
                        else
                        {
                            //move to target
                            controlledObj.Move( targetPos );
                        }

                        //weapons control
                        if( lineVisibility )
                        {
                            foreach( Weapon weapon in initialWeapons )
                            {
                                Vec3 pos = targetPos;
                                Gun gun = weapon as Gun;
                                if( gun != null && currentTask.Entity != null )
                                    gun.GetAdvanceAttackTargetPosition( false, currentTask.Entity, false, out pos );
                                weapon.SetForceFireRotationLookTo( pos );

                                if( weapon.Ready )
                                {
                                    Range range;

                                    range = weapon.Type.WeaponNormalMode.UseDistanceRange;
                                    if( distance >= range.Minimum && distance <= range.Maximum )
                                        weapon.TryFire( false );

                                    range = weapon.Type.WeaponAlternativeMode.UseDistanceRange;
                                    if( distance >= range.Minimum && distance <= range.Maximum )
                                        weapon.TryFire( true );
                                }
                            }
                        }
                    }

                }
                break;

            //BuildBuilding
            case Task.Types.BuildBuilding:
                {
                    float needDistance = controlledObj.Type.OptimalAttackDistanceRange.Maximum;

                    Vec3 targetPos = currentTask.Position;

                    float distance = ( controlledObj.Position - targetPos ).LengthFast();

                    if( distance < needDistance )
                    {
                        controlledObj.Stop();

                        //get to

                        //check free area for build
                        bool free;
                        {
                            Bounds bounds;
                            {
                                PhysicsModel physicsModel = PhysicsWorld.Instance.LoadPhysicsModel(
                                    currentTask.EntityType.PhysicsModel );
                                if( physicsModel == null )
                                    Log.Fatal( string.Format( "No physics model for \"{0}\"",
                                        currentTask.EntityType.ToString() ) );
                                bounds = physicsModel.GetGlobalBounds();

                                bounds += targetPos;
                            }

                            Rect rect = new Rect( bounds.Minimum.ToVec2(), bounds.Maximum.ToVec2() );
                            free = GridPathFindSystem.Instance.IsFreeInMapMotion( rect );
                        }

                        if( !free )
                        {
                            //not free
                            DoNextTask();
                            break;
                        }

                        //check cost
                        RTSFactionManager.FactionItem factionItem = RTSFactionManager.Instance.GetFactionItemByType( Faction );
                        if( factionItem != null )
                        {
                            float cost = ( (RTSBuildingType)currentTask.EntityType ).BuildCost;

                            if( factionItem.Money - cost < 0 )
                            {
                                //No money
                                DoNextTask();
                                break;
                            }

                            factionItem.Money -= cost;
                        }

                        RTSBuilding building = (RTSBuilding)Entities.Instance.Create( currentTask.EntityType, Map.Instance );
                        building.Position = currentTask.Position;

                        building.InitialFaction = Faction;

                        building.PostCreate();
                        building.BuildedProgress = 0;
                        building.Life = 1;

                        //Repair
                        DoTaskInternal( new Task( Task.Types.Repair, building ) );
                    }
                    else
                        controlledObj.Move( targetPos );
                }
                break;

            }
        }
Exemple #13
0
 public bool Equals(Rect other)
 {
     return other.Left == Left && other.Top == Top && other.Width == Width && other.Height == Height;
 }
		public bool IsFreeInMapMotion( Rect rect )
		{
			if( initialized )
			{
				RectI mapMotionRect = GetMapMotionRectangle( rect );
				for( int y = mapMotionRect.Top; y <= mapMotionRect.Bottom; y++ )
					for( int x = mapMotionRect.Left; x <= mapMotionRect.Right; x++ )
						if( !IsFreeInMapMotion( new Vec2I( x, y ) ) )
							return false;
			}
			return true;
		}
		void InitializeMotionMap()
		{
			initialized = true;
			if( gridCellSize != 0 )
				gridCellSizeInv = 1.0f / gridCellSize;

			Rect bounds = gridBounds;
			if( bounds == Rect.Zero )
				bounds = new Rect( Vec2.Zero, new Vec2( 10, 10 ) );

			Vec2 v = bounds.GetSize() * gridCellSizeInv;
			mapSize = new Vec2I( (int)v.X, (int)v.Y );
			mapMotionPosition = new Vec2( bounds.Minimum.X, bounds.Minimum.Y );

			mapMaxIndex = mapSize - new Vec2I( mapSize.X > 0 ? 1 : 0, mapSize.Y > 0 ? 1 : 0 );

			openList = new int[ mapSize.X * mapSize.Y + 2 ];
			whichList = new int[ mapSize.X, mapSize.Y ];
			openX = new int[ mapSize.X * mapSize.Y + 2 ];
			openY = new int[ mapSize.X * mapSize.Y + 2 ];
			parentX = new int[ mapSize.X, mapSize.Y ];
			parentY = new int[ mapSize.X, mapSize.Y ];
			Fcost = new int[ mapSize.X * mapSize.Y + 2 ];
			Gcost = new int[ mapSize.X, mapSize.Y ];
			Hcost = new int[ mapSize.X * mapSize.Y + 2 ];

			pathArray = new Vec2I[ mapSize.X * mapSize.Y ];

			renderVertices.Clear();
			renderFreeIndices.Clear();
			renderBusyIndices.Clear();

			mapMotionRectangles.Clear();

			mapMotion = new byte[ mapSize.X, mapSize.Y ];
			FillMotionMap();
		}
Exemple #16
0
        void DrawArea_RenderUI( Control sender, GuiRenderer renderer )
        {
            Rect rectangle = sender.GetScreenRectangle();
            bool clipRectangle = true;
            ColorValue[] colors = new ColorValue[]{
                new ColorValue( 1 ,0, 0 ),
                new ColorValue( 0, 1, 0 ),
                new ColorValue( 0, 0, 1 ),
                new ColorValue( 1, 1, 0 ),
                new ColorValue( 1, 1, 1 )};

            if( clipRectangle )
                renderer.PushClipRectangle( rectangle );

            //draw triangles
            if( GetDrawAreaMode() == DrawAreaModes.Triangles )
            {
                float distance = rectangle.GetSize().X / 2;

                List<GuiRenderer.TriangleVertex> triangles = new List<GuiRenderer.TriangleVertex>( 256 );

                Radian angle = -EngineApp.Instance.Time;

                const int steps = 30;
                Vec2 lastPosition = Vec2.Zero;
                for( int step = 0; step < steps + 1; step++ )
                {
                    Vec2 localPos = new Vec2( MathFunctions.Cos( angle ), MathFunctions.Sin( angle ) ) * distance;
                    Vec2 pos = rectangle.GetCenter() + new Vec2( localPos.X, localPos.Y * renderer.AspectRatio );

                    if( step != 0 )
                    {
                        ColorValue color = colors[ step % colors.Length ];
                        ColorValue color2 = color;
                        color2.Alpha = 0;
                        triangles.Add( new GuiRenderer.TriangleVertex( rectangle.GetCenter(), color ) );
                        triangles.Add( new GuiRenderer.TriangleVertex( lastPosition, color2 ) );
                        triangles.Add( new GuiRenderer.TriangleVertex( pos, color2 ) );
                    }

                    angle += ( MathFunctions.PI * 2 ) / steps;
                    lastPosition = pos;
                }

                renderer.AddTriangles( triangles );
            }

            //draw quads
            if( GetDrawAreaMode() == DrawAreaModes.Quads )
            {
                //draw background
                {
                    Texture texture = TextureManager.Instance.Load( "Gui\\Various\\Logo.png" );
                    renderer.AddQuad( rectangle, new Rect( 0, -.3f, 1, 1.4f ), texture,
                        new ColorValue( 1, 1, 1 ), true );
                }

                //draw bars
                {
                    float time = EngineApp.Instance.Time;

                    EngineRandom random = new EngineRandom( 0 );

                    int count = 15;
                    float stepOffset = rectangle.GetSize().X / count;
                    float size = stepOffset * .9f;
                    for( int n = 0; n < count; n++ )
                    {
                        float v = MathFunctions.Cos( time * random.NextFloat() );
                        float v2 = ( v + 1 ) / 2;

                        ColorValue color = colors[ n % colors.Length ];
                        Rect rect = new Rect(
                            rectangle.Left + stepOffset * n, rectangle.Bottom - rectangle.GetSize().Y * v2,
                            rectangle.Left + stepOffset * n + size, rectangle.Bottom );
                        renderer.AddQuad( rect, color );
                    }
                }
            }

            //draw lines
            if( GetDrawAreaMode() == DrawAreaModes.Lines )
            {
                float maxDistance;
                {
                    Vec2 s = rectangle.GetSize() / 2;
                    maxDistance = MathFunctions.Sqrt( s.X * s.X + s.Y * s.Y );
                }

                int step = 0;
                float distance = 0;
                Radian angle = -EngineApp.Instance.Time;
                Vec2 lastPosition = Vec2.Zero;

                while( distance < maxDistance )
                {
                    Vec2 localPos = new Vec2( MathFunctions.Cos( angle ), MathFunctions.Sin( angle ) ) * distance;
                    Vec2 pos = rectangle.GetCenter() + new Vec2( localPos.X, localPos.Y * renderer.AspectRatio );

                    if( step != 0 )
                    {
                        ColorValue color = colors[ step % colors.Length ];
                        renderer.AddLine( lastPosition, pos, color );
                    }

                    step++;
                    angle += MathFunctions.PI / 10;
                    distance += .001f;
                    lastPosition = pos;
                }
            }

            //draw text
            if( GetDrawAreaMode() == DrawAreaModes.Text )
            {
                //draw text with specified font.
                renderer.AddText( drawAreaBigFont, "Big Font Sample", rectangle.LeftTop, HorizontalAlign.Left,
                    VerticalAlign.Top, new ColorValue( 1, 1, 1 ) );

                //draw text with word wrap.
                string text = "Test Test Test.\n\nThe expandable user interface system is a unified system " +
                    "for the creation of end-user controls, menus, dialogues, windows and HUD screens. " +
                    "With the help of this system the end-user exercises control over the application.";
                renderer.AddTextWordWrap( text, rectangle, HorizontalAlign.Right, false, VerticalAlign.Bottom, 0,
                    new ColorValue( 1, 1, 0 ) );
            }

            if( clipRectangle )
                renderer.PopClipRectangle();
        }
Exemple #17
0
        void CreateWaterPlaneSplash()
        {
            float influenceRadius = Type.Radius * .75f;

            foreach( WaterPlane waterPlane in WaterPlane.Instances )
            {
                //check by height
                if( Position.Z + influenceRadius < waterPlane.Position.Z )
                    continue;
                if( Position.Z - influenceRadius > waterPlane.Position.Z )
                    continue;

                //check by bounds
                Rect bounds2 = new Rect( waterPlane.Position.ToVec2() );
                bounds2.Expand( waterPlane.Size * .5f );
                if( !bounds2.IsContainsPoint( Position.ToVec2() ) )
                    continue;

                //check by physics
                float height = Position.Z + .01f;
                float waterHeight = waterPlane.Position.Z;
                Ray ray = new Ray( new Vec3( Position.X, Position.Y, height ),
                    new Vec3( 0, 0, waterHeight - height ) );
                if( ray.Direction.Z != 0 )
                {
                    RayCastResult result = PhysicsWorld.Instance.RayCast(
                        ray, (int)ContactGroup.CastOnlyContact );
                    if( result.Shape != null )
                        continue;
                }

                //create splash
                waterPlane.CreateSplash( WaterPlaneType.SplashTypes.Explosion,
                    new Vec3( Position.X, Position.Y, waterPlane.Position.Z ) );
            }
        }
Exemple #18
0
        private void CreateWaterPlaneSplash(Ray ray)
        {
            if (ray.Direction.Z >= 0)
                return;

            foreach (WaterPlane waterPlane in WaterPlane.Instances)
            {
                //check by plane
                Plane plane = new Plane(Vec3.ZAxis, waterPlane.Position.Z);
                float scale;
                if (!plane.LineIntersection(ray.Origin, ray.Origin + ray.Direction, out scale))
                    continue;
                Vec3 pos = ray.GetPointOnRay(scale);

                //check by bounds
                Rect bounds2 = new Rect(waterPlane.Position.ToVec2());
                bounds2.Expand(waterPlane.Size * .5f);
                if (!bounds2.IsContainsPoint(pos.ToVec2()))
                    continue;

                //create splash
                waterPlane.CreateSplash(WaterPlaneType.SplashTypes.Bullet, pos);
            }
        }
 public View AddView(Rect rectangle)
 {
     View view = new View();
     view.Rectangle = rectangle;
     views.Add(view);
     return view;
 }
		public void AddTempClearMotionMap( Rect rectangle )
		{
			if( !initialized )
				return;

			RectI mapRectangle = GetMapMotionRectangle( rectangle );

			TempClearMotionMapDataItem item = new TempClearMotionMapDataItem();
			item.mapRectangle = mapRectangle;
			item.saveMapData = new byte[ mapRectangle.Size.X + 1, mapRectangle.Size.Y + 1 ];

			byte[ , ] data = item.saveMapData;
			for( int y = mapRectangle.Top; y <= mapRectangle.Bottom; y++ )
			{
				for( int x = mapRectangle.Left; x <= mapRectangle.Right; x++ )
				{
					data[ x - mapRectangle.Left, y - mapRectangle.Top ] = mapMotion[ x, y ];
					mapMotion[ x, y ] = 0;
				}
			}

			tempClearMotionMapData.Add( item );
		}
Exemple #21
0
        Vec2 GetMapPositionByMouseOnMinimap()
        {
            Rect screenMapRect = minimapControl.GetScreenRectangle();

            Bounds initialBounds = Map.Instance.InitialCollisionBounds;
            Rect mapRect = new Rect( initialBounds.Minimum.ToVec2(), initialBounds.Maximum.ToVec2() );

            Vec2 point = MousePosition;

            point -= screenMapRect.Minimum;
            point /= screenMapRect.Size;
            point = new Vec2( point.X, 1.0f - point.Y );
            point *= mapRect.Size;
            point += mapRect.Minimum;

            return point;
        }
        private void DrawObjectSelectionBorder(Bounds bounds)
        {
            Camera camera = RendererWorld.Instance.DefaultCamera;
            GuiRenderer renderer = EngineApp.Instance.ScreenGuiRenderer;

            Texture texture = TextureManager.Instance.Load("Gui\\Textures\\ObjectSelectionBorder.png");
            Vec2 viewportSize = renderer.ViewportForScreenGuiRenderer.DimensionsInPixels.Size.ToVec2();

            float sizeY = .08f;
            Vec2 size = SnapToPixel(new Vec2(sizeY / camera.AspectRatio, sizeY), viewportSize);
            float alpha = MathFunctions.Sin(Time * MathFunctions.PI) * .5f + .5f;

            Rect screenRectangle = Rect.Cleared;
            {
                Vec3[] points = null;
                bounds.ToPoints(ref points);
                foreach (Vec3 point in points)
                {
                    Vec2 screenPoint;
                    if (camera.ProjectToScreenCoordinates(point, out screenPoint))
                    {
                        screenPoint.Clamp(new Vec2(0, 0), new Vec2(1, 1));
                        screenRectangle.Add(screenPoint);
                    }
                }

                Vec2[] screenPositions = new Vec2[] {
					new Vec2( 0, 0 ),
					new Vec2( 1, 0 ),
					new Vec2( 0, 1 ),
					new Vec2( 1, 1 ) };
                foreach (Vec2 screenPosition in screenPositions)
                {
                    Ray ray = camera.GetCameraToViewportRay(screenPosition);
                    if (bounds.RayIntersection(ray))
                        screenRectangle.Add(screenPosition);
                }

                if (screenRectangle.GetSize().X < size.X * 2)
                {
                    screenRectangle = new Rect(
                        screenRectangle.GetCenter().X - size.X, screenRectangle.Top,
                        screenRectangle.GetCenter().X + size.X, screenRectangle.Bottom);
                }
                if (screenRectangle.GetSize().Y < size.Y * 2)
                {
                    screenRectangle = new Rect(
                        screenRectangle.Left, screenRectangle.GetCenter().Y - size.Y,
                        screenRectangle.Right, screenRectangle.GetCenter().Y + size.Y);
                }
            }

            {
                Vec2 point = screenRectangle.LeftTop;
                point = SnapToPixel(point, viewportSize) + new Vec2(.25f, .25f) / viewportSize;
                Rect rectangle = new Rect(point, point + size);
                Rect texCoord = new Rect(0, 0, .5f, .5f);
                renderer.AddQuad(rectangle, texCoord, texture, new ColorValue(1, 1, 1, alpha), true);
            }

            {
                Vec2 point = screenRectangle.RightTop;
                point = SnapToPixel(point, viewportSize) + new Vec2(.25f, .25f) / viewportSize;
                Rect rectangle = new Rect(point - new Vec2(size.X, 0), point + new Vec2(0, size.Y));
                Rect texCoord = new Rect(.5f, 0, 1, .5f);
                renderer.AddQuad(rectangle, texCoord, texture, new ColorValue(1, 1, 1, alpha), true);
            }

            {
                Vec2 point = screenRectangle.LeftBottom;
                point = SnapToPixel(point, viewportSize) + new Vec2(.25f, .25f) / viewportSize;
                Rect rectangle = new Rect(point - new Vec2(0, size.Y), point + new Vec2(size.X, 0));
                Rect texCoord = new Rect(0, .5f, .5f, 1);
                renderer.AddQuad(rectangle, texCoord, texture, new ColorValue(1, 1, 1, alpha), true);
            }

            {
                Vec2 point = screenRectangle.RightBottom;
                point = SnapToPixel(point, viewportSize) + new Vec2(.25f, .25f) / viewportSize;
                Rect rectangle = new Rect(point - size, point);
                Rect texCoord = new Rect(.5f, .5f, 1, 1);
                renderer.AddQuad(rectangle, texCoord, texture, new ColorValue(1, 1, 1, alpha), true);
            }
        }
Exemple #23
0
        //Draw minimap
        void Minimap_RenderUI( EControl sender, GuiRenderer renderer )
        {
            Rect screenMapRect = sender.GetScreenRectangle();

            Bounds initialBounds = Map.Instance.InitialCollisionBounds;
            Rect mapRect = new Rect( initialBounds.Minimum.ToVec2(), initialBounds.Maximum.ToVec2() );

            Vec2 mapSizeInv = new Vec2( 1, 1 ) / mapRect.Size;

            //draw units
            Vec2 screenPixel = new Vec2( 1, 1 ) / new Vec2( EngineApp.Instance.VideoMode.Size.ToVec2() );

            foreach( Entity entity in Map.Instance.Children )
            {
                RTSUnit unit = entity as RTSUnit;
                if( unit == null )
                    continue;

                Rect rect = new Rect( unit.MapBounds.Minimum.ToVec2(), unit.MapBounds.Maximum.ToVec2() );

                rect -= mapRect.Minimum;
                rect.Minimum *= mapSizeInv;
                rect.Maximum *= mapSizeInv;
                rect.Minimum = new Vec2( rect.Minimum.X, 1.0f - rect.Minimum.Y );
                rect.Maximum = new Vec2( rect.Maximum.X, 1.0f - rect.Maximum.Y );
                rect.Minimum *= screenMapRect.Size;
                rect.Maximum *= screenMapRect.Size;
                rect += screenMapRect.Minimum;

                //increase 1 pixel
                rect.Maximum += new Vec2( screenPixel.X, -screenPixel.Y );

                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 );

                renderer.AddQuad( rect, color );
            }

            //Draw camera borders
            {
                Camera camera = RendererWorld.Instance.DefaultCamera;

                if( camera.Position.Z > 0 )
                {

                    Plane groundPlane = new Plane( 0, 0, 1, 0 );

                    Vec2[] points = new Vec2[ 4 ];

                    for( int n = 0; n < 4; n++ )
                    {
                        Vec2 p = Vec2.Zero;

                        switch( n )
                        {
                        case 0: p = new Vec2( 0, 0 ); break;
                        case 1: p = new Vec2( 1, 0 ); break;
                        case 2: p = new Vec2( 1, 1 ); break;
                        case 3: p = new Vec2( 0, 1 ); break;
                        }

                        Ray ray = camera.GetCameraToViewportRay( p );

                        float scale;
                        groundPlane.RayIntersection( ray, out scale );

                        Vec3 pos = ray.GetPointOnRay( scale );
                        if( ray.Direction.Z > 0 )
                            pos = ray.Origin + ray.Direction.GetNormalize() * 10000;

                        Vec2 point = pos.ToVec2();

                        point -= mapRect.Minimum;
                        point *= mapSizeInv;
                        point = new Vec2( point.X, 1.0f - point.Y );
                        point *= screenMapRect.Size;
                        point += screenMapRect.Minimum;

                        points[ n ] = point;
                    }

                    for( int n = 0; n < 4; n++ )
                        renderer.AddLine( points[ n ], points[ ( n + 1 ) % 4 ], new ColorValue( 1, 1, 1 ),
                            screenMapRect );
                }
            }
        }
        private bool DoPathFind()
        {
            Dynamic targetObj = null;
            {
                RTSUnitAI ai = Intellect as RTSUnitAI;
                if (ai != null)
                    targetObj = ai.CurrentTask.Entity;
            }

            //remove this unit from the pathfinding grid
            GetNavigationSystem().RemoveObjectFromMotionMap(this);

            float radius = Type.Radius;
            Rect targetRect = new Rect(
                MovePosition.ToVec2() - new Vec2(radius, radius),
                MovePosition.ToVec2() + new Vec2(radius, radius));

            //remove target unit from the pathfinding grid
            if (targetObj != null && targetObj != this)
                GetNavigationSystem().RemoveObjectFromMotionMap(targetObj);

            //TO DO: really need this call?
            GetNavigationSystem().AddTempClearMotionMap(targetRect);

            const int maxFieldsDistance = 1000;
            const int maxFieldsToCheck = 100000;
            bool found = GetNavigationSystem().FindPath(
                Type.Radius * 2 * 1.1f,
                Position.ToVec2(),
                MovePosition.ToVec2(),
                maxFieldsDistance,
                maxFieldsToCheck,
                true,
                false,
                path);

            GetNavigationSystem().DeleteAllTempClearedMotionMap();

            //add target unit to the pathfinding grid
            if (targetObj != null && targetObj != this)
                GetNavigationSystem().AddObjectToMotionMap(targetObj);

            //add this unit the the pathfinding grid
            GetNavigationSystem().AddObjectToMotionMap(this);

            return found;
        }
Exemple #25
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 );
            }
        }
Exemple #26
0
        // TASK: Draw minimap
        void Minimap_RenderUI(EControl sender, GuiRenderer renderer)
        {
            Rect screenMapRect = sender.GetScreenRectangle();

            //Get Map Rectange
            Bounds initialBounds = Map.Instance.InitialCollisionBounds;
            Rect mapRect = new Rect(initialBounds.Minimum.ToVec2(), initialBounds.Maximum.ToVec2());

            Vec2 mapSizeInv = new Vec2(1, 1) / mapRect.Size;

            Unit playerUnit = GetPlayerUnit();

            Rect rect = new Rect(playerUnit.MapBounds.Minimum.ToVec2(),
                                 playerUnit.MapBounds.Maximum.ToVec2() );

            //Get position in the worldmap 2d image
            float worldMapX = (playerUnit.Position.X + 500) / 1000 * 512;
            float worldMapY = (playerUnit.Position.Y + 500) / 1000 * 512;

            //Get begin cliped rectange postion in the worldmap 2d image
            float x1 = worldMapX - 56;
            float y1 = worldMapY - 56;

            float x2 = x1 + 128;
            float y2 = x2 + 128;

            //Convert to % unit : 0.00...1.00
            float fx1 = x1 / 512.0f;
            float fy1 = (float)y1 / 512.0f;

            float fx2 = x2 / 512.0f;
            float fy2 = y2 / 512.0f;

            Rect coordRect = new Rect(fx1, fy1, fx2, fy2);
            minimapControl.BackTextureCoord = coordRect;

            //    //draw units
            //    Vec2 screenPixel = new Vec2(1, 1) / new Vec2(EngineApp.Instance.VideoMode.Size.ToVec2());
            //    {
            //        ////Loading texture to the engine
            //        //Texture texture = null;
            //        //string mapDirectory = Path.GetDirectoryName(mapName);
            //        //string textureName = mapDirectory + "\\Data\\Minimap";
            //        //string textureFileName = "Minimap";
            //        //bool finded = false;
            //        //string[] extensions = new string[] { "dds", "tga", "png", "jpg" };
            //        //foreach (string extension in extensions)
            //        //{
            //        //    textureFileName = textureName + "." + extension;
            //        //    if (VirtualFile.Exists(textureFileName))
            //        //    {
            //        //        finded = true;
            //        //        break;
            //        //    }
            //        //}
            //        //if (finded)
            //        //    texture = TextureManager.Instance.Load(textureFileName);

            //        Unit playerUnit = GetPlayerUnit();

            //        Rect rect = new Rect(playerUnit.MapBounds.Minimum.ToVec2(),
            //                             playerUnit.MapBounds.Maximum.ToVec2()
            //                             );

            //        rect -= mapRect.Minimum;
            //        rect.Minimum *= mapSizeInv;
            //        rect.Maximum *= mapSizeInv;

            //        rect.Minimum = new Vec2(rect.Minimum.X, 1.0f - rect.Minimum.Y);
            //        rect.Maximum = new Vec2(rect.Maximum.X, 1.0f - rect.Maximum.Y);

            //        rect.Minimum *= screenMapRect.Size;
            //        rect.Maximum *= screenMapRect.Size;

            //        rect += screenMapRect.Minimum;

            //        //increase 1 pixel
            //        rect.Maximum += new Vec2(screenPixel.X, -screenPixel.Y);

            //        ColorValue color;

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

            //        renderer.AddQuad(rect, color);

            //    }

            //    //foreach (Entity entity in Map.Instance.Children)
            //    //{
            //    //    GameCharacter unit = entity as GameCharacter;

            //    //    if (unit == null)
            //    //        continue;

            //    //    Rect rect = new Rect(unit.MapBounds.Minimum.ToVec2(), unit.MapBounds.Maximum.ToVec2());

            //    //    rect -= mapRect.Minimum;
            ////    rect.Minimum *= mapSizeInv;
            ////    rect.Maximum *= mapSizeInv;
            ////    rect.Minimum = new Vec2(rect.Minimum.X, 1.0f - rect.Minimum.Y);
            ////    rect.Maximum = new Vec2(rect.Maximum.X, 1.0f - rect.Maximum.Y);
            ////    rect.Minimum *= screenMapRect.Size;
            ////    rect.Maximum *= screenMapRect.Size;
            ////    rect += screenMapRect.Minimum;

            ////    //increase 1 pixel
            ////    rect.Maximum += new Vec2(screenPixel.X, -screenPixel.Y);

            ////    ColorValue color;

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

            ////    renderer.AddQuad(rect, color);
            ////}

            //    //Draw camera borders
            //    {
            //        //    Camera camera = RendererWorld.Instance.DefaultCamera;

            //        //    if (camera.Position.Z > 0)
            //        //    {

            //        //        Plane groundPlane = new Plane(0, 0, 1, 0);

            //        //        Vec2[] points = new Vec2[4];

            //        //        for (int n = 0; n < 4; n++)
            //        //        {
            //        //            Vec2 p = Vec2.Zero;

            //        //            switch (n)
            //        //            {
            //        //                case 0: p = new Vec2(0, 0); break;
            //        //                case 1: p = new Vec2(1, 0); break;
            //        //                case 2: p = new Vec2(1, 1); break;
            //        //                case 3: p = new Vec2(0, 1); break;
            //        //            }

            //        //            Ray ray = camera.GetCameraToViewportRay(p);

            //        //            float scale;
            //        //            groundPlane.RayIntersection(ray, out scale);

            //        //            Vec3 pos = ray.GetPointOnRay(scale);
            //        //            if (ray.Direction.Z > 0)
            //        //                pos = ray.Origin + ray.Direction.GetNormalize() * 10000;

            //        //            Vec2 point = pos.ToVec2();

            //        //            point -= mapRect.Minimum;
            //        //            point *= mapSizeInv;
            //        //            point = new Vec2(point.X, 1.0f - point.Y);
            //        //            point *= screenMapRect.Size;
            //        //            point += screenMapRect.Minimum;

            //        //            points[n] = point;
            //        //        }

            //        //        for (int n = 0; n < 4; n++)
            //        //            renderer.AddLine(points[n], points[(n + 1) % 4], new ColorValue(1, 1, 1),
            //        //                screenMapRect);
            //        //    }
            //    }
        }
Exemple #27
0
            void UpdateBarControlValue( Control control, float value )
            {
                Rect screenRectangle = control.GetScreenRectangle();
                Vec2 rectSize = screenRectangle.Size;
                float width = rectSize.X / 2 * Math.Abs( value );
                Rect rect;
                if( value > 0 )
                {
                    float left = screenRectangle.Left + rectSize.X / 2;
                    rect = new Rect( left, screenRectangle.Top, left + width, screenRectangle.Bottom );
                }
                else
                {
                    float right = screenRectangle.Left + rectSize.X / 2;
                    rect = new Rect( right - width, screenRectangle.Top, right, screenRectangle.Bottom );
                }

                Control barControl = control.Controls[ "Bar" ];
                barControl.SetScreenClipRectangle( rect );
            }
        //Tank specific
        void DrawTankGunTarget( GuiRenderer renderer )
        {
            Tank tank = GetPlayerUnit() as Tank;
            if( tank == null )
                return;

            Gun gun = tank.MainGun;
            if( gun == null )
                return;

            Vec3 gunPosition = gun.GetInterpolatedPosition();
            Vec3 gunDirection = gun.GetInterpolatedRotation() * new Vec3( 1, 0, 0 );

            RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                new Ray( gunPosition, gunDirection * 1000 ),
                (int)ContactGroup.CastOnlyContact );

            bool finded = false;
            Vec3 pos = Vec3.Zero;

            foreach( RayCastResult result in piercingResult )
            {
                bool ignore = false;

                MapObject obj = MapSystemWorld.GetMapObjectByBody( result.Shape.Body );

                Dynamic dynamic = obj as Dynamic;
                if( dynamic != null && dynamic.GetParentUnit() == tank )
                    ignore = true;

                if( !ignore )
                {
                    finded = true;
                    pos = result.Position;
                    break;
                }
            }

            if( !finded )
                pos = gunPosition + gunDirection * 1000;

            Vec2 screenPos;
            RendererWorld.Instance.DefaultCamera.ProjectToScreenCoordinates( pos, out screenPos );

            //draw quad
            {
                Texture texture = TextureManager.Instance.Load( "Cursors/Target.png" );
                float size = .015f;
                float aspect = RendererWorld.Instance.DefaultCamera.AspectRatio;
                Rect rectangle = new Rect(
                    screenPos.X - size, screenPos.Y - size * aspect,
                    screenPos.X + size, screenPos.Y + size * aspect );
                renderer.AddQuad( rectangle, new Rect( 0, 0, 1, 1 ), texture,
                    new ColorValue( 0, 1, 0 ) );
            }
        }
        void TickMove()
        {
            //path find control
            {
                if (pathFindWaitTime != 0)
                {
                    pathFindWaitTime -= TickDelta;
                    if (pathFindWaitTime < 0)
                        pathFindWaitTime = 0;
                }

                // If a path has not been found to the target and the time to wait is zero
                if (pathFoundedToPosition != MovePosition.ToVec2() && pathFindWaitTime == 0)
                    path.Clear();

                // If we don't have a path
                if (path.Count == 0)
                {
                    if (pathFindWaitTime == 0)
                    {
                        // Find a path to move position
                        if (DoPathFind())
                        {
                            pathFoundedToPosition = MovePosition.ToVec2();
                            pathFindWaitTime = .5f;
                        }
                        else
                        {
                            // If a path has not been found, re-try in 1 second
                            pathFindWaitTime = 1.0f;
                        }
                    }
                }
            }

            if (path.Count == 0)
                return;

            //line movement to path[ 0 ]
            {
                Vec2 destPoint = path[0];
                // The difference between the first point in the path and this object's current position
                Vec2 diff = destPoint - Position.ToVec2();

                if (diff == Vec2.Zero)
                {
                    path.RemoveAt(0);
                    return;
                }

                Radian dir = MathFunctions.ATan16(diff.Y, diff.X);

                float halfAngle = dir * 0.5f;
                Quat rot = new Quat(new Vec3(0, 0, MathFunctions.Sin16(halfAngle)),
                    MathFunctions.Cos16(halfAngle));
                // Rotate the object
                Rotation = rot;
                // Normalise the difference vector
                Vec2 dirVector = diff.GetNormalizeFast();
                // The difference vector multiplied by the velocity per 1/30 second
                Vec2 dirStep = dirVector * (Type.MaxVelocity * TickDelta);
                // Calculate the object's new position
                Vec2 newPos = Position.ToVec2() + dirStep;
                // If the first point in the path is less than the direction step
                if (Math.Abs(diff.X) <= Math.Abs(dirStep.X) && Math.Abs(diff.Y) <= Math.Abs(dirStep.Y))
                {
                    // then move to the first position in the path
                    //unit at point
                    newPos = path[0];
                    path.RemoveAt(0);
                }

                GridPathFindSystem.Instance.RemoveObjectFromMotionMap(this);

                bool free;
                {
                    float radius = Type.Radius;
                    Rect targetRect = new Rect(newPos - new Vec2(radius, radius), newPos + new Vec2(radius, radius));

                    free = GridPathFindSystem.Instance.IsFreeInMapMotion(targetRect);
                }

                GridPathFindSystem.Instance.AddObjectToMotionMap(this);

                if (free)
                {
                    float newZ = GridPathFindSystem.Instance.GetMotionMapHeight(newPos) + Type.Height * .5f;
                    // Set the object's position
                    Position = new Vec3(newPos.X, newPos.Y, newZ);
                }
                else
                    path.Clear();
            }
        }
        /// <summary>
        /// Draw a target at center of screen
        /// </summary>
        /// <param name="renderer"></param>
        void DrawTarget( GuiRenderer renderer )
        {
            Unit playerUnit = GetPlayerUnit();

            Weapon weapon = null;
            {
                //PlayerCharacter specific
                PlayerCharacter playerCharacter = playerUnit as PlayerCharacter;
                if( playerCharacter != null )
                    weapon = playerCharacter.ActiveWeapon;

                //Turret specific
                Turret turret = playerUnit as Turret;
                if( turret != null )
                    weapon = turret.MainGun;

                //Tank specific
                Tank tank = playerUnit as Tank;
                if( tank != null )
                    weapon = tank.MainGun;
            }

            //draw quad
            if( weapon != null || currentAttachedGuiObject != null || currentSwitch != null )
            {
                Texture texture = TextureManager.Instance.Load( "Cursors/Target.png" );
                float size = .02f;
                float aspect = RendererWorld.Instance.DefaultCamera.AspectRatio;
                Rect rectangle = new Rect(
                    .5f - size, .5f - size * aspect,
                    .5f + size, .5f + size * aspect );
                renderer.AddQuad( rectangle, new Rect( 0, 0, 1, 1 ), texture );
            }

            //Tank specific
            DrawTankGunTarget( renderer );
        }