Skip to content

Commit

Permalink
First public release
Browse files Browse the repository at this point in the history
  • Loading branch information
mokojm committed Mar 6, 2022
1 parent 8a8add7 commit 4a87f46
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 12 deletions.
114 changes: 105 additions & 9 deletions BuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,32 @@ public class BuildHelperMain : MelonMod
//Keyboard
public static KeyCode AddVoxelsKey = KeyCode.F;
public static KeyCode RemoveVoxelsKey = KeyCode.G;
public static KeyCode AddVoxelHeightKey = KeyCode.H;
public static KeyCode RemoveVoxelsRayKey = KeyCode.J;
public static KeyCode PaintVoxelsKey = KeyCode.K;
public static KeyCode AddVoxelHeightKey = KeyCode.Space;
public static KeyCode RemoveVoxelsRayKey = KeyCode.H;
public static KeyCode PaintVoxelsKey = KeyCode.J;

public static bool AddVoxelsKeyB = false;
public static bool RemoveVoxelsKeyB = false;
public static bool AddVoxelHeightKeyB = false;
public static bool RemoveVoxelsRayKeyB = false;
public static bool PaintVoxelsKeyB = false;

//PaintVoxel check to make sure one voxel is painted at a time
public static bool painterLocked = false;

//Fixed height
public static GameObject sphere;

//Time management for AddVoxelHeight
public static float thisTime;
public static bool addVoxHPressed = false;

//Time management for Sphere
public static float sphereTime;

//MainCamera check for identifying whether FPS mod is "ON" or not
public static Camera mainCam;


//Methods

Expand All @@ -52,10 +68,23 @@ public static void Initialize(HoverData thisHover)
clickEffect = hover.master.clickEffect;
voxelColor = (VoxelType)hover.master.uiMaster.paletteMenu.selectedPickerIndex;

//Initialize sphere
sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(0, 0, 0);
sphere.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
sphere.GetComponent<MeshRenderer>().material = GameObject.Find("HoverHightlight").GetComponent<MeshRenderer>().material;
sphere.GetComponent<MeshRenderer>().material.color = new Color(1, 1, 1, 0.5f);
sphere.SetActive(fixedHeight);

//Initialize mainCamera
mainCam = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();

isInitialized = true;
}
}



public static Voxel SearchVoxel(int2 hexPos, int dstHeight)
{
if (graph.cornerDict.ContainsKey(hexPos))
Expand Down Expand Up @@ -106,7 +135,7 @@ public static void RemoveVoxelsRay()
//Build at a specific height
public static void AddVoxelHeight()
{
if (fixedHeight == false)
if (fixedHeight == false || !mainCam.isActiveAndEnabled)
{
return;
}
Expand Down Expand Up @@ -188,7 +217,7 @@ public static void StartAddVoxels()
//Remove several voxels
public static void RemoveVoxels()
{
int dstHeight = fixedHeight ? height - 1 : hover.dstHeight - 1;
int dstHeight = hover.srcHeight;
dstHeight = dstHeight < 0 ? 0 : dstHeight;
//int dstHeight = height == 999 ? hover.dstHeight - 1 : height - 1;

Expand Down Expand Up @@ -237,12 +266,14 @@ public static IEnumerator PaintVoxels()
maker.AddAction(hexPos, (byte)dstHeight, srcVoxelColor, VoxelType.Empty);
graph.RemoveVoxel(voxel);
maker.EndAction();
painterLocked = true;
yield return new WaitForSeconds(.1f);

maker.BeginNewAction();
graph.AddVoxel(hexPos, (byte)dstHeight, voxelColor, true);
maker.AddAction(hexPos, (byte)dstHeight, VoxelType.Empty, voxelColor);
maker.EndAction();
painterLocked = false;

clickEffect.Click(hover, true, voxel);
}
Expand All @@ -251,7 +282,10 @@ public static IEnumerator PaintVoxels()

public static void StartPaintVoxels()
{
MelonCoroutines.Start(PaintVoxels());
if (painterLocked == false)
{
MelonCoroutines.Start(PaintVoxels());
}
}


Expand All @@ -268,11 +302,37 @@ public override void OnSceneWasLoaded(int buildIndex, string sceneName)
}
}

public static void ResetSphere()
{
sphere.transform.position = hover.pointerHitPos;
}

public static void Initialize()
public static void UpdateSphere(float upDown = 0)
{
/*float x = hover.srcVert.hexPos.x;
float z = hover.srcVert.hexPos.y;
float y = sphere.transform.position.y;*/

}
float x = hover.pointerHitPos.x;
float z = hover.pointerHitPos.z;
float y = sphere.transform.position.y;

if (upDown == 0)
{
sphere.transform.position = new Vector3(x, y, z);
//sphere.transform.position = new Vector3(hover.pointerHitPos.x, sphere.transform.position.y, hover.pointerHitPos.z);
}
else if (upDown == 1)
{
sphere.transform.position = new Vector3(x, y+1, z);
//sphere.transform.position = new Vector3(hover.pointerHitPos.x, sphere.transform.position.y + 1, hover.pointerHitPos.z);
}
else if (sphere.transform.position.y > 1)
{
sphere.transform.position = new Vector3(x, y-1, z);
//sphere.transform.position = new Vector3(hover.pointerHitPos.x, sphere.transform.position.y - 1, hover.pointerHitPos.z);
}
}

public static void Reset()
{
Expand Down Expand Up @@ -307,15 +367,27 @@ public override void OnUpdate()
RemoveVoxelsKeyB = false;
}

if (Input.GetKey(AddVoxelHeightKey))
//Add voxel height input management
if (Input.GetKeyDown(AddVoxelHeightKey))
{
thisTime = Time.time;
AddVoxelHeightKeyB = true;
addVoxHPressed = true;
sphere.SetActive(false);
}
else if (Input.GetKeyUp(AddVoxelHeightKey))
{
AddVoxelHeightKeyB = false;
addVoxHPressed = false;
}

if (addVoxHPressed && Time.time - thisTime > 1)
{
AddVoxelHeightKeyB = true;
}


//Others
if (Input.GetKeyDown(RemoveVoxelsRayKey))
{
RemoveVoxelsRayKeyB = true;
Expand All @@ -333,6 +405,30 @@ public override void OnUpdate()
{
PaintVoxelsKeyB = false;
}

//Sphere update
if (Input.GetKeyDown(KeyCode.LeftAlt))
{
UpdateSphere(1);
height = height < 255 ? height + 1 : height;
sphere.SetActive(fixedHeight);
sphereTime = Time.time;
}
else if (Input.GetKeyDown(KeyCode.LeftControl))
{
UpdateSphere(-1);
height = height > 0 ? height - 1 : height;
sphere.SetActive(fixedHeight);
sphereTime = Time.time;
}
else if (Time.time - sphereTime > 10)
{
sphere.SetActive(false);
}
else
{
UpdateSphere();
}
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions BuildHelperUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public static void Initialize(MelonMod thisMod)

myModSettings = UIManager.Register(thisMod, new Color32(243, 227, 182, 255));

myModSettings.AddToggle("Fixed Height", "General", new Color32(243, 227, 182, 255), BuildHelperMain.fixedHeight, new Action<bool>(delegate (bool value) { BuildHelperMain.fixedHeight = value; }));
myModSettings.AddInputField("Height", "General", new Color32(255, 179, 174, 255), TMP_InputField.ContentType.Alphanumeric, "0", new Action<string>(delegate (string value) { FixedHeightInput(value); }));
refInputField = myModSettings.controlInputFields["Height"].GetComponent<UnityEngine.UI.InputField>();
myModSettings.AddToggle("Fixed Height", "General", new Color32(243, 227, 182, 255), BuildHelperMain.fixedHeight, new Action<bool>(delegate (bool value) { FixedHeightToggle(value); }));
/*myModSettings.AddInputField("Height", "General", new Color32(255, 179, 174, 255), TMP_InputField.ContentType.Alphanumeric, "0", new Action<string>(delegate (string value) { FixedHeightInput(value); }));
refInputField = myModSettings.controlInputFields["Height"].GetComponent<UnityEngine.UI.InputField>();*/

// Mod Setting management for keyboard shortcuts
Apply();

//Keyboard shortcuts button creation
myModSettings.AddKeybind("Add", "Input", BuildHelperMain.AddVoxelHeightKey, new Color32(10, 190, 124, 255));
myModSettings.AddKeybind("Add blocks", "Input", BuildHelperMain.AddVoxelsKey, new Color32(10, 190, 124, 255));
myModSettings.AddKeybind("Remove blocks", "Input", BuildHelperMain.RemoveVoxelsKey, new Color32(10, 190, 124, 255));
Expand All @@ -36,6 +40,12 @@ public static void Initialize(MelonMod thisMod)
isInitialized = true;
}

public static void FixedHeightToggle(bool value)
{
BuildHelperMain.fixedHeight = value;
BuildHelperMain.height = 0;
BuildHelperMain.ResetSphere();
}
public static void FixedHeightInput(string value)
{
if (int.TryParse(value, out int validHeight) && validHeight >= 0 && validHeight < 256)
Expand All @@ -47,6 +57,7 @@ public static void FixedHeightInput(string value)
refInputField.text = BuildHelperMain.height.ToString();
}
}

public static void Apply()
{
myModSettings.GetValueKeyCode("Add", "Input", out BuildHelperMain.AddVoxelHeightKey);
Expand Down
1 change: 1 addition & 0 deletions Harmony.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class BuildHelper

public static void Postfix(ref HoverData __instance)
{

if (BuildHelperMain.isInitialized == false)
{
BuildHelperMain.Initialize(__instance);
Expand Down
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,74 @@
# Townscaper-BuildHelper
A mod to build faster and easier in Townscaper

# Features

- Painter : keep pressing "J" to paint the blocks with your chosen color.
<p align="center">
<a href="./paint_help.gif">
<img src="./paint_help.gif" alt="BuildHelper">
</a>
</p>


- Add ("F") and Remove ("G") a group of blocks
<p align="center">
<a href="./add_remove.gif">
<img src="./add_remove.gif" alt="BuildHelper">
</a>
</p>


- Remove a line of blocks like an eraser beam, press "H"
<p align="center">
<a href="./line_remove.gif">
<img src="./line_remove.gif" alt="BuildHelper">
</a>
</p>


- Add blocks ("Space") or a group of blocks ("F") at the specified height using "LeftCtrl" and "LeftAlt" to manage the height.
<p align="center">
<a href="./fixed_height.gif">
<img src="./fixed_height.gif" alt="BuildHelper">
</a>
</p>


# Installation

1. Download "Melon Loader" by LavaGang:
https://github.com/LavaGang/MelonLoader/releases/latest/

2. Start the MelonLoader.Installer.exe

2.1. Click "Select" and navigate to your Townscaper folder and select the Townscaper.exe (usually: C:\Program Files(x86)\Steam\steamapps\common\Townscaper\Townscaper.exe)

2.2. Untick the "Latest" checkbox and choose version 0.4.3

2.3. Click install

2.4. During the installation a "Mods" folder gets created it your game folder. MelonLoader does not(!) change any game files.
You can uninstall anytime through the installer or by deleting the "version.dll" file.

3. Download the mod (latest release) by clicking [here]()

4. Extract all the files from "BuildHelper.zip" into your games Mods folder.

5. Download the utility mod "ModUI" (latest release) from: https://github.com/DigitalzombieTLD/TownscaperModUI/releases/latest/

6. Extract the all files from the ModUI download into your games Mods folder

7. Start the game !

8. The menu can be found here :
<p align="center">
<a href="./Mod_use.gif">
<img src="./Mod_use.gif" alt="PlantColorUse">
</a>
</p>


# Advanced feature

You can modify keyboard shortcuts by clicking them and pressing the wanted key. Then press "Apply Settings".
Binary file added add_remove.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixed_height.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added line_remove.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added paint_help.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4a87f46

Please sign in to comment.