public void Should_Get_Terrain_Behaviour_By_Id_Return_Expected_Value()
        {
            ITerrainNode[,] terrainArray = new ITerrainNode[Rows,Cols];
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    terrainArray[i,j] = MockRepository.GenerateMock<ITerrainNode>();
                    var terrainBehaviour = MockRepository.GenerateMock<ITerrainBehaviour>();
                    terrainBehaviour.Stub(x => x.RowIndex).Return(i);
                    terrainBehaviour.Stub(x => x.ColsIndex).Return(j);
                    terrainBehaviour.Stub(x => x.GetTerrainId()).Return(i*100 + j);
                    terrainArray[i, j].Stub(x => x.Item).Return(terrainBehaviour);
                }
            }
            var nodeContainer = MockRepository.GenerateMock<ITerrainNodeContainer>();
            nodeContainer.Stub(x => x.TerrainNodesArray).Return(terrainArray);
            _nodesFactory.Stub(x => x.CreateNodeContainer(Arg<ITerrainBehaviour[,]>.Is.Anything)).Return(nodeContainer);

            ITerrainService testedClass = new TerrainService(_nodesFactory, _searchPathStrategy);
            var terrains = new ITerrainBehaviour[1];
            terrains[0] = MockRepository.GenerateMock<ITerrainBehaviour>();
            testedClass.NetworkTerrainFinishGeneration(terrains, 1, 1);
            var terrain = testedClass.GetTerrainBehaviourById(5 * 100 + 7);
            Assert.AreEqual(terrain.RowIndex, 5);
            Assert.AreEqual(terrain.ColsIndex, 7);
        }
 public void Initialize(IFigureBehaviour figureBehaviour, ITerrainBehaviour currentTerrainBehaviour)
 {
     _currentTerrainBehaviour = currentTerrainBehaviour;
     _figureBehaviour = figureBehaviour;
     var position = _currentTerrainBehaviour.GetComponent<Transform>().position;
     _figureBehaviour.GetComponent<Transform>().position = position + Movement;
 }
 public void ChangeTargetTerrain(int terrainId)
 {
     var terrainBehaviour = _terrainService.GetTerrainBehaviourById(terrainId);
     _canMove = true;
     _pathCollection =
     new List<ITerrainBehaviour>(_terrainService.Search(_currentTerrainBehaviour, terrainBehaviour));
     _pathCollection.RemoveAt(0);
     _targetTerrainBehaviour = _pathCollection.First();
     _pathCollection.RemoveAt(0);
 }
 public IEnumerable<ITerrainBehaviour> Search(ITerrainBehaviour startTerrainBehaviour, 
                                              ITerrainBehaviour targetTerrainBehaviour,
                                              ITerrainNodeContainer terrainContainer)
 {
     var coastArray = new int[terrainContainer.RowsLength, terrainContainer.ColumnsLength];
     coastArray[startTerrainBehaviour.RowIndex, startTerrainBehaviour.ColsIndex] = startTerrainBehaviour.MoveCoast;
     var resoult = FindPath(startTerrainBehaviour, targetTerrainBehaviour, terrainContainer, coastArray);
     resoult.Reverse();
     return resoult;
 }
        public void Generate(ICollection<GameObject> terrainsCollection, int rows, int columns)
        {
            TerrainStartGeneration?.Invoke(this, EventArgs.Empty);
            const float xMovement = -1.728f;
            const int zMovement = 3;

            var currentXMovement = terrainsCollection.First().transform.position.x;
            var currentZMovement = terrainsCollection.First().transform.position.z;

            var currentXMovement2 = terrainsCollection.First().transform.position.x + xMovement / 2;
            var currentZMovement2 = terrainsCollection.First().transform.position.z + 1.5f;

            var terrains = new ITerrainBehaviour[rows * 2, columns];
            var array = terrainsCollection.ToArray();

            for (int i = 0; i < rows * 2; i = i + 2)
            {
                for (int j = 0; j < columns; j++)
                {
                    currentZMovement += zMovement;
                    var cloneObject = (GameObject)GameObject.Instantiate(array[_random.Next(3)], new Vector3(currentXMovement,
                                                             0,
                                                             currentZMovement),
                                                 Quaternion.identity);
                    var terrainBehaviour = cloneObject.GetComponent<TerrainBehaviour>();
                    terrainBehaviour.RowIndex = i;
                    terrainBehaviour.ColsIndex = j;

                    terrains[i, j] = terrainBehaviour;

                    CmdSpawnInstantiateObject(cloneObject);

                    cloneObject = (GameObject)GameObject.Instantiate(array[_random.Next(3)], new Vector3(currentXMovement2,
                                                             0,
                                                             currentZMovement2),
                                                 Quaternion.identity);
                    terrainBehaviour = cloneObject.GetComponent<TerrainBehaviour>();
                    terrainBehaviour.RowIndex = i + 1;
                    terrainBehaviour.ColsIndex = j;

                    CmdSpawnInstantiateObject(cloneObject);

                    terrains[i + 1, j] = terrainBehaviour;

                    currentZMovement2 += zMovement;
                }

                currentZMovement = terrainsCollection.First().transform.position.z;
                currentXMovement += xMovement;

                currentZMovement2 = terrainsCollection.First().transform.position.z + 1.5f;
                currentXMovement2 += xMovement;
            }
        }
        protected override void Start()
        {
            base.Start();
            MoveProvaider = Container.Resolve<IFigureMoveProvaider>();
            base.Start();
            if (!isServer)
            {
                MoveProvaider.Initialize(this, _currentTerrainBehaviour);
                return;
            }

            _terrainService = Container.Resolve<ITerrainService>();
            _currentTerrainBehaviour = _terrainService.GetTerrainBehaviour(_random.Next(1, 5), _random.Next(1, 5));
            MoveProvaider.Initialize(this, _currentTerrainBehaviour);
        }
        private ITerrainNode[,] Initialize(ITerrainBehaviour[,] terrainBehavioursArray, int rowsLength, int columnsLength)
        {
            var nodes = new TerrainNode[rowsLength, columnsLength];

            for (int i = 0; i < rowsLength; i++)
            {
                for (int j = 0; j < columnsLength; j++)
                {
                    nodes[i, j] = new TerrainNode();
                }
            }

            for (int i = 0; i < rowsLength; i++)
            {
                for (int j = 0; j < columnsLength; j++)
                {
                    nodes[i, j].InitializeNode(terrainBehavioursArray[i, j], nodes);
                }
            }

            return nodes;
        }
 public void ExecuteFixedUpdate()
 {
     if (_canMove && _figureBehaviour.GetComponent<Transform>().position != _targetTerrainBehaviour.GetComponent<Transform>().position)
     {
         _figureBehaviour.GetComponent<Transform>().position = Vector3.MoveTowards(_figureBehaviour.GetComponent<Transform>().position,
                                                                 _targetTerrainBehaviour.GetComponent<Transform>().position + Movement, 0.1f);
     }
     if (_targetTerrainBehaviour != null && _figureBehaviour.GetComponent<Transform>().position == _targetTerrainBehaviour.GetComponent<Transform>().position + Movement)
     {
         if (_pathCollection.Any())
         {
             _targetTerrainBehaviour = _pathCollection.First();
             _currentTerrainBehaviour = _targetTerrainBehaviour;
             _pathCollection.RemoveAt(0);
         }
         else
         {
             _targetTerrainBehaviour = null;
             _canMove = false;
         }
     }
 }
 private ITerrainNode[,] CreateNode(ITerrainBehaviour[,] terrainBehavioursbehaviours)
 {
     var rowsLength = terrainBehavioursbehaviours.GetLength(X_DIMENDION);
     var columnsLength = terrainBehavioursbehaviours.GetLength(Z_DIMENSION);
     return Initialize(terrainBehavioursbehaviours,rowsLength,columnsLength);
 }
 public ITerrainNodeContainer CreateNodeContainer(ITerrainBehaviour[,] terrainBehavioursbehaviours)
 {
     return new TerrainNodeContainer(CreateNode(terrainBehavioursbehaviours));
 }
 public bool CanBuildTown(ITerrainBehaviour targetTerrainBehaviour)
 {
     var row = targetTerrainBehaviour.RowIndex;
     var col = targetTerrainBehaviour.ColsIndex;
     return _buidlerTownStrategy.CanBuildTown(_terrainService.GetTerrainNode(row,col));
 }
 public void Test_Netowrk_Terrain_Finish_Generation()
 {
     _nodesFactory.Stub(x => x.CreateNodeContainer(Arg<ITerrainBehaviour[,]>.Is.Anything)).Repeat.Once();
     ITerrainService testedClass = new TerrainService(_nodesFactory, _searchPathStrategy);
     var terrains = new ITerrainBehaviour[1];
     terrains[0] = MockRepository.GenerateMock<ITerrainBehaviour>();
     testedClass.NetworkTerrainFinishGeneration(terrains, 1, 1);
     _nodesFactory.VerifyAllExpectations();
 }
 public abstract void OnTerrainClick(ITerrainBehaviour terrainBehaviour);
        private List<ITerrainBehaviour> FindPath(ITerrainBehaviour startTerrainBehaviour, ITerrainBehaviour targetTerrainBehaviour, ITerrainNodeContainer terrainContainer, int[,] coastArray)
        {
            var queue = new List<ITerrainNode>()
            {
                terrainContainer.TerrainNodesArray[startTerrainBehaviour.RowIndex, startTerrainBehaviour.ColsIndex]
            };

            var canFinish = false;

            do
            {
                var current = queue.First();
                queue.RemoveAt(0);

                foreach (var terrainNode in current.Nodes)
                {
                    if (terrainNode.Item.TerrainType == TerrainType.Land)
                    {
                        var row = terrainNode.RowIndex;
                        var column = terrainNode.ColumnIndex;
                        var potential = coastArray[current.RowIndex, current.ColumnIndex] + terrainNode.Item.MoveCoast;
                        if (coastArray[row, column] == 0 || potential < coastArray[row, column])
                        {
                            coastArray[row, column] = potential;
                            queue.Add(terrainNode);
                        }
                    }
                }

            } while (queue.Any());

            var pathCollection = new List<ITerrainBehaviour>() {targetTerrainBehaviour};
            canFinish = false;

            var item =
                terrainContainer.TerrainNodesArray[targetTerrainBehaviour.RowIndex, targetTerrainBehaviour.ColsIndex];
            ITerrainNode nextNode = null;
            while (!canFinish)
            {
                var min = coastArray[item.RowIndex, item.ColumnIndex];

                foreach (var terrainNode in item.Nodes)
                {
                    var row = terrainNode.RowIndex;
                    var column = terrainNode.ColumnIndex;
                    if (coastArray[row, column] < min && coastArray[row, column] > 0)
                    {
                        nextNode = terrainNode;
                        min = coastArray[row, column];
                        if (terrainNode.Item == startTerrainBehaviour)
                        {
                            canFinish = true;
                            break;
                        }
                    }
                }
                item = nextNode;
                pathCollection.Add(item.Item);
            }
            return pathCollection;
        }
 public override void OnTerrainClick(ITerrainBehaviour terrainBehaviour)
 {
     var player = Container.Resolve<ISession>().PlayerController;
     player.FigureServerController.InvokeFigureMove(netId.Value,terrainBehaviour.GetTerrainId());
 }
Beispiel #16
0
 public void InitializeNode(ITerrainBehaviour item, ITerrainNode[,] nodesArray)
 {
     Item = item;
     Initialize(nodesArray);
 }