Init
This commit is contained in:
18
Assets/Scripts/Default.asset
Normal file
18
Assets/Scripts/Default.asset
Normal file
@@ -0,0 +1,18 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!114 &11400000
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 40235c4983cab2c6586fac087c61f7d8, type: 3}
|
||||
m_Name: Default
|
||||
m_EditorClassIdentifier:
|
||||
data:
|
||||
leftInput: 97
|
||||
rightInput: 100
|
||||
jumpInput: 32
|
||||
8
Assets/Scripts/Default.asset.meta
Normal file
8
Assets/Scripts/Default.asset.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e7ed72e05b860b8ba83063bee1a21449
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
59
Assets/Scripts/InputPreset.cs
Normal file
59
Assets/Scripts/InputPreset.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "InputPreset", menuName = "ScriptableObjects/InputPreset", order = 1)]
|
||||
|
||||
public class InputPreset : ScriptableObject
|
||||
{
|
||||
public InputPresetData data;
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class InputPresetData{
|
||||
public KeyCode leftInput;
|
||||
public KeyCode rightInput;
|
||||
public KeyCode jumpInput;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class InputManager{
|
||||
public static InputPresetData m_data;
|
||||
public static InputPresetData data(){
|
||||
if(m_data==null){Init();}
|
||||
return m_data;
|
||||
}
|
||||
|
||||
public static void Init(){
|
||||
if(PlayerPrefs.HasKey("inputSettings")){
|
||||
Debug.Log("Trying to load input data : " + PlayerPrefs.GetString("inputSettings"));
|
||||
try{
|
||||
InputPresetData preset = JsonUtility.FromJson<InputPresetData>(PlayerPrefs.GetString("inputSettings"));
|
||||
m_data = preset;
|
||||
}catch(Exception error){
|
||||
Debug.Log("Error loading controls, Initiating default \n Error:" + error.Message);
|
||||
resetDefault();
|
||||
}
|
||||
}else{
|
||||
resetDefault();
|
||||
}
|
||||
}
|
||||
|
||||
static void resetDefault(){
|
||||
m_data = new InputPresetData();
|
||||
m_data.leftInput = KeyCode.A;
|
||||
m_data.rightInput = KeyCode.D;
|
||||
m_data.jumpInput = KeyCode.Space;
|
||||
|
||||
PlayerPrefs.SetString("inputSettings", JsonUtility.ToJson(m_data));
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
|
||||
public static void Save(){
|
||||
PlayerPrefs.SetString("inputSettings", JsonUtility.ToJson(m_data));
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/InputPreset.cs.meta
Normal file
11
Assets/Scripts/InputPreset.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40235c4983cab2c6586fac087c61f7d8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
237
Assets/Scripts/PlayerController.cs
Normal file
237
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,237 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
public Rigidbody2D rigidbody;
|
||||
public SpriteRenderer sprite;
|
||||
public bool invertSprite;
|
||||
public Animator animator;
|
||||
public LayerMask groundLayerMask;
|
||||
// public float drownedDepth;
|
||||
public bool isGrounded = true;
|
||||
public bool isSwimming = false;
|
||||
public Transform groundChecker;
|
||||
public Vector2 gravity;
|
||||
public float buoyantForce;
|
||||
public float buoyantSpd;
|
||||
public float moveSpeed = 1;
|
||||
public float jumpForce = 10;
|
||||
public float jumpDuration = 0.2f;
|
||||
public AudioClip jumpSFX;
|
||||
float jumpT = 0;
|
||||
public bool listenToInput = true;
|
||||
|
||||
void Start()
|
||||
{
|
||||
jumpT = jumpDuration;
|
||||
}
|
||||
|
||||
bool _grounded = false;
|
||||
float moveInput = 0;
|
||||
bool jumpReleased = true;
|
||||
[HideInInspector]
|
||||
public bool inWater;
|
||||
void FixedUpdate()
|
||||
{
|
||||
isGrounded = getGrounded();
|
||||
|
||||
|
||||
//Listen to grounded value
|
||||
if (_grounded != isGrounded)
|
||||
{
|
||||
if (isGrounded)
|
||||
{
|
||||
OnLand();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnFly();
|
||||
}
|
||||
_grounded = isGrounded;
|
||||
}
|
||||
|
||||
//Water Physics
|
||||
//isSwimming = getInWater() && !isGrounded;
|
||||
if (isSwimming)
|
||||
{
|
||||
// float actualBuoyantForce = (drownedDepth) * buoyantForce;
|
||||
// gravity = new Vector2(gravity.x,actualBuoyantForce );
|
||||
// // gravity = new Vector2(gravity.x,((rigidbody.velocity.y < 0)?actualBuoyantForce : 0));
|
||||
// if( (Input.GetAxis("Jump") > 0) ){
|
||||
// gravity+=new Vector2(0,buoyantForce);
|
||||
// }
|
||||
rigidbody.velocity = new Vector2(rigidbody.velocity.x, Mathf.Lerp(rigidbody.velocity.y, buoyantForce, buoyantSpd));
|
||||
if ((Input.GetKey(InputManager.data().jumpInput)))
|
||||
{
|
||||
rigidbody.velocity = new Vector2(rigidbody.velocity.x, jumpForce / 12f);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
gravity = new Vector2(gravity.x, 0);
|
||||
}
|
||||
|
||||
//Gravity Application
|
||||
rigidbody.AddForce(gravity);
|
||||
|
||||
//Update In-Air value on animation
|
||||
animator.SetBool("inAir", !isGrounded);
|
||||
animator.SetBool("isOnWater", inWater);
|
||||
|
||||
float HorizontalAxis = 0;
|
||||
if (Input.GetKey(InputManager.data().leftInput)) { HorizontalAxis = -1; } else if (Input.GetKey(InputManager.data().rightInput)) { HorizontalAxis = 1; }
|
||||
// if (GameManager.isPaused) { HorizontalAxis = 0; }
|
||||
//Move according to input
|
||||
if (listenToInput)
|
||||
{
|
||||
if (isGrounded)
|
||||
{
|
||||
moveInput = HorizontalAxis;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Change moveInput while in-air | IF there is an input
|
||||
if (Input.GetKey(InputManager.data().leftInput) || Input.GetKey(InputManager.data().rightInput))
|
||||
{
|
||||
moveInput = Mathf.Lerp(moveInput, HorizontalAxis, 0.2f);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (isSwimming || inWater) { moveInput = moveInput / 2f; }
|
||||
//Update moving value on Animation
|
||||
animator.SetBool("moving", (moveInput != 0));
|
||||
animator.SetBool("isSwimming", isSwimming);
|
||||
//Flip Image to face moving Direction
|
||||
if (moveInput < 0)
|
||||
{
|
||||
sprite.flipX = (invertSprite) ? false : true;
|
||||
}
|
||||
else if (moveInput > 0)
|
||||
{
|
||||
sprite.flipX = (invertSprite) ? true : false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Apply moving input to player
|
||||
rigidbody.transform.Translate(new Vector2(moveSpeed * moveInput, 0));
|
||||
|
||||
|
||||
bool _canJump = canJump();
|
||||
if ((Input.GetKey(InputManager.data().jumpInput)) && _canJump)
|
||||
{
|
||||
jumpT = 0;
|
||||
jumpReleased = false;
|
||||
rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
|
||||
if(jumpSFX!=null)AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
|
||||
}
|
||||
|
||||
|
||||
//Blocks continous jump button
|
||||
if (!Input.GetKey(InputManager.data().jumpInput)) { jumpReleased = true; }
|
||||
|
||||
//Apply Jump to player
|
||||
if (jumpT < jumpDuration)
|
||||
{
|
||||
jumpT += Time.deltaTime;
|
||||
float progress = (jumpDuration - jumpT) / jumpDuration;
|
||||
//|| jumpT < jumpDuration/2f
|
||||
if ((Input.GetKey(InputManager.data().jumpInput)) || b) { rigidbody.AddForce(new Vector2(0, jumpForce * progress)); }
|
||||
}
|
||||
else { b = false; }
|
||||
|
||||
_isSwimming = inWater;
|
||||
|
||||
}
|
||||
bool b;
|
||||
bool _isSwimming;
|
||||
public bool waterBoost;
|
||||
public bool canJump()
|
||||
{
|
||||
return jumpT >= jumpDuration && isGrounded && listenToInput && jumpReleased && !isSwimming;//&& !GameManager.isPaused
|
||||
}
|
||||
|
||||
public bool canBotJump()
|
||||
{
|
||||
return isGrounded || isSwimming || inWater;
|
||||
}
|
||||
|
||||
//External Player Control Exposure
|
||||
public void Jump()
|
||||
{
|
||||
jumpT = 0;
|
||||
rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
|
||||
b = true;
|
||||
AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
|
||||
}
|
||||
public void move(float input)
|
||||
{
|
||||
input = Mathf.Clamp(input, -1, 1);
|
||||
|
||||
moveInput = input;
|
||||
}
|
||||
|
||||
|
||||
void OnLand()
|
||||
{
|
||||
// Debug.Log("Landed Like a boss!");
|
||||
}
|
||||
|
||||
void OnFly()
|
||||
{
|
||||
// Debug.Log("Hey look im flying");
|
||||
}
|
||||
|
||||
public bool getGrounded()
|
||||
{
|
||||
//return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
|
||||
Collider2D col = GetComponentInChildren<Collider2D>();
|
||||
return (Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y), 0, Vector2.down, 0.1f, groundLayerMask));
|
||||
}
|
||||
// public bool getInWater(){Collider2D col = GetComponentInChildren<Collider2D>();
|
||||
// return (Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y), 0, Vector2.down, 0.1f, waterLayerMask));
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// public static class SettingsInstance{
|
||||
// public static Settings instance;
|
||||
|
||||
// }
|
||||
|
||||
public static class AudioSingleton{
|
||||
private static AudioSource music;
|
||||
private static AudioSource sfx;
|
||||
|
||||
public static AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
|
||||
public static AudioSource getMusicSource(){
|
||||
|
||||
if(music == null){
|
||||
GameObject go = new GameObject("Music Audio Source");
|
||||
go.AddComponent<AudioSource>();
|
||||
music = go.GetComponent<AudioSource>();
|
||||
music.outputAudioMixerGroup = mixer.FindMatchingGroups("Music")[0];
|
||||
}
|
||||
|
||||
return music;
|
||||
}
|
||||
public static AudioSource getSFXSource(){
|
||||
//AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
|
||||
if(sfx == null){
|
||||
GameObject go = new GameObject("SFX Audio Source");
|
||||
go.AddComponent<AudioSource>();
|
||||
sfx= go.GetComponent<AudioSource>();
|
||||
Debug.Log("sfx : " + (sfx==null).ToString() + ", mixer: " + (mixer == null).ToString() );
|
||||
sfx.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];
|
||||
}
|
||||
|
||||
return sfx;
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Scripts/PlayerController.cs.meta
Normal file
11
Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7551b48e43866812687410cc03be59dc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user