Ejemplo n.º 1
0
        private void AddTask(GameState s, RTSUnit unit)
        {
            // Init The Unit
            if (unit.CombatController != null)
            {
                unit.CombatController.Init(s, this, unit.Data.CombatControllerInitArgs);
            }
            if (unit.MovementController != null)
            {
                unit.MovementController.Init(s, this, unit.Data.MovementControllerInitArgs);
            }
            if (unit.AnimationController != null)
            {
                unit.AnimationController.Init(s, this, unit.Data.AnimationControllerInitArgs);
            }
            if (unit.ActionController != null)
            {
                unit.ActionController.Init(s, this, unit.Data.ActionControllerInitArgs);
            }

            var btu = new BTaskUnitDecision(s, unit);

            unit.OnDestruction += (o) => {
                tbEntityDecisions.RemoveTask(btu);
            };
            tbEntityDecisions.AddTask(btu);
        }
    private void OnTriggerExit(Collider other)
    {
        if (other.GetComponent <RTSUnit>() != null || other.tag == "Finish")
        {
            int teamID;
            if (other.tag == "Finish")
            {
                teamID = 1;
            }
            else
            {
                RTSUnit unit = other.GetComponent <RTSUnit>();
                teamID = unit.teamID;
            }

            foreach (RTSGridObserver ro in rtsGridObservers)
            {
                ro.RegisterUnitExit(teamID, row, col);
            }
        }
        if (other.GetComponent <NavMeshObstacle>())
        {
            NavMeshObstacle Obstacle = other.GetComponent <NavMeshObstacle>();
            foreach (RTSGridObserver ro in rtsGridObservers)
            {
                ro.RegisterObstacleExit(row, col);
            }
        }
    }
Ejemplo n.º 3
0
        private void SendSquadQuery(GameState s, RTSSquad squad, GameInputEvent e)
        {
            squad.RecalculateGridPosition();
            Vector2 start = squad.GridPosition;
            var     swe   = e as SetWayPointEvent;
            var     ste   = e as SetTargetEvent;
            Vector2 goal  = start;

            if (swe != null)
            {
                goal = swe.Waypoint;
            }
            else if (ste != null && ste.Target != null)
            {
                goal = ste.Target.GridPosition;
            }
            float minDistSq = float.MaxValue;

            for (int u = 0; u < squad.Units.Count; u++)
            {
                RTSUnit unit   = squad.Units[u];
                float   distSq = (goal - unit.GridPosition).LengthSquared();
                if (distSq < minDistSq)
                {
                    minDistSq = distSq;
                    start     = unit.GridPosition;
                }
            }
            var query = pathfinder.ReissuePathQuery(new PathQuery(start, goal, e.Team), start, goal, e.Team);

            squadQueries.Add(new SquadQuery(squad, query));
        }
    /*
     * public void ReceiveDamage(int damage)
     * {
     *  health -= damage;
     *  AddReward(-2.0f);
     *  if (health <= 0)
     *  {
     *      print("I DIED");
     *  }
     * }
     */

    public void HitEnemy(RTSUnit enemyUnit)
    {
        AddReward(rewardMax);

        EndEpisode();
        // enemyAgent.ReceiveDamage(1);
    }
Ejemplo n.º 5
0
 public void OnUnitSpawn(RTSUnit u)
 {
     if (u.Data == Data)
     {
         instances.Add(u);
     }
 }
Ejemplo n.º 6
0
 public override void SetUnit(RTSUnit u)
 {
     base.SetUnit(u);
     if (unit != null)
     {
         unit.OnAttackMade += unit_OnAttackMade;
     }
 }
Ejemplo n.º 7
0
        private void OnUnitSpawn(RTSUnit u)
        {
            ImpactRegion r = FindRegion(u);

            r.Selected.Add(u);
            r.PopCount++;
            u.OnDestruction += OnUnitDeath;
        }
Ejemplo n.º 8
0
    private void OnTriggerEnter(Collider other)
    {
        RTSUnit otherUnit = other.GetComponent <RTSUnit>();

        if (other.tag == "Finish")
        {
            parentMasterAgent.HitEnemy(otherUnit);
        }
    }
Ejemplo n.º 9
0
 public override void SetUnit(RTSUnit u)
 {
     base.SetUnit(u);
     if (unit != null)
     {
         unit.OnAttackMade  += unit_OnAttackMade;
         unit.OnDestruction += unit_OnDestruction;
         unit.OnDamage      += unit_OnDamage;
     }
 }
Ejemplo n.º 10
0
        public void Add(RTSUnit o)
        {
            Point p = HashHelper.Hash(o.CollisionGeometry.Center, numCells, size);

            // Check If Active
            if (EDynamic[p.X, p.Y].Count < 1)
            {
                ActiveGrids.Add(p);
            }
            EDynamic[p.X, p.Y].Add(o);
        }
Ejemplo n.º 11
0
 public virtual void SetUnit(RTSUnit u)
 {
     if (u == null)
     {
         return;
     }
     if (unit != null)
     {
         throw new ArgumentException("Cannot Rebind This Controller To Another Unit");
     }
     unit = u;
     return;
 }
Ejemplo n.º 12
0
 public override void SetUnit(RTSUnit u)
 {
     base.SetUnit(u);
     if (unit != null)
     {
         // Prevent Units From Running Toward The Location Of A Killed Target
         unit.OnNewTarget += (S, T) => {
             if (mc != null && T == null)
             {
                 mc.Waypoints = null;
             }
         };
     }
 }
Ejemplo n.º 13
0
        public void OnUnitSpawn(RTSUnit u)
        {
            DevConsole.AddCommand("spawn");
            Point       cc = HashHelper.Hash(u.GridPosition, GameState.CGrid.numCells, GameState.CGrid.size);
            RTSBuilding b  = GameState.CGrid.EStatic[cc.X, cc.Y];

            if (b != null)
            {
                foreach (var bc in barracksControllers)
                {
                    if (b.UUID == bc.barracks.UUID)
                    {
                        bc.army.Add(u);
                        u.OnDestruction += bc.OnUnitDeath;
                    }
                }
            }
        }
Ejemplo n.º 14
0
        public void OnKeyPress(object sender, KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
            case Keys.Delete:
                var arr = selected.ToArray();
                foreach (var e in arr)
                {
                    AddEvent(new DamageEvent(TeamIndex, e.UUID, 1000000));
                }
                break;

            case Keys.LeftShift:
            case Keys.RightShift:
                isShiftPressed = true;
                break;

            case Keys.Escape:
                buildingToPlace = null;
                break;

            case Keys.K:
                foreach (var entity in selected.ToArray())
                {
                    RTSUnit unit = entity as RTSUnit;
                    if (unit != null)
                    {
                        AddEvent(new SetOrdersEvent(TeamIndex, unit.UUID, BehaviorFSM.AttackMove, 3));
                    }
                }
                break;

            case Keys.M:
                foreach (var entity in selected.ToArray())
                {
                    RTSUnit unit = entity as RTSUnit;
                    if (unit != null)
                    {
                        AddEvent(new SetOrdersEvent(TeamIndex, unit.UUID, BehaviorFSM.JustMove, 3));
                    }
                }
                break;
            }
        }
Ejemplo n.º 15
0
        private void ApplyInput(GameState s, float dt, SetTargetEvent e)
        {
            RTSTeam        team     = s.teams[e.Team];
            List <IEntity> selected = team.Input.selected;

            if (selected != null && selected.Count > 0)
            {
                RTSSquad squad = null;
                foreach (var unit in selected)
                {
                    RTSUnit u = unit as RTSUnit;
                    if (u != null)
                    {
                        if (squad == null)
                        {
                            squad = u.Team.AddSquad();
                        }
                        if (u.ActionController != null)
                        {
                            u.ActionController.Reset();
                        }
                        squad.Add(u);
                        DevConsole.AddCommand("gc 1");
                    }
                }
                if (squad == null)
                {
                    DevConsole.AddCommand("gc return"); return;
                }
                // Assign The Target To Every Unit In The Squad
                for (int u = 0; u < squad.Units.Count; u++)
                {
                    RTSUnit unit = squad.Units[u];
                    unit.Target = e.Target;
                    DevConsole.AddCommand("gc target");
                }
                AddTask(s, squad);
                SendSquadQuery(s, squad, e);
            }
        }
Ejemplo n.º 16
0
        private void ApplyInput(GameState s, float dt, SpawnUnitEvent e)
        {
            RTSTeam team = s.teams[e.Team];
            RTSUnit unit = team.AddUnit(e.Type, e.Position);

            // Check If A Unit Was Possible
            if (unit == null)
            {
                return;
            }
            s.EntityHashSet.Add(unit.UUID, unit);

            // Add Decision Tasks
            AddTask(s, unit);

            // Add A Single Unit Squad
            RTSSquad squad = team.AddSquad();

            squad.Add(unit);
            squad.RecalculateGridPosition();
            AddTask(s, squad);
        }
Ejemplo n.º 17
0
        public void SetData(RTSUnit u)
        {
            if (prevUnit != null)
            {
                prevUnit.OnDamage -= u_OnDamage;
            }
            prevUnit = u;

            u.OnDamage          += u_OnDamage;
            rectHealthFore.Width = (int)(u.GetHealthRatio() * uic.IconSize);
            rectHealthFore.Color = Color.Lerp(uic.HealthMinColor, uic.HealthMaxColor, u.GetHealthRatio());

            Texture2D t;

            if (iconLib.TryGetValue(u.IconKey, out t))
            {
                icon.Texture = t;
            }

            txtName.Text = u.Data.FriendlyName;
            uiCStats.SetStats(u.Data.BaseCombatData);
        }
Ejemplo n.º 18
0
        private void UnitLoader(object _fi)
        {
            FileInfo fi = _fi as FileInfo;

            GameState state = new GameState();

            state.teams[0] = new RTSTeam(0, RTSInputType.None);
            state.UpdateActiveTeams();
            RTSUnitData _unitData = RTSUnitDataParser.ParseData(GameEngine.Scripts, fi, 0);

            state.teams[0].Race.Units[0] = _unitData;
            state.teams[0].Race.UpdateActiveUnits();
            RTSUnitModel _unitModel = RTSUnitDataParser.ParseModel(renderer, fi, state.teams[0].Race);

            _unitModel.Hook(renderer, state, 0, 0);
            RTSUnit _unit = new RTSUnit(state.teams[0], state.teams[0].Race.Units[0], Vector2.Zero);

            _unit.Height = 0;
            _unitModel.OnUnitSpawn(_unit);

            // Create The Full Animation Loop
            _unit.AnimationController = new BlankAnimController(0, (_unitModel.AnimationTexture.Height / 3) - 1, 30f);

            // Make Sure To Only Modify At A Specific Point
            lock (drawLock) {
                // Check Previous Dispose
                if (unit != null)
                {
                    DisposeUnit();
                }

                // Now Set Data
                unitData  = _unitData;
                unit      = _unit;
                unitModel = _unitModel;
            }
        }
Ejemplo n.º 19
0
        private void ApplyInput(GameState s, float dt, SetWayPointEvent e)
        {
            RTSTeam        team     = s.teams[e.Team];
            List <IEntity> selected = team.Input.selected;
            RTSSquad       squad    = null;

            if (selected != null && selected.Count > 0)
            {
                foreach (var unit in selected)
                {
                    RTSUnit u = unit as RTSUnit;
                    if (u != null)
                    {
                        if (squad == null)
                        {
                            squad = u.Team.AddSquad();
                        }
                        u.Target = null;
                        if (u.MovementController != null)
                        {
                            u.MovementController.Goal = e.Waypoint;
                        }
                        if (u.ActionController != null)
                        {
                            u.ActionController.Reset();
                        }
                        squad.Add(u);
                    }
                }
            }
            if (squad != null)
            {
                AddTask(s, squad);
                SendSquadQuery(s, squad, e);
            }
        }
Ejemplo n.º 20
0
        public void OnNewSelection(ACInputController ic, List <IEntity> entities)
        {
            Clear();
            if (IconLibrary == null)
            {
                return;
            }

            var units     = new Dictionary <RTSUnitData, List <IEntity> >();
            var buildings = new Dictionary <RTSBuildingData, List <IEntity> >();

            for (int i = 0; i < entities.Count; i++)
            {
                RTSUnit u = entities[i] as RTSUnit;
                if (u != null)
                {
                    if (units.ContainsKey(u.Data))
                    {
                        units[u.Data].Add(u);
                    }
                    else
                    {
                        var lu = new List <IEntity>();
                        lu.Add(u);
                        units.Add(u.Data, lu);
                    }
                }
                else
                {
                    RTSBuilding b = entities[i] as RTSBuilding;
                    if (buildings.ContainsKey(b.Data))
                    {
                        buildings[b.Data].Add(b);
                    }
                    else
                    {
                        var lb = new List <IEntity>();
                        lb.Add(b);
                        buildings.Add(b.Data, lb);
                    }
                }
            }

            int wi = 0;

            foreach (var kv in buildings)
            {
                if (wi >= groups.Length)
                {
                    break;
                }
                Texture2D t = IconLibrary[kv.Key.IconKey];
                if (t == null)
                {
                    continue;
                }
                groups[wi].Widget.Texture = t;
                groups[wi].Data           = kv.Value;
                Show(groups[wi]);
                wi++;
            }
            foreach (var kv in units)
            {
                if (wi >= groups.Length)
                {
                    break;
                }
                Texture2D t = IconLibrary[kv.Key.IconKey];
                if (t == null)
                {
                    continue;
                }
                groups[wi].Widget.Texture = t;
                groups[wi].Data           = kv.Value;
                Show(groups[wi]);
                wi++;
            }
        }
Ejemplo n.º 21
0
 public T SetUnit <T>(RTSUnit u) where T : ACUnitController
 {
     SetUnit(u);
     return(this as T);
 }
Ejemplo n.º 22
0
 private void DisposeUnit()
 {
     unit      = null;
     unitData  = null;
     unitModel = null;
 }
Ejemplo n.º 23
0
        private IEntity SelectFromRay(Ray r)
        {
            BoundingBox box;
            IEntity     target = null;
            float?      dist;
            float       closest = float.MaxValue;

            // Loop Through All The Teams
            for (int i = 0; i < GameState.activeTeams.Length; i++)
            {
                int     ti   = GameState.activeTeams[i].Index;
                RTSTeam team = GameState.activeTeams[i].Team;

                // Loop Through All The Units
                for (int ui = 0; ui < team.Units.Count; ui++)
                {
                    RTSUnit  unit = team.Units[ui];
                    FogOfWar f    = GameState.CGrid.GetFogOfWar(unit.GridPosition, TeamIndex);
                    if (f != FogOfWar.Active)
                    {
                        continue;
                    }

                    box  = unit.BBox;
                    dist = r.Intersects(box);
                    if (dist != null && dist.Value < closest)
                    {
                        closest = dist.Value;
                        target  = unit;
                    }
                }

                // Loop Through All The Buildings
                for (int bi = 0; bi < team.Buildings.Count; bi++)
                {
                    RTSBuilding building = team.Buildings[bi];
                    FogOfWar    f        = FogOfWar.Nothing;
                    Point       p        = HashHelper.Hash(building.GridPosition, GameState.CGrid.numCells, GameState.CGrid.size);
                    for (int y = 0; y < building.Data.GridSize.Y; y++)
                    {
                        for (int x = 0; x < building.Data.GridSize.X; x++)
                        {
                            f = GameState.CGrid.GetFogOfWar(p.X + x, p.Y + y, TeamIndex);
                            if (f == FogOfWar.Active)
                            {
                                break;
                            }
                        }
                        if (f == FogOfWar.Active)
                        {
                            break;
                        }
                    }
                    if (f != FogOfWar.Active)
                    {
                        continue;
                    }

                    box  = building.BBox;
                    dist = r.Intersects(box);
                    if (dist != null && dist.Value < closest)
                    {
                        closest = dist.Value;
                        target  = building;
                    }
                }
            }
            return(target);
        }
Ejemplo n.º 24
0
 public static bool IsUnitDead(RTSUnit u)
 {
     return(IsEntityDead(u));
 }
Ejemplo n.º 25
0
    private void Update()
    {
        Vector3 currentPosition = UtilsClass.GetMouseWorldPosition();

        //LMB
        if (Input.GetMouseButtonDown(0))
        {
            //Left mouse Button press
            startPosition = UtilsClass.GetMouseWorldPosition();
            mousedown     = true;
        }
        if (Input.GetMouseButtonUp(0))
        {
            //left Mouse Button Released
            mousedown = false;
            //make selection box disappear
            SelectionBox.SetActive(false);
            Collider2D[] collider2DArray = Physics2D.OverlapAreaAll(startPosition, currentPosition);
            foreach (RTSUnit RTSunit in selectedRTSUnitList)
            {
                RTSunit.SetSelectedVisible(false);
            }
            selectedRTSUnitList.Clear();


            foreach (Collider2D collider2D in collider2DArray)
            {
                RTSUnit RTSunit = collider2D.GetComponent <RTSUnit>();
                if (RTSunit != null)
                {
                    RTSunit.SetSelectedVisible(true);
                    selectedRTSUnitList.Add(RTSunit);
                }
            }
            Debug.Log(selectedRTSUnitList.Count);
        }
        //if lmb held down
        if (mousedown && startPosition != currentPosition)
        {
            DrawSelectionBox(startPosition, currentPosition);
        }

        //RMB
        if (Input.GetMouseButtonDown(1))
        {
            //right Mouse Button press
            nextPositionLeft = UtilsClass.GetMouseWorldPosition();
        }
        if (Input.GetMouseButtonUp(1))
        {
            //right Mouse Button Released
            nextPositionRight = UtilsClass.GetMouseWorldPosition();
            if (selectedRTSUnitList.Count == 1)
            {
                selectedRTSUnitList[0].PathTo(nextPositionLeft);
            }
            if (selectedRTSUnitList.Count > 1)
            {
            }
        }
    }
Ejemplo n.º 26
0
 public BTaskUnitDecision(GameState g, RTSUnit u) :
     base(1)
 {
     unit  = u;
     state = g;
 }