From a8cca74fc4e2d6457c41693e8c0ae393eaeff899 Mon Sep 17 00:00:00 2001 From: finnchen123 Date: Sun, 16 Nov 2025 20:56:51 +0100 Subject: [PATCH] Added basic jump mechanic for slimes and prevention from going over the border, added tile hiding to increase performance --- Assets/Prefabs/slime.prefab | 9 ++-- Assets/Prefabs/slimeBoss.prefab | 8 ++-- Assets/Prefabs/slimeForest.prefab | 8 ++-- Assets/Prefabs/slimeMage.prefab | 8 ++-- Assets/Prefabs/slimeMetal.prefab | 8 ++-- Assets/Prefabs/slimeWarrior.prefab | 8 ++-- Assets/Scripts/InteractableObjects/Enemy.cs | 48 +++++++++++++++++++ Assets/Scripts/Player/PlayerGameObject.cs | 11 +---- Assets/Scripts/Tile.cs | 10 ++++ Assets/Scripts/WorldGenerator.cs | 51 +++++++++++++++++++++ 10 files changed, 136 insertions(+), 33 deletions(-) diff --git a/Assets/Prefabs/slime.prefab b/Assets/Prefabs/slime.prefab index bcf4f17..6837fb2 100644 --- a/Assets/Prefabs/slime.prefab +++ b/Assets/Prefabs/slime.prefab @@ -116,10 +116,10 @@ Rigidbody: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4392939395195199552} - serializedVersion: 4 + serializedVersion: 5 m_Mass: 1 - m_Drag: 0 - m_AngularDrag: 0.05 + m_LinearDamping: 0 + m_AngularDamping: 0.05 m_CenterOfMass: {x: 0, y: 0, z: 0} m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} @@ -134,7 +134,7 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 122 + m_Constraints: 112 m_CollisionDetection: 0 --- !u!114 &-9044075018784067105 MonoBehaviour: @@ -150,3 +150,4 @@ MonoBehaviour: m_EditorClassIdentifier: clip: {fileID: 0} keepAlive: 0 + slimeType: 0 diff --git a/Assets/Prefabs/slimeBoss.prefab b/Assets/Prefabs/slimeBoss.prefab index 46ba22a..89b65f6 100644 --- a/Assets/Prefabs/slimeBoss.prefab +++ b/Assets/Prefabs/slimeBoss.prefab @@ -116,10 +116,10 @@ Rigidbody: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 7849292920502973565} - serializedVersion: 4 + serializedVersion: 5 m_Mass: 1 - m_Drag: 0 - m_AngularDrag: 0.05 + m_LinearDamping: 0 + m_AngularDamping: 0.05 m_CenterOfMass: {x: 0, y: 0, z: 0} m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} @@ -134,7 +134,7 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 122 + m_Constraints: 112 m_CollisionDetection: 0 --- !u!114 &525370201721428245 MonoBehaviour: diff --git a/Assets/Prefabs/slimeForest.prefab b/Assets/Prefabs/slimeForest.prefab index 2aaa49d..6315244 100644 --- a/Assets/Prefabs/slimeForest.prefab +++ b/Assets/Prefabs/slimeForest.prefab @@ -116,10 +116,10 @@ Rigidbody: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 379562847455873462} - serializedVersion: 4 + serializedVersion: 5 m_Mass: 1 - m_Drag: 0 - m_AngularDrag: 0.05 + m_LinearDamping: 0 + m_AngularDamping: 0.05 m_CenterOfMass: {x: 0, y: 0, z: 0} m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} @@ -134,7 +134,7 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 122 + m_Constraints: 112 m_CollisionDetection: 0 --- !u!114 &4274648213253980113 MonoBehaviour: diff --git a/Assets/Prefabs/slimeMage.prefab b/Assets/Prefabs/slimeMage.prefab index 2fa5ae1..0412813 100644 --- a/Assets/Prefabs/slimeMage.prefab +++ b/Assets/Prefabs/slimeMage.prefab @@ -116,10 +116,10 @@ Rigidbody: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 6968562527232053028} - serializedVersion: 4 + serializedVersion: 5 m_Mass: 1 - m_Drag: 0 - m_AngularDrag: 0.05 + m_LinearDamping: 0 + m_AngularDamping: 0.05 m_CenterOfMass: {x: 0, y: 0, z: 0} m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} @@ -134,7 +134,7 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 122 + m_Constraints: 112 m_CollisionDetection: 0 --- !u!114 &3695996598061783199 MonoBehaviour: diff --git a/Assets/Prefabs/slimeMetal.prefab b/Assets/Prefabs/slimeMetal.prefab index e8254f0..abd37eb 100644 --- a/Assets/Prefabs/slimeMetal.prefab +++ b/Assets/Prefabs/slimeMetal.prefab @@ -116,10 +116,10 @@ Rigidbody: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 710453253311522916} - serializedVersion: 4 + serializedVersion: 5 m_Mass: 1 - m_Drag: 0 - m_AngularDrag: 0.05 + m_LinearDamping: 0 + m_AngularDamping: 0.05 m_CenterOfMass: {x: 0, y: 0, z: 0} m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} @@ -134,7 +134,7 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 122 + m_Constraints: 112 m_CollisionDetection: 0 --- !u!114 &6506905793749053412 MonoBehaviour: diff --git a/Assets/Prefabs/slimeWarrior.prefab b/Assets/Prefabs/slimeWarrior.prefab index b21e0d3..d898902 100644 --- a/Assets/Prefabs/slimeWarrior.prefab +++ b/Assets/Prefabs/slimeWarrior.prefab @@ -116,10 +116,10 @@ Rigidbody: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 7591882314654710750} - serializedVersion: 4 + serializedVersion: 5 m_Mass: 1 - m_Drag: 0 - m_AngularDrag: 0.05 + m_LinearDamping: 0 + m_AngularDamping: 0.05 m_CenterOfMass: {x: 0, y: 0, z: 0} m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} @@ -134,7 +134,7 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 122 + m_Constraints: 112 m_CollisionDetection: 0 --- !u!114 &-1716680456059707178 MonoBehaviour: diff --git a/Assets/Scripts/InteractableObjects/Enemy.cs b/Assets/Scripts/InteractableObjects/Enemy.cs index 09e64ff..ff4de0d 100644 --- a/Assets/Scripts/InteractableObjects/Enemy.cs +++ b/Assets/Scripts/InteractableObjects/Enemy.cs @@ -6,6 +6,7 @@ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; +using System.Diagnostics; namespace Assets.Scripts.InteractableObjects { @@ -15,6 +16,53 @@ namespace Assets.Scripts.InteractableObjects BasicSlime slime; SlimeFactory factory = new SlimeFactory(); public SlimeType slimeType; + bool isJumping; + Stopwatch jumpTimer; + + // Start is called before the first frame update + void Start() + { + isJumping = false; + jumpTimer = new Stopwatch(); + //Nothing + } + + // Update is called once per frame + void Update() + { + if (gameObject.activeSelf) + { + if (!isJumping) + { + jumpTimer.Start(); + isJumping = true; + gameObject.GetComponent().AddForce(new Vector3(rand.Next(-5, 5), 10, rand.Next(-5, 5)), ForceMode.Impulse); + //gameObject.GetComponent().AddForce(new Vector3(5, 10, 5), ForceMode.Impulse); + } + if (jumpTimer.ElapsedMilliseconds >= 5000) + { + jumpTimer.Reset(); + isJumping = false; + } + if (gameObject.transform.position.x > 1000 || gameObject.transform.position.x > 5000 || gameObject.transform.position.x > 5000) + { + UnityEngine.Debug.Log("BUG!"); + } + } + } + + void OnTriggerExit(Collider col) + { + if (col.name.Contains("_") && col.gameObject == gameObject.transform.parent.gameObject) + { + gameObject.GetComponent().AddForce(gameObject.GetComponent().linearVelocity * -2, ForceMode.Impulse); + } + } + + void OnDisable() + { + + } public override void handleInteraction(GameObject player) { diff --git a/Assets/Scripts/Player/PlayerGameObject.cs b/Assets/Scripts/Player/PlayerGameObject.cs index 8ac3007..edc4fb9 100644 --- a/Assets/Scripts/Player/PlayerGameObject.cs +++ b/Assets/Scripts/Player/PlayerGameObject.cs @@ -224,15 +224,8 @@ namespace Assets.Scripts.Player { worldGenerator.changeCurrentTile(col.gameObject); - worldGenerator.createTile(new Vector3(-1, 0, 0)); - worldGenerator.createTile(new Vector3(1, 0, 0)); - worldGenerator.createTile(new Vector3(0, 0, 1)); - worldGenerator.createTile(new Vector3(0, 0, -1)); - - worldGenerator.createTile(new Vector3(-1, 0, -1)); - worldGenerator.createTile(new Vector3(1, 0, -1)); - worldGenerator.createTile(new Vector3(-1, 0, 1)); - worldGenerator.createTile(new Vector3(1, 0, 1)); + worldGenerator.generateMissingTiles(); + worldGenerator.hideFarTiles(); } if (col.name.Contains("House")) diff --git a/Assets/Scripts/Tile.cs b/Assets/Scripts/Tile.cs index ebe3596..f1b0d55 100644 --- a/Assets/Scripts/Tile.cs +++ b/Assets/Scripts/Tile.cs @@ -29,6 +29,16 @@ public class Tile : MonoBehaviour } + public void show() + { + gameObject.SetActive(true); + } + + public void hide() + { + gameObject.SetActive(false); + } + public void generateContent() { foreach (Vector3 position in getSpawnLocations()) diff --git a/Assets/Scripts/WorldGenerator.cs b/Assets/Scripts/WorldGenerator.cs index 315c567..ea7dde9 100644 --- a/Assets/Scripts/WorldGenerator.cs +++ b/Assets/Scripts/WorldGenerator.cs @@ -96,6 +96,57 @@ public class WorldGenerator : MonoBehaviour newTile.GetComponent().generateTile(pos, (TileType)Enum.Parse(typeof(TileType), newTile.name.Split("_")[0])); tiles.Add(pos, newTile); } + else + { + tiles[pos].GetComponent().show(); + } + } + + public void hideTile(Vector3 requiredPosition) + { + Vector3 pos = currentTile.GetComponent().getPosition() + requiredPosition; + if (tiles.ContainsKey(pos) && pos.y == 0) + { + tiles[pos].GetComponent().hide(); + } + } + + public void generateMissingTiles() + { + createTile(new Vector3(-1, 0, 0)); + createTile(new Vector3(1, 0, 0)); + createTile(new Vector3(0, 0, 1)); + createTile(new Vector3(0, 0, -1)); + + createTile(new Vector3(-1, 0, -1)); + createTile(new Vector3(1, 0, -1)); + createTile(new Vector3(-1, 0, 1)); + createTile(new Vector3(1, 0, 1)); + } + + public void hideFarTiles() + { + hideTile(new Vector3(-2, 0, -2)); + hideTile(new Vector3(-2, 0, -1)); + hideTile(new Vector3(-2, 0, 0)); + hideTile(new Vector3(-2, 0, 1)); + hideTile(new Vector3(-2, 0, 2)); + + hideTile(new Vector3(2, 0, -2)); + hideTile(new Vector3(2, 0, -1)); + hideTile(new Vector3(2, 0, 0)); + hideTile(new Vector3(2, 0, 1)); + hideTile(new Vector3(2, 0, 2)); + + hideTile(new Vector3(-1, 0, -2)); + hideTile(new Vector3(0, 0, -2)); + hideTile(new Vector3(1, 0, -2)); + hideTile(new Vector3(2, 0, -2)); + + hideTile(new Vector3(-2, 0, 2)); + hideTile(new Vector3(-1, 0, 2)); + hideTile(new Vector3(0, 0, 2)); + hideTile(new Vector3(1, 0, 2)); } public void changeCurrentTile(GameObject newCurrent)