SHARE
TWEET

ModAPI Tutorial: DrivableCarScript.cpp

a guest Feb 27th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include "DrivableCarScript.h"
  3. #include <Spore\GameModes.h>
  4. #include <Spore\Simulator.h>
  5. #include <Spore\App\ScenarioMode.h>
  6.  
  7. DrivableCarScript::DrivableCarScript()
  8.     : DefaultObject()
  9.     , mVehicle(nullptr)
  10.     , mIsDriving(false)
  11.     , mTask(nullptr)
  12. {
  13. }
  14.  
  15.  
  16. DrivableCarScript::~DrivableCarScript()
  17. {
  18. }
  19.  
  20.  
  21. int DrivableCarScript::AddRef()
  22. {
  23.     return DefaultObject::AddRef();
  24. }
  25.  
  26. int DrivableCarScript::Release()
  27. {
  28.     return DefaultObject::Release();
  29. }
  30.  
  31. // You can extend this function to return any other types your class implements.
  32. void* DrivableCarScript::Cast(uint32_t type) const
  33. {
  34.     if (type == TYPE) return (DrivableCarScript*)this;
  35.     else if (type == IWinProc::TYPE) return (IWinProc*)this;
  36.     else return DefaultObject::Cast(type);
  37. }
  38.  
  39.  
  40. int DrivableCarScript::GetEventFlags() const {
  41.     return UTFWin::kEventFlagBasicInput;
  42. }
  43.  
  44. bool DrivableCarScript::IsPlayingAdventure() {
  45.     return IsScenarioMode() && ScenarioMode::Get()->GetMode() == ScenarioMode::Mode::PlayMode;
  46. }
  47.  
  48. void DrivableCarScript::ToggleDriving() {
  49.     mIsDriving = !mIsDriving;
  50.     mTask = nullptr;
  51.  
  52.     if (!mIsDriving && mVehicle) mVehicle->StopMovement();
  53. }
  54.  
  55. cVehicle* DrivableCarScript::FindVehicle() const
  56. {
  57.     auto playerPos = GameNounManager()->GetAvatar()->GetPosition();
  58.     auto vehicles = GetData<cVehicle>();
  59.  
  60.     float closestDistance = 100000.0f;  // just start with something big
  61.     cVehicle* closestVehicle = nullptr;
  62.  
  63.     for (auto v : vehicles) {
  64.         float distance = abs((v->GetPosition() - playerPos).length());
  65.         if (distance < closestDistance) {
  66.             closestDistance = distance;
  67.             // v is an intruisve_ptr, you can get the pointer itself with .get()
  68.             closestVehicle = v.get();
  69.         }
  70.     }
  71.  
  72.     // Only accept if player is 5 meters or less from the car
  73.     // A better design would consider the boundaries of the vehicle for this
  74.     if (closestDistance <= 5.0f) return closestVehicle;
  75.     return nullptr;
  76. }
  77.  
  78. void DrivableCarScript::OnKeyPress() {
  79.     if (mIsDriving) {
  80.         // Since he was driving and pressed Z, this task will stop the vehicle
  81.         mTask = Simulator::ScheduleTask(this, &DrivableCarScript::ToggleDriving, 1.0f);
  82.     }
  83.     else {
  84.         mVehicle = FindVehicle();
  85.         // Only do the task if there is an available vehicle
  86.         if (mVehicle) {
  87.             mTask = Simulator::ScheduleTask(this, &DrivableCarScript::ToggleDriving, 1.0f);
  88.         }
  89.     }
  90. }
  91.  
  92. void DrivableCarScript::OnKeyRelease() {
  93.     // The user released the key, so cancel the task if it was too soon
  94.     if (mTask && !mTask->HasExecuted()) {
  95.         Simulator::RemoveScheduledTask(mTask);
  96.         mTask = nullptr;
  97.         mVehicle = nullptr;
  98.         mIsDriving = false;
  99.     }
  100. }
  101.  
  102. bool DrivableCarScript::HandleUIMessage(IWindow* pWindow, const Message& message) {
  103.     if (IsPlayingAdventure()) {
  104.         // Check for mIsPressing to ensure this is called only once during the key press
  105.         if (!mIsPressing && message.IsType(kMsgKeyDown) && message.Key.vkey == 'Z')
  106.         {
  107.             OnKeyPress();
  108.             mIsPressing = true;
  109.         }
  110.         else if (message.IsType(kMsgKeyUp) && message.Key.vkey == 'Z')
  111.         {
  112.             OnKeyRelease();
  113.             mIsPressing = false;
  114.         }
  115.     }
  116.    
  117.     // If we arrive here, the message wasn't for us    
  118.     return false;
  119. }
  120.  
  121. void DrivableCarScript::Update() {
  122.     if (IsPlayingAdventure()) {
  123.         if (mIsDriving) {
  124.             mVehicle->MoveTo(GameViewManager()->GetWorldMousePosition());
  125.         }
  126.     }
  127.     // If we have exited play mode, stop driving
  128.     else {
  129.         mVehicle = nullptr;
  130.         mIsDriving = false;
  131.         mTask = nullptr;
  132.     }
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top