GameEntities.RTSBuilding.CreateProductedUnit C# (CSharp) Method

CreateProductedUnit() private method

private CreateProductedUnit ( ) : void
return void
        void CreateProductedUnit()
        {
            RTSUnit unit = (RTSUnit)Entities.Instance.Create( productUnitType, Map.Instance );

            if (unit.Type.Name == "ForagerAnt")
            {
                ForagerAnt character;
                character = unit as ForagerAnt;

                if (character == null)
                {
                    Log.Fatal("RTSBuilding: CreateProductedUnit: character == null");
                }

                Vec2 p = GridPathFindSystem.Instance.GetNearestFreePosition(Position.ToVec2(),
                    character.Type.Radius * 2);
                unit.Position = new Vec3(p.X, p.Y, GridPathFindSystem.Instance.GetMotionMapHeight(p) +
                    character.Type.Height * .5f);

                if (Intellect != null)
                    unit.InitialFaction = Intellect.Faction;

                unit.PostCreate();
                // Move the unit to the gather point
                AntUnitAI intellect = unit.Intellect as AntUnitAI;
                if (intellect != null &&
                    this.GatherPoint.X != 0 && this.GatherPoint.Y != 0 && this.GatherPoint.Z != 0)
                {
                    // Paths cannot be found to locations on the map already occupied by another ant,
                    // randomly generate a new gather point within 15 units of the original gather point
                    Random rand = new Random();
                    Vec3 newGatherPoint = new Vec3(this.GatherPoint.X + ((float)rand.NextDouble() * 30f - 15f),
                        this.GatherPoint.Y + ((float)rand.NextDouble() * 30f - 15f), this.GatherPoint.Z);
                    intellect.DoTask(new AntUnitAI.Task(AntUnitAI.Task.Types.Move, newGatherPoint),
                        false);
                }

            }
            else
            {
                GenericAntCharacter character;
                character = unit as GenericAntCharacter;

                if (character == null)
                {
                    Log.Fatal("RTSBuilding: CreateProductedUnit: character == null");
                }

                Vec2 p = GridPathFindSystem.Instance.GetNearestFreePosition(Position.ToVec2(),
                    character.Type.Radius * 2);
                unit.Position = new Vec3(p.X, p.Y, GridPathFindSystem.Instance.GetMotionMapHeight(p) +
                    character.Type.Height * .5f);

                if (Intellect != null)
                    unit.InitialFaction = Intellect.Faction;

                unit.PostCreate();
                // Move the unit to the gather point
                AntUnitAI intellect = unit.Intellect as AntUnitAI;
                if (intellect != null &&
                    this.GatherPoint.X != 0 && this.GatherPoint.Y != 0 && this.GatherPoint.Z != 0)
                {
                    // Paths cannot be found to locations on the map already occupied by another ant,
                    // randomly generate a new gather point within 15 units of the original gather point
                    Random rand = new Random();
                    Vec3 newGatherPoint = new Vec3(this.GatherPoint.X + ((float)rand.NextDouble() * 30f - 15f),
                        this.GatherPoint.Y + ((float)rand.NextDouble() * 30f - 15f), this.GatherPoint.Z);
                    intellect.DoTask(new AntUnitAI.Task(AntUnitAI.Task.Types.Move, newGatherPoint),
                        false);
                }
            }
        }