mirror of
https://github.com/OMGeeky/GameDevGameJamV2.git
synced 2026-01-03 01:56:23 +01:00
Checkpoints & movement
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using OMGeeky;
|
||||
|
||||
using Unity.Mathematics;
|
||||
|
||||
using UnityEngine;
|
||||
@@ -13,7 +15,7 @@ public class Character : MonoBehaviour
|
||||
|
||||
#region Components
|
||||
|
||||
private new Collider2D collider;
|
||||
private new CompositeCollider2D collider;
|
||||
private SpriteRenderer spriteRenderer;
|
||||
|
||||
#endregion
|
||||
@@ -24,7 +26,6 @@ public class Character : MonoBehaviour
|
||||
[SerializeField] private Sprite aliveSprite;
|
||||
[SerializeField] private Sprite deadSprite;
|
||||
[SerializeField] private float dissolveSpeed = 0.05f;
|
||||
[SerializeField] private Transform spawnPosition;
|
||||
[SerializeField] private Transform parentForNew;
|
||||
[SerializeField] private LayerMask findLayerMask;
|
||||
[SerializeField] private float eatRange = 2.5f;
|
||||
@@ -34,6 +35,7 @@ public class Character : MonoBehaviour
|
||||
|
||||
#region Other public data
|
||||
|
||||
public Checkpoint spawnPosition;
|
||||
public Character root;
|
||||
|
||||
#endregion
|
||||
@@ -60,13 +62,13 @@ public class Character : MonoBehaviour
|
||||
SpawnNew();
|
||||
}
|
||||
|
||||
if ( spawnPosition == null )
|
||||
spawnPosition = transform;
|
||||
// if ( spawnPosition == null )
|
||||
// spawnPosition = transform;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
collider = GetComponent<Collider2D>();
|
||||
collider = GetComponent<CompositeCollider2D>();
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
spriteRenderer.sprite = aliveSprite;
|
||||
if ( root != null )
|
||||
@@ -91,7 +93,7 @@ public class Character : MonoBehaviour
|
||||
|
||||
public void KillSelf()
|
||||
{
|
||||
if ( math.distance( transform.position , spawnPosition.position ) < 2 )
|
||||
if ( math.distance( transform.position , spawnPosition.transform.position ) < 2 )
|
||||
{
|
||||
Debug.Log( "Cant restart here, too near to spawn" );
|
||||
return;
|
||||
@@ -109,7 +111,7 @@ public class Character : MonoBehaviour
|
||||
|
||||
private void SpawnNew()
|
||||
{
|
||||
var newPlayer = Instantiate( root , spawnPosition.position , Quaternion.identity , parentForNew );
|
||||
var newPlayer = Instantiate( root , root.spawnPosition.transform.position , Quaternion.identity , parentForNew );
|
||||
newPlayer.Init( root );
|
||||
root.spawnedObjects.Add( newPlayer );
|
||||
Spawned?.Invoke( newPlayer );
|
||||
@@ -121,9 +123,16 @@ public class Character : MonoBehaviour
|
||||
if ( target == null )
|
||||
return;
|
||||
|
||||
var size = target.transform.transform.localScale;
|
||||
size.z = 0;
|
||||
transform.localScale += size;
|
||||
//TODO: buff player after eating
|
||||
// var size = target.transform.transform.localScale;
|
||||
// size.z = 0;
|
||||
|
||||
// var localScale = transform.localScale;
|
||||
// var finalSize = MathHelpers.AddBoxAreaToBox( (Vector2) size , (Vector2) localScale );
|
||||
|
||||
// transform.localScale += size;
|
||||
// localScale = new Vector3( finalSize.x , finalSize.y , localScale.z );
|
||||
// transform.localScale = localScale;
|
||||
target.DissolveSelf();
|
||||
}
|
||||
|
||||
@@ -136,9 +145,11 @@ public class Character : MonoBehaviour
|
||||
var t = transform;
|
||||
var localScale = t.localScale;
|
||||
Array.Clear( findColliders , 0 , findColliders.Length );
|
||||
var bounds = collider.bounds;
|
||||
var size = new Vector2( bounds.size.x + range , bounds.size.y + range );
|
||||
Physics2D.OverlapBoxNonAlloc( point: t.position
|
||||
, size: new Vector2( localScale.x + range , localScale.y + range )
|
||||
, angle: t.rotation.eulerAngles.z
|
||||
, size: size
|
||||
, angle: 0
|
||||
, results: findColliders
|
||||
, layerMask: findLayerMask );
|
||||
|
||||
@@ -207,6 +218,10 @@ public class Character : MonoBehaviour
|
||||
return;
|
||||
|
||||
target.transform.parent = transform;
|
||||
|
||||
// var targetInput = target.GetComponent<InputToMovement>();
|
||||
target.isActivePlayer = false;
|
||||
|
||||
RemovePlayerComponentsFromTarget( target );
|
||||
}
|
||||
|
||||
@@ -214,10 +229,11 @@ public class Character : MonoBehaviour
|
||||
{
|
||||
var targetRb = target.GetComponent<Rigidbody2D>();
|
||||
var targetCol = target.GetComponent<CompositeCollider2D>();
|
||||
var targetController = target.GetComponent<CharacterController2D>();
|
||||
var targetInput = target.GetComponent<InputToMovement>();
|
||||
Destroy( targetInput );
|
||||
Destroy( targetController );
|
||||
|
||||
// var targetController = target.GetComponent<CharacterController2D>();
|
||||
// var targetInput = target.GetComponent<InputToMovement>();
|
||||
// Destroy( targetInput );
|
||||
// Destroy( targetController );
|
||||
Destroy( targetCol );
|
||||
Destroy( targetRb );
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
|
||||
using Unity.Mathematics;
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
@@ -31,12 +33,14 @@ public class CharacterController2D : MonoBehaviour
|
||||
}
|
||||
|
||||
private Collider2D[] colliders = new Collider2D[10];
|
||||
[SerializeField] private float speed = 10f;
|
||||
[SerializeField] private float maxHorizontalSpeedUp = 30f;
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if ( !active )
|
||||
return;
|
||||
|
||||
|
||||
m_Grounded = false;
|
||||
|
||||
Array.Clear( colliders , 0 , colliders.Length );
|
||||
@@ -69,18 +73,20 @@ public class CharacterController2D : MonoBehaviour
|
||||
|
||||
public void Move( float move , bool jump )
|
||||
{
|
||||
if(!active)
|
||||
if ( !active )
|
||||
return;
|
||||
|
||||
|
||||
//only control the player if grounded or airControl is turned on
|
||||
if ( m_Grounded || airControl )
|
||||
{
|
||||
// Move the character by finding the target velocity
|
||||
var velocity = m_Rigidbody2D.velocity;
|
||||
Vector3 targetVelocity = new Vector2( move * 10f , velocity.y );
|
||||
Vector3 targetVelocity = new Vector2( move * speed , 0 );
|
||||
if ( velocity.x < maxHorizontalSpeedUp && velocity.x > -maxHorizontalSpeedUp )
|
||||
m_Rigidbody2D.AddForce( targetVelocity , ForceMode2D.Impulse );
|
||||
|
||||
// And then smoothing it out and applying it to the character
|
||||
m_Rigidbody2D.velocity = Vector3.SmoothDamp( velocity , targetVelocity , ref m_Velocity , movementSmoothing );
|
||||
// m_Rigidbody2D.velocity = Vector3.SmoothDamp( velocity , targetVelocity , ref m_Velocity , movementSmoothing );
|
||||
}
|
||||
|
||||
// If the player should jump...
|
||||
|
||||
34
Assets/Scripts/Checkpoint.cs
Normal file
34
Assets/Scripts/Checkpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OMGeeky
|
||||
{
|
||||
public class Checkpoint : MonoBehaviour
|
||||
{
|
||||
private Animator animator;
|
||||
private static readonly int IsActive = Animator.StringToHash( "IsActive" );
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
public void SetInactive()
|
||||
{
|
||||
if(animator != null)
|
||||
animator.SetBool( IsActive, false );
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D( Collider2D other )
|
||||
{
|
||||
if ( other.TryGetComponent( out Character character ) )
|
||||
{
|
||||
character.root.spawnPosition.SetInactive();
|
||||
character.root.spawnPosition = this;
|
||||
animator.SetBool( IsActive, true );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Checkpoint.cs.meta
Normal file
11
Assets/Scripts/Checkpoint.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4fac85d72798bcb4f8597a7cae9b4ad9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
77
Assets/Scripts/MathHelpers.cs
Normal file
77
Assets/Scripts/MathHelpers.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
|
||||
using Unity.Mathematics;
|
||||
|
||||
|
||||
public static class MathHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the area of the two boxes
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns>The new box with the area of both boxes</returns>
|
||||
public static float3 AddBoxAreaToBox( float3 a , float3 b )
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
/*
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
|
||||
if ( a.y == 0 )
|
||||
y = b.y;
|
||||
else if ( b.y == 0 )
|
||||
y = a.y;
|
||||
else
|
||||
y = ((b.x / a.x) * b.y) / a.y;
|
||||
|
||||
if ( a.x == 0 )
|
||||
x = b.x;
|
||||
else if ( b.x == 0 )
|
||||
x = a.x;
|
||||
else
|
||||
x = ((b.y / a.y) * b.x) / a.x;
|
||||
|
||||
if ( a.z == 0 )
|
||||
z = b.z;
|
||||
else if ( b.z == 0 )
|
||||
z = a.z;
|
||||
else
|
||||
{
|
||||
z = ((b.y / a.y) * b.x) / a.x;
|
||||
}
|
||||
|
||||
return new float3( x , y , z );
|
||||
*/
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the area of the two boxes
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns>The new box with the area of both boxes</returns>
|
||||
public static float2 AddBoxAreaToBox( float2 a , float2 b )
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
|
||||
if ( a.y == 0 )
|
||||
y = b.y;
|
||||
else if ( b.y == 0 )
|
||||
y = a.y;
|
||||
else
|
||||
y = (((b.x / a.x) * b.y) / a.y) + a.y;
|
||||
|
||||
if ( a.x == 0 )
|
||||
x = b.x;
|
||||
else if ( b.x == 0 )
|
||||
x = a.x;
|
||||
else
|
||||
x = (((b.y / a.y) * b.x) / a.x) + a.x;
|
||||
|
||||
return new float2( x , y );
|
||||
}
|
||||
|
||||
}
|
||||
3
Assets/Scripts/MathHelpers.cs.meta
Normal file
3
Assets/Scripts/MathHelpers.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3cbddf56b1af4211bda3f1bcf8453080
|
||||
timeCreated: 1653240870
|
||||
Reference in New Issue
Block a user