diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index 3a89200..3decb6e 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -113,6 +113,106 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1 &14655764 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 14655767} + - component: {fileID: 14655766} + - component: {fileID: 14655765} + m_Layer: 0 + m_Name: Grass-Middle (6) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!61 &14655765 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 14655764} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0.5, y: 0.5} + oldSize: {x: 0.5, y: 0.5} + newSize: {x: 0.5, y: 0.5} + adaptiveTilingThreshold: 0.5 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 0.5, y: 0.5} + m_EdgeRadius: 0 +--- !u!212 &14655766 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 14655764} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RenderingLayerMask: 4294967295 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 0 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: 9c2dea53371f58841be290a540505639, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 0.5, y: 0.5} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 +--- !u!4 &14655767 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 14655764} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 1.59, y: 2.44, z: 0} + m_LocalScale: {x: 3.656072, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 8 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &545594683 GameObject: m_ObjectHideFlags: 0 @@ -736,3 +836,103 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 6 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 90} +--- !u!1 &2136553079 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 2136553082} + - component: {fileID: 2136553081} + - component: {fileID: 2136553080} + m_Layer: 0 + m_Name: Grass-Middle (5) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!61 &2136553080 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 2136553079} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0.5, y: 0.5} + oldSize: {x: 0.5, y: 0.5} + newSize: {x: 0.5, y: 0.5} + adaptiveTilingThreshold: 0.5 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 0.5, y: 0.5} + m_EdgeRadius: 0 +--- !u!212 &2136553081 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 2136553079} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RenderingLayerMask: 4294967295 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 0 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: 9c2dea53371f58841be290a540505639, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 0.5, y: 0.5} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 +--- !u!4 &2136553082 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 2136553079} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -0.31, y: 5.17, z: 0} + m_LocalScale: {x: 3.656072, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 7 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Scripts/Earthworm.cs b/Assets/Scripts/Earthworm.cs index 031d31e..0824ae6 100644 --- a/Assets/Scripts/Earthworm.cs +++ b/Assets/Scripts/Earthworm.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using UnityEngine; public class Earthworm : MonoBehaviour @@ -10,6 +11,8 @@ public class Earthworm : MonoBehaviour public LayerMask mask; private Vector3 position; + private List rope1Position = new List(); + private List rope2Position = new List(); private Dictionary actions = new Dictionary(); @@ -19,11 +22,6 @@ public class Earthworm : MonoBehaviour Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, position.y, Camera.main.transform.position.z); - #region Manette - //float horizontal = Input.GetAxis("Horizontal"); - //float vertical = Input.GetAxis("Vertical"); - //Vector2 direction = new Vector2(horizontal, vertical).normalized; - #endregion Vector3 camera = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 direction = camera - position; @@ -48,20 +46,63 @@ public class Earthworm : MonoBehaviour Destroy(actions[KeyCode.Q].joint); Destroy(actions[KeyCode.Q].line); actions.Remove(KeyCode.Q); + rope1Position.Clear(); } if (Input.GetKeyUp(KeyCode.D) && actions.ContainsKey(KeyCode.D)) { Destroy(actions[KeyCode.D].joint); Destroy(actions[KeyCode.D].line); actions.Remove(KeyCode.D); + rope2Position.Clear(); } - if(actions.ContainsKey(KeyCode.Q)) - actions[KeyCode.Q].line.GetComponent().SetPosition(0, transform.GetChild(0).position); + if (actions.ContainsKey(KeyCode.Q)) + { + LineRenderer line = actions[KeyCode.Q].line.GetComponent(); + rope1Position[0] = transform.GetChild(0).position; + Vector3 hookDirection = rope1Position[1] - transform.GetChild(0).position; + + RaycastHit2D ray = Physics2D.Raycast(transform.GetChild(0).position, hookDirection.normalized, hookDirection.magnitude, mask); + if (ray.collider != null && !RopeContain(1, ray.point)) + { + print(ray.collider.transform.name); + PolygonCollider2D collider = ray.collider as PolygonCollider2D; + print(collider.transform.name); + rope1Position.Insert(rope1Position.Count - 2, GetClosestColliderPointFromRaycastHit(ray, collider)); + } + + line.positionCount = rope1Position.Count; + line.SetPositions(rope1Position.ToArray()); + } if (actions.ContainsKey(KeyCode.D)) actions[KeyCode.D].line.GetComponent().SetPosition(0, transform.GetChild(transform.childCount - 1).position); } + private Vector2 GetClosestColliderPointFromRaycastHit(RaycastHit2D hit, PolygonCollider2D polyCollider) + { + var distanceDictionary = polyCollider.points.ToDictionary( + position => Vector2.Distance(hit.point, polyCollider.transform.TransformPoint(position)), + position => polyCollider.transform.TransformPoint(position)); + + var orderedDictionary = distanceDictionary.OrderBy(e => e.Key); + return orderedDictionary.Any() ? orderedDictionary.First().Value : Vector2.zero; + } + + bool RopeContain(int ropeNumber, Vector3 point) + { + if(ropeNumber == 1) + { + foreach(Vector3 pos in rope1Position) + { + if (pos == point) + return true; + //if (Vector3.Distance(pos, point) < 0.5f) + // return true; + } + } + return false; + } + void Grab(KeyCode key, Vector2 direction) { RaycastHit2D hit = Physics2D.Raycast(position, direction, key == KeyCode.Q ? 20 : 150, mask); @@ -77,7 +118,10 @@ public class Earthworm : MonoBehaviour Destroy(actions[KeyCode.Q].joint); Destroy(actions[KeyCode.Q].line); actions.Remove(KeyCode.Q); + rope1Position.Clear(); } + rope1Position.Add(transform.GetChild(0).position); + rope1Position.Add(hit.point); break; case KeyCode.D: joint = (DistanceJoint2D)transform.GetChild(transform.childCount - 1).gameObject.AddComponent(typeof(DistanceJoint2D)); @@ -86,6 +130,7 @@ public class Earthworm : MonoBehaviour Destroy(actions[KeyCode.D].joint); Destroy(actions[KeyCode.D].line); actions.Remove(KeyCode.D); + rope2Position.Clear(); } break; default: