Ejemplo n.º 1
0
        public void MoveTo(int x, int y, bool ignoreblocking = false)
        {
            SetupPathFinder();

            if (Map.Map[x, y].Blocked && !ignoreblocking)
            {
                _currentpath = null;
                return;
            }

            TargetX = x;
            TargetY = y;

            var position = CordUtil.WorldToTile(transform.position);
            var currentx = position.First;
            var currenty = position.Second;

            Pathfinder.ResultFound = r =>
            {
                _currentpath = r;

                if (_currentpath != null)
                {
                    _nextworldpos = CordUtil.TileToWorld(_currentpath.X, _currentpath.Y);
                }
            };

            _currentpath = null;
            Pathfinder.FindPath(currentx, currenty, x, y, this);
        }
Ejemplo n.º 2
0
        public GameObject Create(BuildingType type, int team, int x, int y)
        {
            var building  = Container.CreateEmptyGameObject("Building");
            var selection = Container.CreateEmptyGameObject("SelectionBox");

            selection.transform.SetParent(building.transform);
            selection.transform.localScale = new Vector3(2, 2, 1);

            building.tag = "Building";


            var render = Container.InstantiateComponent <SpriteRenderer>(building);


            render.sprite = _buildingSprite;

            render.sortingOrder = 1;
            var collider     = Container.InstantiateComponent <BoxCollider2D>(building);
            var controller   = Container.InstantiateComponent <BuildingController>(building);
            var selectrender = Container.InstantiateComponent <SpriteRenderer>(selection);

            selectrender.sprite = _SelectionSprite;
            collider.isTrigger  = true;

            building.transform.position = CordUtil.TileToWorld(x, y);

            if (team != 0 && type != BuildingType.Farm)
            {
                var unitbuilder = Container.InstantiateComponent <UnitBuilderAI>(building);
                unitbuilder.Cooldown = 30f;
                unitbuilder.Type     = type == BuildingType.TownCenter ? UnitType.Worker : UnitType.Warrior;
            }

            controller.PrimaryActionList = GetPrimaryActionList(type);
            controller.HP                   = 1;
            controller.MaxHP                = 1;
            controller.PlayerOwner          = team;
            controller.SelectionBox         = selection;
            controller.ConstructionTimeLeft = 10f;
            controller.CompletedSprite      = type == BuildingType.TownCenter ? _cityCenter :
                                              type == BuildingType.Farm ? _farm
                                                      : _barracks;
            if (team != 0)
            {
                render.sprite = controller.CompletedSprite;
            }


            building.SetActive(true);
            selection.SetActive(false);


            return(building);
        }
Ejemplo n.º 3
0
        void FixedUpdate()
        {
            SetupPathFinder();

            if (_currentpath == null)
            {
                return;
            }

            if (Vector3.Distance(_nextworldpos, transform.position) <= _distanceBeforeNext)
            {
                if (_currentpath.Next == null)
                {
                    _currentpath = null;
                    return;
                }


                if (Map.Map[_currentpath.Next.X, _currentpath.Next.Y].Blocked)
                {
                    MoveTo(TargetX, TargetY);
                    return;
                }

                _currentpath = _currentpath.Next;

                SwapPosition(_currentpath.X, _currentpath.Y);

                if (_currentpath == null)
                {
                    return;
                }

                _nextworldpos = CordUtil.TileToWorld(_currentpath.X, _currentpath.Y);
            }

            transform.position = Vector3.MoveTowards(transform.position, _nextworldpos, Speed * Time.fixedDeltaTime);
        }
Ejemplo n.º 4
0
        public void MoveToTile(int x, int y)
        {
            var loc = CordUtil.TileToWorld(x, y);

            Camera.transform.position = new Vector3(loc.x, loc.y, -10);
        }
Ejemplo n.º 5
0
        public GameObject Create(UnitType type, int team, int x, int y)
        {
            var unit         = Container.CreateEmptyGameObject("Unit");
            var head         = Container.CreateEmptyGameObject("Head");
            var body         = Container.CreateEmptyGameObject("Body");
            var selectionBox = Container.CreateEmptyGameObject("Selection");

            unit.tag = "Unit";

            head.transform.parent         = unit.transform;
            body.transform.parent         = unit.transform;
            selectionBox.transform.parent = unit.transform;

            head.transform.position = new Vector3(0, 0.148f, 0);

            var col = unit.AddComponent <BoxCollider2D>();

            col.offset    = new Vector2(-0.0025f, 0.0126f);
            col.size      = new Vector2(0.2365f, 0.5196f);
            col.isTrigger = true;

            var controller = Container.InstantiateComponent <UnitController>(unit);
            var pathfinder = Container.InstantiateComponent <PathFinderFollower>(unit);

            controller.SelectionBox      = selectionBox;
            controller.PlayerOwner       = team;
            controller.HP                = 1;
            controller.MaxHP             = 1;
            controller.PrimaryActionList = GetPrimaryActions(type);

            if (team != 0 && type == UnitType.Worker)
            {
                Container.InstantiateComponent <HarvestAIController>(unit);
            }

            if (type == UnitType.Warrior)
            {
                controller.CanAttack = true;
                var attacksight = Container.CreateEmptyGameObject("Sight");
                attacksight.transform.SetParent(unit.transform);
                var sightcol = Container.InstantiateComponent <CircleCollider2D>(attacksight);
                Container.InstantiateComponent <AttackSight>(attacksight);
                sightcol.radius                = 2.5f;
                sightcol.isTrigger             = true;
                attacksight.transform.position = new Vector3(attacksight.transform.position.x, attacksight.transform.position.y, 5);

                var spear = Container.CreateEmptyGameObject("Spear");
                spear.transform.SetParent(unit.transform);
                var spearrender = Container.InstantiateComponent <SpriteRenderer>(spear);
                spearrender.sortingOrder       = 5;
                spearrender.sprite             = _spearSprite;
                spearrender.transform.position = new Vector3(0.06f, 0, 0);
            }

            if (type == UnitType.Worker)
            {
                controller.CanHarvest = true;
            }

            var headsprite      = Container.InstantiateComponent <SpriteRenderer>(head);
            var bodysprite      = Container.InstantiateComponent <SpriteRenderer>(body);
            var selectionsprite = Container.InstantiateComponent <SpriteRenderer>(selectionBox);
            var rbody           = Container.InstantiateComponent <Rigidbody2D>(unit);

            rbody.gravityScale   = 0f;
            rbody.freezeRotation = true;
            rbody.isKinematic    = true;

            selectionsprite.sortingOrder     = 3;
            headsprite.sortingOrder          = 2;
            bodysprite.sortingOrder          = 1;
            headsprite.sortingLayerName      = "Units";
            bodysprite.sortingLayerName      = "Units";
            selectionsprite.sortingLayerName = "Units";

            selectionsprite.sprite = _selectionSprite;
            bodysprite.sprite      = _teamBodies[team];
            headsprite.sprite      = _heads[0];

            selectionsprite.gameObject.SetActive(false);
            head.gameObject.SetActive(true);
            body.gameObject.SetActive(true);
            unit.gameObject.SetActive(true);

            unit.transform.position = CordUtil.TileToWorld(x, y);

            pathfinder.CurrentX = x;
            pathfinder.CurrentY = y;
            pathfinder.SwapPosition(x, y);

            return(unit);
        }