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; } } } }