moar shit

This commit is contained in:
Marquitos
2016-05-15 20:21:29 -03:00
parent 8a4ae25e04
commit e7f07a6e53
59 changed files with 800 additions and 505 deletions

View File

@@ -0,0 +1,204 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Forces
{
public class Force : System.Object
{
public ForceProps properties;
protected Vector2 goal_vector = Vector2.up;
protected Vector2 last_velocity = Vector2.zero;
protected Vector2 init_in = Vector2.zero;
protected Vector2 init_out = Vector2.zero;
protected Vector2 current_output = Vector2.zero;
protected float init_time = 0.0f;
/// <summary>
/// last call from velocity method
/// </summary>
protected float last_step_time = 0.0f;
/// <summary>
/// last call from user (like AddForce) to now when start fading out
/// </summary>
protected float last_call_time = 0.0f;
protected float current_delta_step = 0.0f;
protected float current_delta_call = 0.0f;
protected float current_delta_notCall = 0.0f;
/// <summary>
/// time since init calling
/// </summary>
protected float time_calling = 0.0f;
/// <summary>
/// absolute time since init
/// </summary>
protected float time_stepping = 0.0f;
/// <summary>
/// time since left calling
/// </summary>
protected float time_notCalling = 0.0f;
protected bool killed = false;
public Force(string id, Vector2 target_direction, float target_speed, float inertia_in = 1.0f, float inertia_out = 1.0f, float impulse = 0.0f)
{
properties = new ForceProps(id, target_speed, inertia_in, inertia_out, impulse);
Call(target_direction);
}
public Force(ForceProps props, Vector2 target_direction)
{
properties = props;
Call(target_direction);
}
public Vector2 Velocity()
{
Vector2 delta = Vector2.zero;
Vector2 current_target_velocity = Vector2.zero;
if (init_time == 0.0f) Initialize();
StartStep();
current_target_velocity = FadeIn() + FadeOut();
delta = current_target_velocity;
current_output = Clamp(last_velocity + delta, goal_vector);
EndStep();
return current_output;
}
/// <summary>
/// called by user to add force
/// </summary>
public void Call(Vector2 new_goal_vector)
{
if (properties.forceMode == ForceMode.Impulse)
Initialize();
last_call_time = Time.time;
goal_vector = new_goal_vector.normalized * properties.target_speed;
}
/// <summary>
/// Determines if the force is being used after reaching zero force
/// </summary>
/// <returns></returns>
public bool Using()
{
//Wait half a second to check if the velocity is null
return ((Time.time - last_call_time) < 0.5f || last_velocity != Vector2.zero) && !killed;
}
public void Kill ()
{
last_call_time = 0.0f;
current_delta_call = 0.0f;
current_delta_notCall = 0.0f;
last_velocity = Vector2.zero;
killed = true;
}
/// <summary>
/// Returns the value of the last call
/// </summary>
/// <returns></returns>
public Vector2 LastVelocity()
{
return last_velocity;
}
protected void Initialize()
{
init_time = Time.time;
}
protected void StartStep()
{
///Is the force acting like an impulse? if it is, then stop calling if impulse time < currentTime
bool impulse = (properties.impulse > 0.0f && Time.time - init_time < properties.impulse);
current_delta_step = last_step_time == 0.0f ? 0.0f : Time.time - last_step_time;
if (last_call_time == 0.0f)
{
current_delta_call = 0.0f;
current_delta_notCall = 0.0f;
}
else
{
if (last_step_time - last_call_time > current_delta_step && !impulse)
{
current_delta_notCall = current_delta_step;
///set a start point to fade velocity
init_out = last_velocity;
current_delta_call = 0.0f;
time_calling = 0.0f;
}
else
{
current_delta_call = current_delta_step;
///set a start point to fade velocity
init_in = last_velocity;
current_delta_notCall = 0.0f;
time_notCalling = 0.0f;
}
}
time_calling += current_delta_call;
time_stepping += current_delta_step;
}
protected Vector2 FadeIn()
{
// float fade_in_fact = 0.0f;
Vector2 output = Vector2.zero;
if (current_delta_call <= 0.0f) return Vector2.zero;
//fade_in_fact = Mathf.Clamp01(time_calling / inertia_in);
output = (goal_vector - last_velocity) * (current_delta_call / properties.inertia_in);
return output;
}
protected Vector2 FadeOut()
{
//float fade_out_fact = 0.0f;
Vector2 output = Vector2.zero;
if (current_delta_notCall <= 0.0f) return Vector2.zero;
//fade_out_fact = Mathf.Clamp01(time_notCalling / inertia_in);
output = (Vector2.zero - last_velocity) * (current_delta_notCall / properties.inertia_out);
return output;
}
protected Vector2 Clamp(Vector2 vector, Vector2 max)
{
float max_magnitude = max.magnitude;
float currentMagnitude = vector.magnitude;
float dot = Vector2.Dot(vector, max);
Vector2 output = vector;
//check if it is calling to clamp
if (currentMagnitude > max_magnitude && dot > 0.0f && current_delta_call != 0.0f)
{
output = max;
}
//checl if is not calling to clamp to zero
else if (dot <= 0.0f && current_delta_call == 0.0f)
{
output = Vector2.zero;
}
return output;
}
protected void EndStep()
{
last_step_time = Time.time;
last_velocity = current_output;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 57b436089801943408c989af783a9a85
timeCreated: 1455409843
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
using UnityEngine;
using System.Collections.Generic;
namespace Forces
{
/// <summary>
/// Controls forces to be esay for the user to use
/// </summary>
public class ForceController : System.Object
{
protected Dictionary<string, Force> active_forces = new Dictionary<string, Force>();
protected List<Force2Call> call_forces = new List<Force2Call>();
protected List<string> null_forces_index = new List<string>();
protected Vector2 lastFinalVel;
public void AddForce(ForceProps forceProps, Vector3 direction)
{
Force target = Target(forceProps, direction);
call_forces.Add(new Force2Call(direction, target));
}
public void DestroyForce (ForceProps forceProps)
{
Force f = GetForce(forceProps);
if (f == null) return;
f.Kill();
null_forces_index.Add(forceProps.Name);
}
public Force GetForce(ForceProps force)
{
if (active_forces.ContainsKey(force.Name))
return active_forces[force.Name];
return null;
}
public Vector2 Update()
{
Vector2 final_velocity = Vector2.zero;
///ADD AND CALL STEP
foreach (Force2Call f in call_forces)
{
f.force.Call(f.direction);
}
///APPLY STEP
foreach (string key in active_forces.Keys)
{
Force f = active_forces[key];
///Add to null forces if the force isnt using
if (!f.Using())
{
null_forces_index.Add(f.properties.Name);
continue;
}
Vector2 add = f.Velocity();
final_velocity += add;
}
///CLEAR STEP
foreach (string i in null_forces_index)
{
active_forces.Remove(i);
}
call_forces.Clear();
null_forces_index.Clear();
Vector2 final = final_velocity * Time.deltaTime * 100.0f;
Vector3 out_ = final - lastFinalVel;
lastFinalVel = final;
return out_;
}
protected Force Target(ForceProps force, Vector3 direction)
{
Force out_ = GetForce(force);
if (out_ == null)
{
out_ = new Force(force, direction);
active_forces.Add(out_.properties.Name, out_);
}
return out_;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 114545d266452cb4b96fd2ef86902650
timeCreated: 1456794068
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
using UnityEngine;
using System.Collections;
namespace Forces
{
public enum ForceMode { Force, Impulse, Aceleration }
/// <summary>
/// Label force static information
/// </summary>
[System.Serializable]
public struct ForceProps
{
/// <summary>
/// indetify instance id
/// </summary>
public string Name;
/// <summary>
/// inertia value when starts in seconds (fade in)
/// </summary>
public float inertia_in;
/// <summary>
/// inertia value when ends in seconds (fade out)
/// </summary>
public float inertia_out;
/// <summary>
/// time calling without Update
/// </summary>
public float impulse;
/// <summary>
/// goal speed
/// </summary>
public float target_speed;
/// <summary>
/// mode of applying this force
/// </summary>
public ForceMode forceMode;
public ForceProps(string Name, float target_speed, float inertia_in = 1.0f, float inertia_out = 1.0f, float impulse = 0.0f)
{
this.Name = Name;
this.target_speed = target_speed;
this.inertia_in = inertia_in;
this.inertia_out = inertia_out;
this.impulse = impulse;
this.forceMode = impulse > 0.0f ? ForceMode.Impulse : ForceMode.Force;
}
}
/// <summary>
/// Target force and new vector to apply
/// </summary>
public struct Force2Call
{
public Vector3 direction;
public Force force;
public Force2Call(Vector3 direction, Force force)
{
this.direction = direction;
this.force = force;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2f6cb937bd9326148b7ab27db4d1d775
timeCreated: 1456794126
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: