private void InitializeTiles(int boardSize) { BoardContainer.Width = TileSize * (vm.game.Board.GetUpperBound(0) + 1) + (2 * BoardContainer.StrokeThickness); BoardContainer.Height = TileSize * (vm.game.Board.GetUpperBound(0) + 1) + (2 * BoardContainer.StrokeThickness); // Generate tiles for selected game vm.game.Board.ForEachCell((i, j) => { ContentControl c = GenerateTile(i, j); RegisterControl(c); }); NextTile.ApplyTemplate(); }
public void CalcPlayerJumpPath(Vector3 p_playerDir) { Vector3 __targetPos = playerGO.transform.localPosition + p_playerDir; positions = new List <MovimentPosition> (); if (HasWallInPosition(__targetPos + Vector3.up, 0.3f) || HasStairInPosition(__targetPos + Vector3.up, 0.3f)) { currentJumpType = JumpType.VERTICAL_JUMP; positions.Add(new MovimentPosition(playerGO.transform.localPosition, 0f)); positions.Add(new MovimentPosition(playerGO.transform.localPosition, 1f)); positions.Add(new MovimentPosition(playerGO.transform.localPosition, 1f)); } else if (HasWallInPosition(__targetPos + p_playerDir + Vector3.up, 0.3f)) { currentJumpType = JumpType.HALF_JUMP; positions.Add(new MovimentPosition(playerGO.transform.localPosition, 0f)); positions.Add(new MovimentPosition(__targetPos + Vector3.up, 1f)); positions.Add(new MovimentPosition(__targetPos, 1f)); } else if (HasStairInPosition(__targetPos + p_playerDir + (Vector3.up * 0.5f), 0.3f)) { currentJumpType = JumpType.LAND_ON_STAIR; positions.Add(new MovimentPosition(playerGO.transform.localPosition, 0f)); positions.Add(new MovimentPosition(__targetPos + Vector3.up, 1f)); positions.Add(new MovimentPosition(__targetPos + p_playerDir + (Vector3.up * 0.5f), 1f)); nextTile = NextTile.STAIR_UP; } else { currentJumpType = JumpType.FULL_JUMP; positions.Add(new MovimentPosition(playerGO.transform.localPosition, 0f)); positions.Add(new MovimentPosition(__targetPos + Vector3.up, 1f)); positions.Add(new MovimentPosition(__targetPos + p_playerDir, 1f)); } Debug.Log(currentJumpType); }
public void CheckNextTile(Vector3 p_playerDir) { currentTile = nextTile; Vector3 playerTargetPosition = playerGO.transform.localPosition + p_playerDir; Collider[] __collisions; //Check For Ladders if (currentTile == NextTile.GROUND) { __collisions = Physics.OverlapSphere(playerGO.transform.localPosition + (p_playerDir * 0.5f) - (Vector3.up * 0.5f), 0.1f); if (HasColliderWithTag(__collisions, "Ladder")) { nextTile = NextTile.LADDER_DOWN; return; } } //Going Down on Stairs if (currentTile == NextTile.STAIR_DOWN) { __collisions = Physics.OverlapSphere(playerTargetPosition - (Vector3.up * 1.5f), 0.2f); if (HasColliderWithTag(__collisions, "Stair")) { nextTile = NextTile.STAIR_DOWN; return; } if (HasStairInPosition(playerTargetPosition + Vector3.up, 0.1f)) { currentTile = NextTile.STAIR_UP; nextTile = NextTile.STAIR_UP; return; } if (!HasWallInPosition(playerTargetPosition, 0.05f)) { Debug.Log("down"); currentTile = NextTile.STAIR_DOWN; nextTile = NextTile.GROUND; return; } else { Debug.Log("up"); currentTile = NextTile.STAIR_UP; nextTile = NextTile.GROUND; return; } } //Going Up on Stairs if (currentTile == NextTile.STAIR_UP) { __collisions = Physics.OverlapSphere(playerTargetPosition + (Vector3.up * 1.5f), 0.2f); if (HasColliderWithTag(__collisions, "Stair")) { Debug.Log("Here"); nextTile = NextTile.STAIR_UP; return; } __collisions = Physics.OverlapSphere(playerTargetPosition + (Vector3.up * -1.0f), 0.2f); if (HasColliderWithTag(__collisions, "Stair")) { Debug.Log("Here2"); nextTile = NextTile.STAIR_DOWN; return; } if (HasWallInPosition(playerTargetPosition + (Vector3.up * -1.0f), 0.2f) && !HasWallInPosition(playerTargetPosition, 0.2f)) { Debug.Log("Here3"); currentTile = NextTile.STAIR_DOWN; nextTile = NextTile.GROUND; return; } } //Try to find something in front of him, Checks for Walls, goes if finds Enemy or an Item __collisions = Physics.OverlapSphere(playerTargetPosition + (Vector3.up * 0.5f), 0.2f); if (__collisions.Length > 0) { if (HasColliderWithTag(__collisions, "Stair")) { nextTile = NextTile.STAIR_UP; return; } //Continue if Finds Item or Enemy else if (__collisions [0].name.StartsWith("Enemy") || HasColliderWithTag(__collisions, "Sword") || HasColliderWithTag(__collisions, "Dynamite")) { nextTile = NextTile.GROUND; return; } else if (HasWallInPosition(playerTargetPosition + (Vector3.up * 0.5f), 0.2f)) { if (currentTile == NextTile.STAIR_UP) { nextTile = NextTile.GROUND; } else { nextTile = NextTile.WALL; } return; } else { Debug.Log("Should Not Be Here"); nextTile = NextTile.WALL; return; } } //Try to find Ground on the next tile __collisions = Physics.OverlapSphere(playerTargetPosition - (Vector3.up * 0.5f), 0.2f); if (__collisions.Length > 0) { if (HasColliderWithNameStart(__collisions, "P_Small") || HasColliderWithNameStart(__collisions, "P_Big") || HasColliderWithTag(__collisions, "Elevator")) { nextTile = NextTile.GROUND; return; } else if (HasColliderWithTag(__collisions, "Stair")) { nextTile = NextTile.STAIR_DOWN; return; } else { nextTile = NextTile.WALL; return; } } __collisions = Physics.OverlapSphere(playerGO.transform.localPosition + (p_playerDir * 0.5f) - (Vector3.up * 0.5f), 0.1f); if (HasColliderWithTag(__collisions, "Ladder")) { nextTile = NextTile.LADDER_DOWN; return; } else { nextTile = NextTile.NOTHING; } }
public void CalcPlayerPath(Vector3 p_playerDir) { Vector3 playerTargetPosition = playerGO.transform.localPosition + p_playerDir; CheckNextTile(p_playerDir); positions = new List <MovimentPosition> (); if (nextTile == NextTile.LADDER_DOWN) { CalcPlayerClimbDownLadder(p_playerDir); nextTile = NextTile.GROUND; } else if (nextTile != NextTile.NOTHING) { positions.Add(new MovimentPosition(playerGO.transform.localPosition, 0f)); if (nextTile == NextTile.GROUND) { if (currentTile == NextTile.GROUND) { positions.Add(new MovimentPosition(playerTargetPosition, 1f)); } else if (currentTile == NextTile.STAIR_UP) { positions.Add(new MovimentPosition(playerGO.transform.localPosition + Vector3.up * 0.5f + p_playerDir * 0.5f, 0.5f)); positions.Add(new MovimentPosition(playerTargetPosition + Vector3.up * 0.5f, 1f)); } else if (currentTile == NextTile.STAIR_DOWN) { positions.Add(new MovimentPosition(playerGO.transform.localPosition + Vector3.down * 0.5f + p_playerDir * 0.5f, 0.5f)); positions.Add(new MovimentPosition(playerTargetPosition + Vector3.down * 0.5f, 1f)); } } else if (nextTile == NextTile.STAIR_DOWN) { if (currentTile == NextTile.GROUND || currentTile == NextTile.WALL) { positions.Add(new MovimentPosition(playerGO.transform.localPosition + p_playerDir * 0.5f, 0.5f)); positions.Add(new MovimentPosition(playerTargetPosition + Vector3.down * 0.5f, 1f)); } else if (currentTile == NextTile.STAIR_DOWN) { positions.Add(new MovimentPosition(playerTargetPosition + Vector3.down, 1f)); } } else if (nextTile == NextTile.STAIR_UP) { if (currentTile == NextTile.GROUND) { positions.Add(new MovimentPosition(playerGO.transform.localPosition + p_playerDir * 0.5f, 0.5f)); positions.Add(new MovimentPosition(playerTargetPosition + Vector3.up * 0.5f, 1f)); } else if (currentTile == NextTile.STAIR_UP) { positions.Add(new MovimentPosition(playerTargetPosition + Vector3.up, 1f)); } } } else if (nextTile == NextTile.NOTHING) { positions.Add(new MovimentPosition(playerGO.transform.localPosition, 0f)); positions.Add(new MovimentPosition(playerTargetPosition, 1f)); } }
private void UpdateMovement(float deltaTime) { if (CurrentTile != DestinationTile) { if (NextTile == null) { // get next step if (_path != null && _path.Count > 0) { NextTile = _path.Pop(); UpdateDirection(); } else { _path = null; } } } else { // Arrived DestinationTile = null; UpdateDirection(); } // Enter behaviour if (NextTile?.IsEnterable() == Enterable.Never) { // cannot enter DestinationTile = NextTile = null; } else if (NextTile?.IsEnterable() == Enterable.Soon) { // wait until we can enter the tile return; } if (NextTile != null) { float totalDistance = Position.Distance(CurrentTile.Position, NextTile.Position); float incrementDistance = Speed * deltaTime; float percentage = incrementDistance / totalDistance; _movementCompletePercentage += percentage; if (_movementCompletePercentage >= 1) { _movementCompletePercentage = 0; CurrentTile = NextTile; NextTile = null; } else { new CharacterUpdatedEvent { Character = this, Direction = _characterDirection }.Publish(); } } }
public void Update(float deltaTime) { // TODO: pathfinding // If we have a next tile, move to it if (jobReached == false && DestTile != CurrTile) { // We have some place to be // Do we have pathfinding already? if (pathfinding == null) { // If not we should find new pathfinding. pathfinding = new AStar(DestTile.world.Graph, CurrTile, DestTile); } if (CurrTile == NextTile) // We moved another step in the right direction // If this is the first step on our journey it's fine because we just generated a path. // We might not just be moving inside of a room, but on the world scale { NextTile = pathfinding.DequeueNextTile(); if (NextTile == null) // The pathfinding does not know where to go. Delete the current job, since only a job can make a character move { Debug.Log("Deleting job"); CurrentJob.DeleteJob(); return; } } // MOVEMENT // We have a place to be, update our movement, lets assume that next tile is always 1 distance unit away // Can we move to the next tile? if (NextTile.IsEnterable()) { ProgressToNextTile += deltaTime * Speed; if (ProgressToNextTile >= 1) { // We have reached the next tile! CurrTile = NextTile; ProgressToNextTile = 0; } } OnCharacterPositionChanged(this); } else { // We have reached our destination :) // do work on the current job if (CurrentJob != null) { // This is also where we gain experience for the work done Skills jobSkill = CurrentJob.GetJobType(); float skillLvl = stats[jobSkill]; CurrentJob.DoWork(this, deltaTime); // Xp gained is based on time spent working, so more work (due to higher level) doesn't equal more xp float xpAmount = deltaTime / (skillLvl * 60); stats[jobSkill] += xpAmount; } // If we don't have a job look for one if (CurrentJob == null) { // Request a job from the world // TODO: subclass this so different characters can request different jobs // TODO: this gets spammed if there are no jobs, perhaps make the character idle for a couple seconds? Could also be a job! // TODO: move this request into the job queue, should probably pass your own preferences of jobs in that function too Job j = world.Jobs.RequestJob(jobPriorities); OverrideJob(j); if (CurrentJob == null) { // No job available for the moment, so lets just return and do nothing // TODO: make the character do something? return; } } } }