/// <summary> /// Logistics and Construction resemble each other very much, so this is the method to handle both. /// </summary> private void HandleTransport(GameTime time) { if (!mIsMoving && mDone) { mDone = false; mTask = mDirector.GetDistributionDirector.GetManager(Graphid).RequestNewTask(unit: this, job: Job, assignedAction: Optional <IPlatformAction> .Of(null)); //First go to the location where you want to get your Resource from //Check if the given destination is null (it shouldnt). if (mTask.Begin.IsPresent()) { mDestination = Optional <INode> .Of(mTask.Begin.Get()); TargetGraphid = mTask.Begin.Get().GetGraphIndex(); } else { //In this case the DistributionManager has given you no valid task. That means there is no work in this job to be done. Ask in the next cycle. mDone = true; } } //This means we arrived at the point we want to pick up a Resource if (mTask.Begin.IsPresent() && CurrentNode.Equals(mTask.Begin.Get()) && ReachedTarget(mTask.Begin.Get().Center)) { if (!Carrying.IsPresent()) { if (mTask.GetResource != null) { PickUp((EResourceType)mTask.GetResource); } //Failed to get resource, because no Resources were present. Tell the DistributionManager and consider your work done. if (!Carrying.IsPresent()) { if (mTask.Job == JobType.Logistics) { if (mTask.Action.IsPresent()) { if (mTask.GetResource != null) { mDirector.GetDistributionDirector.GetManager(Graphid).RequestResource(mTask.End.Get(), (EResourceType)mTask.GetResource, mTask.Action.Get()); } } else { if (mTask.GetResource != null) { mDirector.GetDistributionDirector.GetManager(Graphid).RequestResource(mTask.End.Get(), (EResourceType)mTask.GetResource, null); } } } if (mTask.Job == JobType.Construction) { if (mTask.Action.IsPresent()) { if (mTask.GetResource != null) { mDirector.GetDistributionDirector.GetManager(Graphid).RequestResource(mTask.End.Get(), (EResourceType)mTask.GetResource, mTask.Action.Get(), true); } } else { if (mTask.GetResource != null) { mDirector.GetDistributionDirector.GetManager(Graphid).RequestResource(mTask.End.Get(), (EResourceType)mTask.GetResource, null, true); } } } mDone = true; } } //Everything went fine with picking up, so now move on to your final destination if (mTask.End.IsPresent() && !mDone) { mDestination = Optional <INode> .Of(mTask.End.Get()); TargetGraphid = mTask.End.Get().GetGraphIndex(); } } //This means we arrived at the point we want to leave the Resource and consider our work done if (mTask.End.IsPresent() && CurrentNode.Equals(mTask.End.Get()) && ReachedTarget(mTask.End.Get().Center) && Carrying.IsPresent()) { var res = Carrying.Get(); res.UnFollow(); ((PlatformBlank)CurrentNode).StoreResource(res); Carrying = Optional <Resource> .Of(null); mDone = true; } }
/// <summary> /// In the Idle case the unit will just get a Target to move to and do so. /// In the Production case the unit will go to the producing Platform and call its produce method. /// </summary> /// <param name="gametime"></param> public void Update(GameTime gametime) { if (!mInitialized) { return; } //If true, this unit has still a task to finish and shall not act like the job it has now until the task is finished. //This should only occur when Logistic or Constructing units have only just picked up a Resource and their job changed after that. if (mFinishTask) { RegulateMovement(); if (Carrying.IsPresent()) { Carrying.Get().Follow(this); } //This means we arrived at the point we want to leave the Resource and consider our work done if (!mTask.End.IsPresent() || !CurrentNode.Equals(mTask.End.Get()) || !ReachedTarget(mTask.End.Get().Center)) { return; } if (Carrying.IsPresent()) { var res = Carrying.Get(); res.UnFollow(); ((PlatformBlank)CurrentNode).StoreResource(res); Carrying = Optional <Resource> .Of(null); } mDone = true; //We can now do the job we were assigned to. mFinishTask = false; } else { switch (Job) { case JobType.Idle: if (!mIsMoving && mDone) { mDone = false; mTask = mDirector.GetDistributionDirector.GetManager(Graphid) .RequestNewTask(unit: this, job: Job, assignedAction: Optional <IPlatformAction> .Of(null)); //Check if the given destination is null (it shouldnt) if (mTask.End.IsPresent()) { mDestination = Optional <INode> .Of(mTask.End.Get()); TargetGraphid = mTask.End.Get().GetGraphIndex(); } } RegulateMovement(); //We arrived at the target, so its now time to get another job if (mNodeQueue.Count == 0 && Job == JobType.Idle) { mDone = true; } break; case JobType.Production: //You arrived at your destination and you now want to work. if (!mIsMoving && !mDone && CurrentNode.Equals(mTask.End.Get()) && !mAssigned) { mTask.End.Get().ShowedUp(this, Job); mAssigned = true; } if (mAssigned) { mTask.End.Get().Produce(); } RegulateMovement(); break; case JobType.Defense: //You arrived at your destination and you now want to work. if (!mIsMoving && !mDone && CurrentNode.Equals(mTask.End.Get())) { if (!mAssigned) { mTask.End.Get().ShowedUp(this, Job); mAssigned = true; } } RegulateMovement(); break; case JobType.Logistics: HandleTransport(gametime); RegulateMovement(); if (Carrying.IsPresent()) { Carrying.Get().Follow(this); } break; case JobType.Construction: HandleTransport(gametime); RegulateMovement(); if (Carrying.IsPresent()) { Carrying.Get().Follow(this); } break; } } }