Attempting to commit framework code and character controller. Please work. - Don
This commit is contained in:
parent
66b8c265a1
commit
e16a46bd57
9
Assets/Resources/Materials.meta
Normal file
9
Assets/Resources/Materials.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f80300c8efcde24894c3c89a8c393bc
|
||||
folderAsset: yes
|
||||
timeCreated: 1454133135
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Assets/Resources/Materials/Object_Materials.meta
Normal file
9
Assets/Resources/Materials/Object_Materials.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fcea9ce4bc0c3c74d8a3299271a8702c
|
||||
folderAsset: yes
|
||||
timeCreated: 1454133156
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Assets/Resources/Materials/Physics.meta
Normal file
9
Assets/Resources/Materials/Physics.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6795327771d1f94dbd3e765cbdfb2ec
|
||||
folderAsset: yes
|
||||
timeCreated: 1454133142
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Binary file not shown.
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c1198204bc64ae347b6e0a54aef13a9b
|
||||
timeCreated: 1454133168
|
||||
licenseType: Free
|
||||
NativeFormatImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Binary file not shown.
@ -1,15 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class Character : MonoBehaviour {
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
79
Assets/Resources/Scripts/Controller.cs
Normal file
79
Assets/Resources/Scripts/Controller.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using GameUtils;
|
||||
|
||||
/// <summary>
|
||||
/// The base "controller" class defines axes of movement, speed of movement, and the controls needed to
|
||||
/// move a character in 3D space. To extend this to either first or third person control, axes of movement
|
||||
/// are always defined according to the camera.
|
||||
/// </summary>
|
||||
public class Controller : MonoBehaviour
|
||||
{
|
||||
public float movementSpeed, turnSpeed, groundCheckDistance; // speeds set in editor, intensity from iManager.
|
||||
|
||||
[HideInInspector]
|
||||
public bool onGround, canJump, wallDetected;
|
||||
|
||||
[HideInInspector]
|
||||
public Collider2D characterCollider;
|
||||
|
||||
[HideInInspector]
|
||||
public List<CollisionPoint> collPointList; // This is the useful thing for us...
|
||||
|
||||
[HideInInspector]
|
||||
public float movementIntensity;
|
||||
|
||||
[HideInInspector]
|
||||
public Vector2 movementVector;
|
||||
|
||||
[HideInInspector]
|
||||
public Dictionary<GameInput, bool> gameInputMap;
|
||||
|
||||
[HideInInspector]
|
||||
public Camera mainCamera;
|
||||
|
||||
[HideInInspector]
|
||||
public bool groundCast, isBoxColliding;
|
||||
|
||||
private Vector3 commonGroundSearchPoint;
|
||||
private Vector2 jumpCheckObjectPosition;
|
||||
private Transform jumpCheckObject;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
characterCollider = gameObject.GetComponent<BoxCollider2D>(); // Change this if collider shape changes.
|
||||
jumpCheckObject = transform.Find("JumpCheckPosition");
|
||||
// Ground cast vectors are the positions on the bottom corners of the player's hitbox. We use these to determine
|
||||
// if the player is on the ground, which is basic information to be used by derivative classes.
|
||||
// Remember that the character's transform position is sitting on the center-top of its hitbox.
|
||||
}
|
||||
|
||||
void OnCollisionStay2D(Collision2D other)
|
||||
{
|
||||
isBoxColliding = true;
|
||||
}
|
||||
|
||||
void OnCollisionExit2D(Collision2D other)
|
||||
{
|
||||
isBoxColliding = false;
|
||||
}
|
||||
|
||||
void FixedUpdate() // Only ever runs at 30 fps, so we save some computation time on these casts at least...
|
||||
{
|
||||
Debug.DrawLine(jumpCheckObject.position, jumpCheckObject.position + -jumpCheckObject.up * groundCheckDistance, Color.green);
|
||||
groundCast = Physics2D.Raycast(jumpCheckObject.position, -jumpCheckObject.up, groundCheckDistance);
|
||||
print("Debug groundCast: " + groundCast);
|
||||
if (groundCast && isBoxColliding)
|
||||
{
|
||||
onGround = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
onGround = false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void HandleInput() { } // Note that one input should move the variable camera position object around the player...
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04615787dccc1d642a748a80392b5b82
|
||||
timeCreated: 1454120443
|
||||
guid: eb4f819bc107d974f8c76f7cbec5ef15
|
||||
timeCreated: 1454122395
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
@ -1,15 +1,52 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using GameUtils;
|
||||
|
||||
public class GameManager : MonoBehaviour {
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
|
||||
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
public static GameManager gManager; // gManager for GameManager, iManager for InputManager, etc...
|
||||
public static InputManager iManager;
|
||||
|
||||
private GameObject[] tagged_objects;
|
||||
private ISceneObject[] scene_objects;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (gManager == null)
|
||||
{
|
||||
gManager = this;
|
||||
iManager = new InputManager(0.1f, 0.2f);
|
||||
tagged_objects = GameObject.FindGameObjectsWithTag("Scene_Object");
|
||||
scene_objects = new ISceneObject[tagged_objects.Length];
|
||||
iManager.Initialize();
|
||||
// Initialize the list of scene objects, all of which have ONE ISceneObject component.
|
||||
for (int i = 0; i < tagged_objects.Length; i++)
|
||||
{
|
||||
scene_objects[i] = tagged_objects[i].GetComponent<ISceneObject>(); // Grab all of those scene objects!
|
||||
}
|
||||
|
||||
// Initialize all scene objects.
|
||||
for (int j = 0; j < scene_objects.Length; j++)
|
||||
{
|
||||
scene_objects[j].Initialize();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update all scene objects in one update loop.
|
||||
/// </summary>
|
||||
void Update()
|
||||
{
|
||||
iManager.ObjectUpdate();
|
||||
for (int j = 0; j < scene_objects.Length; j++)
|
||||
{
|
||||
scene_objects[j].ObjectUpdate();
|
||||
}
|
||||
}
|
||||
}
|
114
Assets/Resources/Scripts/InputManager.cs
Normal file
114
Assets/Resources/Scripts/InputManager.cs
Normal file
@ -0,0 +1,114 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using GameUtils;
|
||||
using System.Diagnostics;
|
||||
|
||||
/// <summary>
|
||||
/// The game's input manager. Should behave as a singleton, as it is only created by the GameManager singleton.
|
||||
/// Offers the public use of an input dictionary and movement information (vector, intensity, etc.)
|
||||
/// </summary>
|
||||
public class InputManager : ISceneObject
|
||||
{
|
||||
public Vector2 movementVector, rightStickVector; // the final calculated movement vector, passed for handling to controller.
|
||||
// typically, the movement of the left stick/WASD.
|
||||
public float deadZone, rDeadZone, movementIntensity, rightStickIntensity; // How hard is the player pushing the stick in any direction?
|
||||
public Dictionary<GameInput, bool> gameInputMap;
|
||||
|
||||
private Camera mainCamera;
|
||||
private Vector2 inputUpVector, inputRightVector; // a normalized vector created from the literal horizontal/vertical input.
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a high-level handler for input. Keeps a movement
|
||||
/// vector that resets to 0 on no input, along with a
|
||||
/// button dictionary reading input from Unity, so that you don't have to.
|
||||
/// </summary>
|
||||
/// <param name="intendedDeadZone"> The deadzone for joystick input. Not important for keyboard input.</param>
|
||||
public InputManager(float intendedDeadZone, float rightStickDeadZone)
|
||||
{
|
||||
deadZone = intendedDeadZone; // To pass this as a parameter, because I felt like it.
|
||||
rDeadZone = rightStickDeadZone;
|
||||
// We could just run Initialize() here, really...
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
movementVector = Vector2.zero;
|
||||
rightStickVector = Vector2.zero;
|
||||
mainCamera = Camera.main;
|
||||
gameInputMap = new Dictionary<GameInput, bool>() { { GameInput.JUMP, false } }; // Add more input types as needed.
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The input tracking functions defined below work only for standard Xbox360 controller layouts, as that is common
|
||||
/// among gamers. For other types of controllers, more advanced methods of input tracking would be needed, most
|
||||
/// likely by pulling info from a configuration text file.
|
||||
///
|
||||
/// I also need to setup the other possible buttons/joystick axes in the Input Manager. I will return to do this
|
||||
/// later, as for right now all I need to worry about it gameplay testing and mechanics. A pattern similar to the
|
||||
/// one used below would be used for OSX, were I to tackle that today.
|
||||
///
|
||||
/// Note that the left joystick never needs to be tampered with, as Unity's global Horizontal and Vertical buttons
|
||||
/// act like axes and always seem to be mapped correctly.
|
||||
///
|
||||
/// Also note: On Windows, using Xbox 360 controllers, the triggers are seen as one axis, the 3rd axis. This is by
|
||||
/// design.
|
||||
///
|
||||
/// "The triggers are seen as a single joystick axis with the R Trigger going from 0 to 1 and the L trigger going from
|
||||
/// 0 to -1 on the same axis. If both are pressed at the same time they negate the effect of each other to offer a
|
||||
/// "smooth accelration + break" effect (as racing games and games with similar acc / break schemes are the primary
|
||||
/// reason for the triggers)."
|
||||
///
|
||||
/// Check the InputManager for this project to see the available inputs for Windows. Note that these buttons need to
|
||||
/// be set in the InputManager of any project that this controller is ported to.
|
||||
/// </summary>
|
||||
[Conditional("UNITY_EDITOR_WIN"), Conditional("UNITY_STANDALONE_WIN")]
|
||||
private void TrackInput_Windows()
|
||||
{
|
||||
gameInputMap[GameInput.JUMP] = (Input.GetButton("Xbox360Controller_A") || Input.GetButtonDown("Xbox360Controller_A") || Input.GetButtonDown("Jump") || Input.GetButton("Jump"));
|
||||
}
|
||||
|
||||
[Conditional("UNITY_EDITOR_OSX"), Conditional("UNITY_STANDALONE_OSX")]
|
||||
private void TrackInput_MacOSX()
|
||||
{
|
||||
UnityEngine.Debug.Log("You shouldn't see this...");
|
||||
}
|
||||
|
||||
//... other input tracking methods will be needed to track input on different consoles and Linux.
|
||||
|
||||
public void ObjectUpdate()
|
||||
{
|
||||
TrackInput_Windows();
|
||||
TrackInput_MacOSX();
|
||||
if (Input.GetAxis("Vertical") > deadZone ||
|
||||
Input.GetAxis("Horizontal") > deadZone ||
|
||||
Input.GetAxis("Vertical") < -deadZone ||
|
||||
Input.GetAxis("Horizontal") < -deadZone)
|
||||
|
||||
{
|
||||
movementVector = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
|
||||
UnityEngine.Debug.Log(movementVector);
|
||||
movementIntensity = movementVector.magnitude; // Always 1 on keyboard, but not joystick!
|
||||
}
|
||||
else
|
||||
{
|
||||
movementIntensity = 0.0f;
|
||||
movementVector = Vector2.zero;
|
||||
}
|
||||
|
||||
if (Input.GetAxis("RHorizontal") > rDeadZone ||
|
||||
Input.GetAxis("RHorizontal") < -rDeadZone ||
|
||||
Input.GetAxis("RVertical") > rDeadZone ||
|
||||
Input.GetAxis("RVertical") < -rDeadZone)
|
||||
{
|
||||
rightStickVector = new Vector2(Input.GetAxis("RHorizontal"), Input.GetAxis("RVertical"));
|
||||
rightStickIntensity = rightStickVector.magnitude;
|
||||
}
|
||||
else
|
||||
{
|
||||
rightStickIntensity = 0.0f;
|
||||
rightStickVector = Vector2.zero;
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/Resources/Scripts/InputManager.cs.meta
Normal file
12
Assets/Resources/Scripts/InputManager.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df3fc0ea74484414d8eab988c8ea33f6
|
||||
timeCreated: 1454122161
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,15 +1,62 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using GameUtils;
|
||||
|
||||
public class PlayerCharacter : MonoBehaviour {
|
||||
public class PlayerCharacter : Controller, ISceneObject {
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
// TPC-Specific Variables
|
||||
//private bool onGround, canJump, wallDetected; // Other bools should be setup for the different input states (charging, jumping, etc.)
|
||||
private Rigidbody2D playerRB;
|
||||
private float distanceToWall;
|
||||
private RaycastHit wallHit;
|
||||
//private Collider playerCollider;
|
||||
private Vector2 joystickMovement, jumpMovement;
|
||||
|
||||
// TPC Editor Variables
|
||||
public float surfaceNormalAngleThreshold, jumpMagnitude, chargeSpeedModifier, wallRayCastDistance;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
// Put initialization code here.
|
||||
movementVector = GameManager.iManager.movementVector; // Shorthand for the InputManager's movement vector.
|
||||
movementIntensity = GameManager.iManager.movementIntensity;
|
||||
gameInputMap = GameManager.iManager.gameInputMap;
|
||||
onGround = false;
|
||||
canJump = true;
|
||||
wallDetected = false;
|
||||
playerRB = gameObject.GetComponent<Rigidbody2D>();
|
||||
mainCamera = Camera.main;
|
||||
distanceToWall = wallRayCastDistance;
|
||||
wallHit = new RaycastHit();
|
||||
joystickMovement = Vector2.zero;
|
||||
jumpMovement = Vector2.zero;
|
||||
}
|
||||
|
||||
public void ObjectUpdate()
|
||||
{
|
||||
print(onGround);
|
||||
movementVector = GameManager.iManager.movementVector; // Shorthand for the InputManager's movement vector.
|
||||
movementIntensity = GameManager.iManager.movementIntensity;
|
||||
|
||||
// Put update code here.
|
||||
if(movementIntensity > 0.0f)
|
||||
{
|
||||
print("Attempting to move: " + movementVector);
|
||||
joystickMovement = new Vector2(movementVector.x, 0.0f) * movementIntensity * movementSpeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
joystickMovement = Vector2.zero;
|
||||
}
|
||||
if(onGround && gameInputMap[GameInput.JUMP])
|
||||
{
|
||||
print("JUUUUUUUUUUUUUUUUUUUUUUMP");
|
||||
jumpMovement = transform.up * jumpMagnitude;
|
||||
}
|
||||
else
|
||||
{
|
||||
jumpMovement = Vector2.zero;
|
||||
}
|
||||
playerRB.velocity = joystickMovement + jumpMovement;
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,52 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System;
|
||||
|
||||
namespace Game_Utils
|
||||
/// <summary>
|
||||
/// This will store away all of our utility functions during this little game jam of mine.
|
||||
/// </summary>
|
||||
namespace GameUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Collision point class containing relevant collision data: point(world space), contact normal, intensity (how hard?)
|
||||
/// </summary>
|
||||
public class CollisionPoint : IComparable<CollisionPoint>
|
||||
{
|
||||
public Vector3 point, normal;
|
||||
public float intensity;
|
||||
|
||||
public int CompareTo(CollisionPoint other)
|
||||
{
|
||||
// Sort by whichever contact normal is closest to pointing straight up.
|
||||
return Vector3.Angle(normal, Vector3.up).CompareTo(Vector3.Angle(other.normal, Vector3.up));
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISceneObject
|
||||
{
|
||||
// Called by the GameBrain on scene switch.
|
||||
void Initialize();
|
||||
// Called by the GameBrain's Update function.
|
||||
void ObjectUpdate();
|
||||
void Initialize();
|
||||
}
|
||||
|
||||
public enum GameInput
|
||||
{
|
||||
JUMP // Add more as needed
|
||||
}
|
||||
|
||||
public static class Utilities
|
||||
{
|
||||
public static Vector2 Vec2(Vector3 input)
|
||||
{
|
||||
return new Vector2(input.x, input.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Game State Enumerator
|
||||
public enum GameState
|
||||
{
|
||||
TITLE, // index 0
|
||||
MAINMENU // index 1
|
||||
// ... this goes on to define levels, most useful if kept in the same loading order that is to be used during build.
|
||||
}
|
||||
}
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user