Skip to content

Instantly share code, notes, and snippets.

@simon56modder
Created January 29, 2021 08:19
Show Gist options
  • Save simon56modder/2ad2299c62426ceea0015e68701c9f8d to your computer and use it in GitHub Desktop.
Save simon56modder/2ad2299c62426ceea0015e68701c9f8d to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICities;
using ColossalFramework;
using UnityEngine;
using ProceduralObjects;
using ProceduralObjects.UI;
using ProceduralObjects.Tools;
namespace SelfRotationModule
{
public class SelfRotationPOMod : LoadingExtensionBase, IUserMod
{
public string Name { get { return "Self-Rotation Module"; } }
public string Description { get { return "Self-rotation module for Procedural Objects"; } }
private POModuleType moduleType;
public override void OnCreated(ILoading loading)
{
base.OnCreated(loading);
if (moduleType == null)
{
moduleType = new POModuleType()
{
Name = Name,
Author = "Simon Royer",
TypeID = "SelfRotationModule",
ModuleType = typeof(SelfRotationModule),
maxModulesOnMap = 1000
};
}
if (!ProceduralObjectsMod.ModuleTypes.Contains(moduleType))
ProceduralObjectsMod.ModuleTypes.Add(moduleType);
}
public override void OnReleased()
{
base.OnReleased();
if (moduleType == null)
return;
if (ProceduralObjectsMod.ModuleTypes.Contains(moduleType))
ProceduralObjectsMod.ModuleTypes.Remove(moduleType);
}
}
public class SelfRotationModule : POModule
{
public float speedRPM = 1;
public byte axis = 1;
public bool reverseDirection = false, worldRotation = false;
public override void OnModuleCreated(ProceduralObjectsLogic logic)
{
base.OnModuleCreated(logic);
window = new Rect(0, 0, 300, 200);
}
public override void UpdateModule(ProceduralObjectsLogic logic, bool simulationPaused, bool layerVisible)
{
if (simulationPaused || !layerVisible)
return;
if (axis == 0)
{
if (worldRotation)
parentObject.m_rotation = Quaternion.Euler(new Vector3(((360f * speedRPM) * Time.deltaTime / 60f) * (reverseDirection ? -1f : 1f), 0, 0)) * parentObject.m_rotation;
else
parentObject.m_rotation = parentObject.m_rotation * Quaternion.Euler(new Vector3(((360f * speedRPM) * Time.deltaTime / 60f) * (reverseDirection ? -1f : 1f), 0, 0));
}
else if (axis == 1)
{
if (worldRotation)
parentObject.m_rotation = Quaternion.Euler(new Vector3(0, ((360f * speedRPM) * Time.deltaTime / 60f) * (reverseDirection ? -1f : 1f), 0)) * parentObject.m_rotation;
else
parentObject.m_rotation = parentObject.m_rotation * Quaternion.Euler(new Vector3(0, ((360f * speedRPM) * Time.deltaTime / 60f) * (reverseDirection ? -1f : 1f), 0));
}
else
{
if (worldRotation)
parentObject.m_rotation = Quaternion.Euler(new Vector3(0, 0, ((360f * speedRPM) * Time.deltaTime / 60f) * (reverseDirection ? -1f : 1f))) * parentObject.m_rotation;
else
parentObject.m_rotation = parentObject.m_rotation * Quaternion.Euler(new Vector3(0, 0, ((360f * speedRPM) * Time.deltaTime / 60f) * (reverseDirection ? -1f : 1f)));
}
}
public override void DrawCustomizationWindow(int id)
{
base.DrawCustomizationWindow(id);
GUI.Label(new Rect(5, 52, 110, 20), "Speed (RPM) :");
var newSpeed = GUI.TextField(new Rect(115, 50, 180, 25), speedRPM.ToString());
if (newSpeed != speedRPM.ToString())
{
float speed = 1f;
if (float.TryParse(newSpeed, out speed))
speedRPM = speed;
}
reverseDirection = GUI.Toggle(new Rect(5, 77, 250, 20), reverseDirection, "Reverse Direction");
worldRotation = GUI.Toggle(new Rect(5, 92, 250, 20), worldRotation, "Use World Rotation instead of local");
GUI.Label(new Rect(5, 116, 110, 20), "Rotate around");
axis = (byte)GUI.Toolbar(new Rect(115, 115, 180, 25), axis, new string[] { "X", "Y", "Z" });
}
public override void GetData(Dictionary<string, string> data, bool forSaveGame)
{
data.Add("speedRPM", speedRPM.ToString());
data.Add("axis", axis.ToString());
data.Add("reverseDirection", reverseDirection.ToString());
data.Add("worldRotation", worldRotation.ToString());
}
public override void LoadData(Dictionary<string, string> data, bool fromSaveGame)
{
if (data.ContainsKey("speedRPM"))
speedRPM = float.Parse(data["speedRPM"]);
if (data.ContainsKey("axis"))
axis = byte.Parse(data["axis"]);
if (data.ContainsKey("reverseDirection"))
reverseDirection = bool.Parse(data["reverseDirection"]);
if (data.ContainsKey("worldRotation"))
worldRotation = bool.Parse(data["worldRotation"]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment