1
Fork 0
mirror of https://github.com/Steffo99/gravity-fusion.git synced 2024-11-21 16:04:18 +00:00

Add merge without conditions

This commit is contained in:
Steffo 2019-10-05 12:36:09 +02:00
parent d736aa97a2
commit fe03b38480
7 changed files with 238 additions and 11 deletions

View file

@ -7,11 +7,10 @@ public class Gravitation : MonoBehaviour
{
[Header("Forces")]
protected Vector3 appliedForce;
protected float forcesIntensity;
[Header("Internals")]
public static List<Gravitation> simulatedObjects;
public int positionInList;
public static List<Gravitation> simulatedObjects;
[Header("References")]
protected new Rigidbody2D rigidbody;
@ -28,7 +27,7 @@ public class Gravitation : MonoBehaviour
}
}
private void Awake() {
private void OnEnable() {
if(simulatedObjects == null) {
simulatedObjects = new List<Gravitation>();
}
@ -36,12 +35,15 @@ public class Gravitation : MonoBehaviour
simulatedObjects.Add(this);
}
private void OnDisable() {
simulatedObjects.Remove(this);
}
private void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
gameController = GameObject.Find("GameController").GetComponent<GameController>();
appliedForce = new Vector3(0f, 0f, 0f);
forcesIntensity = 0f;
}
// O(n²)
@ -53,16 +55,9 @@ public class Gravitation : MonoBehaviour
float force = gameController.gravitationConstant * this.mass * other.mass / Mathf.Pow(distance, 2);
Vector3 direction = (other.transform.position - this.transform.position).normalized;
this.appliedForce += direction * force;
this.forcesIntensity += force;
other.appliedForce -= direction * force;
other.forcesIntensity += force;
}
rigidbody.AddForce(appliedForce);
if(forcesIntensity >= 5f) {
GetComponent<SpriteRenderer>().color = Color.yellow;
}
appliedForce = new Vector3(0, 0, 0);
forcesIntensity = 0f;
}
}

View file

@ -0,0 +1,52 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CircleCollider2D))]
public class Merge : MonoBehaviour
{
[Header("Config")]
public GameObject mergeIntoPrefab;
[Header("References")]
protected new CircleCollider2D collider;
[Header("Internals")]
protected List<Merge> mergeables;
private void Start()
{
collider = GetComponent<CircleCollider2D>();
mergeables = new List<Merge>();
mergeables.Add(this);
}
private void OnTriggerEnter2D(Collider2D other) {
Merge otherMerge = other.GetComponent<Merge>();
mergeables.Add(otherMerge);
if(CanMerge()) DoMerge();
}
private void OnTriggerExit2D(Collider2D other) {
Merge otherMerge = other.GetComponent<Merge>();
mergeables.Remove(otherMerge);
}
protected bool CanMerge() {
return false;
}
protected void DoMerge() {
GameObject mergeResult = Instantiate(mergeIntoPrefab, transform.position, Quaternion.identity);
MergedInfo mergedInfo = mergeResult.AddComponent<MergedInfo>();
mergedInfo.mergedCount = mergeables.Count;
foreach(Merge merged in mergeables) {
Rigidbody2D mergedBody = merged.GetComponentInParent<Rigidbody2D>();
if(mergedBody != null) {
mergedInfo.mergedMass += mergedBody.mass;
}
Destroy(merged.transform.parent);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1010c8363cc1ebd4e9c1808f9a2737e2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MergedInfo : MonoBehaviour
{
public int mergedCount;
public float mergedMass;
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c9b169f8058046d4fb7374586f318ae6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -13,6 +13,7 @@ GameObject:
- component: {fileID: 5473375028011702753}
- component: {fileID: 5473375028011702752}
- component: {fileID: 5473375028011702755}
- component: {fileID: 720206987848026506}
m_Layer: 0
m_Name: Gravitation Object
m_TagString: Untagged
@ -134,3 +135,17 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
positionInList: 0
--- !u!114 &720206987848026506
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5473375028011702754}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1010c8363cc1ebd4e9c1808f9a2737e2, type: 3}
m_Name:
m_EditorClassIdentifier:
requiredForce: 5
transformIntoPrefab: {fileID: 0}

View file

@ -121,6 +121,140 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &467926748
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 467926753}
- component: {fileID: 467926752}
- component: {fileID: 467926751}
- component: {fileID: 467926750}
- component: {fileID: 467926749}
m_Layer: 0
m_Name: Sun
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &467926749
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 467926748}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d16f17bda197c324f8b088f106c66c5c, type: 3}
m_Name:
m_EditorClassIdentifier:
positionInList: 0
--- !u!58 &467926750
CircleCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 467926748}
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}
serializedVersion: 2
m_Radius: 0.5
--- !u!50 &467926751
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 467926748}
m_BodyType: 0
m_Simulated: 1
m_UseFullKinematicContacts: 0
m_UseAutoMass: 0
m_Mass: 10
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 0
m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
--- !u!212 &467926752
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 467926748}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
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_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
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: d8cf31929ca287542bdc219ec19f002e, type: 3}
m_Color: {r: 1, g: 1, b: 0, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 1, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!4 &467926753
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 467926748}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 2, y: 2, z: 2}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1803533283597936005
PrefabInstance:
m_ObjectHideFlags: 0