Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "DrivableCarScript.h"
- #include <Spore\GameModes.h>
- #include <Spore\Simulator.h>
- #include <Spore\App\ScenarioMode.h>
- DrivableCarScript::DrivableCarScript()
- : DefaultObject()
- , mVehicle(nullptr)
- , mIsDriving(false)
- , mTask(nullptr)
- {
- }
- DrivableCarScript::~DrivableCarScript()
- {
- }
- int DrivableCarScript::AddRef()
- {
- return DefaultObject::AddRef();
- }
- int DrivableCarScript::Release()
- {
- return DefaultObject::Release();
- }
- // You can extend this function to return any other types your class implements.
- void* DrivableCarScript::Cast(uint32_t type) const
- {
- if (type == TYPE) return (DrivableCarScript*)this;
- else if (type == IWinProc::TYPE) return (IWinProc*)this;
- else return DefaultObject::Cast(type);
- }
- int DrivableCarScript::GetEventFlags() const {
- return UTFWin::kEventFlagBasicInput;
- }
- bool DrivableCarScript::IsPlayingAdventure() {
- return IsScenarioMode() && ScenarioMode::Get()->GetMode() == ScenarioMode::Mode::PlayMode;
- }
- void DrivableCarScript::ToggleDriving() {
- mIsDriving = !mIsDriving;
- mTask = nullptr;
- if (!mIsDriving && mVehicle) mVehicle->StopMovement();
- }
- cVehicle* DrivableCarScript::FindVehicle() const
- {
- auto playerPos = GameNounManager()->GetAvatar()->GetPosition();
- auto vehicles = GetData<cVehicle>();
- float closestDistance = 100000.0f; // just start with something big
- cVehicle* closestVehicle = nullptr;
- for (auto v : vehicles) {
- float distance = abs((v->GetPosition() - playerPos).length());
- if (distance < closestDistance) {
- closestDistance = distance;
- // v is an intruisve_ptr, you can get the pointer itself with .get()
- closestVehicle = v.get();
- }
- }
- // Only accept if player is 5 meters or less from the car
- // A better design would consider the boundaries of the vehicle for this
- if (closestDistance <= 5.0f) return closestVehicle;
- return nullptr;
- }
- void DrivableCarScript::OnKeyPress() {
- if (mIsDriving) {
- // Since he was driving and pressed Z, this task will stop the vehicle
- mTask = Simulator::ScheduleTask(this, &DrivableCarScript::ToggleDriving, 1.0f);
- }
- else {
- mVehicle = FindVehicle();
- // Only do the task if there is an available vehicle
- if (mVehicle) {
- mTask = Simulator::ScheduleTask(this, &DrivableCarScript::ToggleDriving, 1.0f);
- }
- }
- }
- void DrivableCarScript::OnKeyRelease() {
- // The user released the key, so cancel the task if it was too soon
- if (mTask && !mTask->HasExecuted()) {
- Simulator::RemoveScheduledTask(mTask);
- mTask = nullptr;
- mVehicle = nullptr;
- mIsDriving = false;
- }
- }
- bool DrivableCarScript::HandleUIMessage(IWindow* pWindow, const Message& message) {
- if (IsPlayingAdventure()) {
- // Check for mIsPressing to ensure this is called only once during the key press
- if (!mIsPressing && message.IsType(kMsgKeyDown) && message.Key.vkey == 'Z')
- {
- OnKeyPress();
- mIsPressing = true;
- }
- else if (message.IsType(kMsgKeyUp) && message.Key.vkey == 'Z')
- {
- OnKeyRelease();
- mIsPressing = false;
- }
- }
- // If we arrive here, the message wasn't for us
- return false;
- }
- void DrivableCarScript::Update() {
- if (IsPlayingAdventure()) {
- if (mIsDriving) {
- mVehicle->MoveTo(GameViewManager()->GetWorldMousePosition());
- }
- }
- // If we have exited play mode, stop driving
- else {
- mVehicle = nullptr;
- mIsDriving = false;
- mTask = nullptr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement