Last active
August 10, 2018 10:58
-
-
Save Wampa842/9a7448c8f1af072789ccae6e64001b24 to your computer and use it in GitHub Desktop.
My Summer Car buyable mod item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
When the mod is loaded, instantiate your GameObject, then set its position to somewhere in Teimo's shop. | |
The GameObject must have the usual interactivity components - a convex MeshCollider and a Rigidbody. | |
It's best to set Rigidbody.isKinematic to true and GameObject.tag to "Untagged" before adding the behaviour component. | |
*/ | |
/*MIT License | |
Copyright(c) 2018 Wampa842 | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE.*/ | |
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using MSCLoader; | |
using HutongGames.PlayMaker; | |
namespace BuyableItem | |
{ | |
// Required class - GameHook by zamp | |
public class GameHook | |
{ | |
/*MIT License | |
Copyright(c) 2017 zamp | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE.*/ | |
private class FsmHookAction : FsmStateAction | |
{ | |
public Action hook; | |
public override void OnEnter() | |
{ | |
hook.Invoke(); | |
Finish(); | |
} | |
} | |
public static void InjectStateHook(GameObject gameObject, string stateName, Action hook) | |
{ | |
var state = GetStateFromGameObject(gameObject, stateName); | |
if (state != null) | |
{ | |
// inject our hook action to the state machine | |
var actions = new List<FsmStateAction>(state.Actions); | |
var hookAction = new FsmHookAction(); | |
hookAction.hook = hook; | |
actions.Insert(0, hookAction); | |
state.Actions = actions.ToArray(); | |
} | |
} | |
private static FsmState GetStateFromGameObject(GameObject obj, string stateName) | |
{ | |
var comps = obj.GetComponents<PlayMakerFSM>(); | |
foreach (var playMakerFsm in comps) | |
{ | |
var state = playMakerFsm.FsmStates.FirstOrDefault(x => x.Name == stateName); | |
if (state != null) | |
return state; | |
} | |
return null; | |
} | |
} | |
public class BuyableBehaviour : MonoBehaviour | |
{ | |
public static float Price => 300.0f; | |
public static Vector3 CounterPos => new Vector3(-1551.03f, 4.8f, 1182.74f); | |
public static string ItemName => "item"; | |
// While in the shop, the items should be maintained on a List so they can be properly disposed of when the shop is restocked | |
// public List<GameObject> ShopList; | |
private GameObject _register; | |
private bool _bought = false; | |
private bool _buying = false; | |
private FsmBool _guiBuy; | |
private FsmString _guiText; | |
// Make interactive | |
public void Activate() | |
{ | |
transform.parent = null; | |
gameObject.name = ItemName + "(Clone)"; | |
gameObject.layer = LayerMask.NameToLayer("Parts"); | |
gameObject.tag = "PART"; | |
gameObject.GetComponent<Rigidbody>().isKinematic = false; | |
} | |
// Public member that sets the "bought" status (e.g. for loading a save) | |
public void SetBought() | |
{ | |
_bought = true; | |
} | |
// Put the item in the basket | |
private void _buy() | |
{ | |
PlayMakerFSM registerFsm = _register.GetComponent<PlayMakerFSM>(); | |
registerFsm.FsmVariables.GetFsmFloat("PriceTotal").Value += Price; | |
registerFsm.SendEvent("PURCHASE"); | |
_buying = true; | |
gameObject.SetActive(false); | |
} | |
// Pay for the items in the basket | |
private void _pay() | |
{ | |
if (_buying) | |
{ | |
// Move to counter | |
gameObject.SetActive(true); | |
transform.position = CounterPos; | |
// Make interactive and dynamic | |
Activate(); | |
_buying = false; | |
_bought = true; | |
// If the shop content is maintained on a list, now is the time to remove the item | |
// ShopList.Remove(gameObject); | |
} | |
} | |
// Find GUI FSMvars and hook into the store action | |
void Awake() | |
{ | |
_register = GameObject.Find("STORE/StoreCashRegister/Register"); | |
_guiBuy = PlayMakerGlobals.Instance.Variables.FindFsmBool("GUIbuy"); | |
_guiText = PlayMakerGlobals.Instance.Variables.FindFsmString("GUIinteraction"); | |
GameHook.InjectStateHook(_register, "Purchase", () => { _pay(); }); | |
} | |
void Update() | |
{ | |
bool activate = Input.GetKeyDown(KeyCode.Mouse0); | |
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, 1.0f); | |
if (hit.collider.gameObject == this.gameObject) | |
{ | |
if (!_bought) | |
{ | |
_guiBuy.Value = true; | |
_guiText.Value = $"{ItemName} ({Price} mk)"; | |
if (activate) | |
{ | |
_buy(); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment