*
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2830449a1f4f7e54184f0ba1e4b62181
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiWaypoint : MonoBehaviour {
|
||||
public float radius = 0.4f;
|
||||
public Color color = new Color(0.2f, 0.2f, 1, 0.6f);
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
AdvancedGizmo.DrawCircle(transform.position, Vector3.forward, radius, color);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11879864f36973b4b9097cbfd448b2db
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: fc00ec05a89da4ff695a4273715cd5ce, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d18f032676b70b845b5d1f39535e9f6b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public enum BehaviorStatus {
|
||||
Invalid,
|
||||
Success,
|
||||
Fail,
|
||||
Running
|
||||
};
|
||||
|
||||
public abstract class AiBehavior : MonoBehaviour {
|
||||
public string behaviorName;
|
||||
|
||||
protected virtual void OnBegin(AiBlackboard blackboard) { }
|
||||
protected virtual void OnEnd(AiBlackboard blackboard) { }
|
||||
protected abstract BehaviorStatus OnExecute(AiBlackboard blackboard);
|
||||
private BehaviorStatus _state = BehaviorStatus.Invalid;
|
||||
public BehaviorStatus State {
|
||||
get { return _state; }
|
||||
}
|
||||
|
||||
public bool Running {
|
||||
get { return _state == BehaviorStatus.Running; }
|
||||
}
|
||||
|
||||
public BehaviorStatus Tick(AiBlackboard blackboard) {
|
||||
if (_state != BehaviorStatus.Running) {
|
||||
OnBegin(blackboard);
|
||||
}
|
||||
|
||||
_state = OnExecute(blackboard);
|
||||
if (_state == BehaviorStatus.Running) {
|
||||
_state = BehaviorStatus.Success;
|
||||
}
|
||||
|
||||
if (_state != BehaviorStatus.Running) {
|
||||
OnEnd(blackboard);
|
||||
}
|
||||
|
||||
return _state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac3489aca4eaca1438c296121ea6606f
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,47 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class BlackboardVariables {
|
||||
public static string LastEnemyPosition = "lastEnemyPosition";
|
||||
public static string LastSound = "lastSound";
|
||||
public static string LastSuspiciousTime = "lastSuspTime";
|
||||
public static string Enemy = "enemy";
|
||||
}
|
||||
|
||||
|
||||
public class AiBlackboard : MonoBehaviour {
|
||||
private Dictionary<string, object> _values = new Dictionary<string, object>();
|
||||
|
||||
public GameObject Obj {
|
||||
get {
|
||||
return (GameObject)_values["gameobject"];
|
||||
}
|
||||
}
|
||||
|
||||
public void SetValue(string key, object val) {
|
||||
_values[key] = val;
|
||||
}
|
||||
|
||||
/*public bool GetValue(string key, out object val) {
|
||||
if (_values.ContainsKey(key)) {
|
||||
val = _values[key];
|
||||
return true;
|
||||
}
|
||||
val = null;
|
||||
return false;
|
||||
}*/
|
||||
|
||||
public bool GetValue<T>(string key, out T obj) {
|
||||
if (_values.ContainsKey(key)) {
|
||||
obj = (T)_values[key];
|
||||
return true;
|
||||
}
|
||||
obj = default(T);
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool RemoveValue(string key) {
|
||||
return _values.Remove(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b64878cdefd0e5b409d5bb0cbd88f2b0
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiBlackboardDecorator : MonoBehaviour {
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8fda99ed7265d9447a016da7ea552c0a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public abstract class AiComposite : AiBehavior {
|
||||
public AiBehavior[] children;
|
||||
|
||||
public void Add(AiBehavior behavior) {
|
||||
if (children == null || children.Length == 0) {
|
||||
children = new AiBehavior[1];
|
||||
children[0] = behavior;
|
||||
}
|
||||
else {
|
||||
Array.Resize(ref children, children.Length + 1);
|
||||
children[children.Length - 1] = behavior;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ff17649812f35546970a12bf331b117
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,6 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public abstract class AiDecorator : AiBehavior {
|
||||
public AiBehavior child;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5bf7cefe7649b64aa3bfba8a2240871
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiInverter : AiDecorator {
|
||||
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var result = child.Tick(blackboard);
|
||||
if (result == BehaviorStatus.Success) {
|
||||
return BehaviorStatus.Fail;
|
||||
} else if (result == BehaviorStatus.Fail) {
|
||||
return BehaviorStatus.Success;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 393988bfa4e415c498546e68a6e1ff46
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 50453c57d72f4764f91cac3e9da6775d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public enum AiParallelPolicy {
|
||||
Any,
|
||||
All
|
||||
}
|
||||
|
||||
public class AiParallel : AiComposite {
|
||||
public AiParallelPolicy successPolicy = AiParallelPolicy.Any;
|
||||
public AiParallelPolicy failurePolicy = AiParallelPolicy.All;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
int failCount = 0;
|
||||
int successCount = 0;
|
||||
foreach (var behavior in children) {
|
||||
//if (behavior.Running) {
|
||||
behavior.Tick(blackboard);
|
||||
//}
|
||||
|
||||
if (behavior.State == BehaviorStatus.Success) {
|
||||
++successCount;
|
||||
if (successPolicy == AiParallelPolicy.Any) {
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
else if (behavior.State == BehaviorStatus.Fail) {
|
||||
++failCount;
|
||||
if (failurePolicy == AiParallelPolicy.Any) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failurePolicy == AiParallelPolicy.All && failCount == children.Length) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
if (successPolicy == AiParallelPolicy.All && successCount == children.Length) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
return BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e69523706cb2fa24d94ca0a2ba3cfc83
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: c9ec39ec3da62a64d941014c71e7f028, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiRepeatDecorator : AiDecorator {
|
||||
public int count = 1;
|
||||
private int _counter;
|
||||
public bool repeatForever;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
base.OnBegin(blackboard);
|
||||
_counter = 0;
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var state = child.Tick(blackboard);
|
||||
if (state == BehaviorStatus.Success) {
|
||||
if (repeatForever) {
|
||||
return BehaviorStatus.Running;
|
||||
}
|
||||
else {
|
||||
_counter++;
|
||||
if (_counter == count) {
|
||||
return BehaviorStatus.Success;
|
||||
} else {
|
||||
return BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a02e2a8ad2091bd48b7acd45fff4b3c6
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d5935485dc5991449b53931f8c2e312, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiReturnFailure : AiDecorator {
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return (childStatus == BehaviorStatus.Fail || childStatus == BehaviorStatus.Success)
|
||||
? BehaviorStatus.Fail
|
||||
: childStatus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d998bba05cd11ab4087e0ddf80721c8a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: b3ecdd4f2d60e5149ba02592143f517b, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiReturnSuccess : AiDecorator {
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return (childStatus == BehaviorStatus.Fail || childStatus == BehaviorStatus.Success)
|
||||
? BehaviorStatus.Success
|
||||
: childStatus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94bfc6ef33c4fa74dbad429a88d561d9
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 72f177c027d95ac438d85c3a968f00e3, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiRoot : AiBehavior {
|
||||
public AiBehavior child;
|
||||
public AiBlackboard blackboard;
|
||||
private Health _health;
|
||||
|
||||
void Start() {
|
||||
blackboard.SetValue("gameobject", gameObject);
|
||||
_health = GetComponent<Health>();
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
OnExecute(blackboard);
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (enabled && (_health == null || !_health.Dead)) {
|
||||
if (child == null) {
|
||||
Debug.LogWarning("AiRoot: child is null");
|
||||
return BehaviorStatus.Invalid;
|
||||
}
|
||||
return child.Tick(blackboard);
|
||||
}
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e18ffee70d70d64a8dd9750b2751d89
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: ded096a5301fa644e8891d1f6101b751, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiSelector : AiComposite {
|
||||
|
||||
private int _currentId = 0;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
_currentId = 0;
|
||||
}
|
||||
|
||||
protected override void OnEnd(AiBlackboard blackboard) {
|
||||
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (children == null || children.Length == 0) {
|
||||
Debug.LogError("Broken behavior! No children!");
|
||||
return BehaviorStatus.Invalid;
|
||||
}
|
||||
|
||||
while(_currentId < children.Length) {
|
||||
var current = children[_currentId];
|
||||
var result = current.Tick(blackboard);
|
||||
if (result != BehaviorStatus.Fail) {
|
||||
return result;
|
||||
}
|
||||
_currentId++;
|
||||
}
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5804a06425c3ad4a8d15d809813993a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: bcabd2f1888c8f14ca23cb37f12fefb2, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiSequence : AiComposite {
|
||||
|
||||
private int _currentId = 0;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
_currentId = 0;
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (children == null || children.Length == 0) {
|
||||
Debug.LogError("Broken behavior! No children!");
|
||||
return BehaviorStatus.Invalid;
|
||||
}
|
||||
|
||||
//Debug.Log("Sequence: " + behaviorName);
|
||||
|
||||
while(_currentId < children.Length) {
|
||||
var current = children[_currentId];
|
||||
var result = current.Tick(blackboard);
|
||||
if (result != BehaviorStatus.Success) {
|
||||
return result;
|
||||
}
|
||||
_currentId++;
|
||||
}
|
||||
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbc50ac62b293274197389809d6f729c
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 8acfd1a7d74af514c9cfa3c24b4744a8, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiUntilFailure : AiDecorator {
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return childStatus == BehaviorStatus.Fail ? BehaviorStatus.Success : BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3d664e3d1f23ea647bbf57eb52207ad1
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6f3711f476005504c9f518040eab635a, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiUntilSuccess : AiDecorator {
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return childStatus == BehaviorStatus.Success ? BehaviorStatus.Success : BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7156873ba7e0ded4282c752529bc401b
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: bbde1a7d7d1792a46937e629e84df4e0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 091298dbbffdc354dacc39cb6f2fc17f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,19 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiAim : AiBehavior {
|
||||
|
||||
private GameObject obj;
|
||||
private HumanController selfController;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (obj == null) {
|
||||
obj = blackboard.Obj;
|
||||
selfController = obj.GetComponent<HumanController>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
return selfController.PrepareItem() ? BehaviorStatus.Success : BehaviorStatus.Fail;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 703aed8d891f45d4a891c457267df9cb
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiCanHitEnemy : AiBehavior {
|
||||
|
||||
private GameObject obj;
|
||||
private Inventory inventory;
|
||||
private HumanController selfController;
|
||||
public LayerMask skipRaycast;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (obj == null) {
|
||||
blackboard.GetValue<GameObject>("gameobject", out obj);
|
||||
inventory = obj.GetComponent<Inventory>();
|
||||
selfController = obj.GetComponent<HumanController>();
|
||||
}
|
||||
|
||||
PlayerController enemy;
|
||||
blackboard.GetValue("enemy", out enemy);
|
||||
if (enemy == null) {
|
||||
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
Vector3 right = obj.transform.localToWorldMatrix.MultiplyVector(Vector3.right).normalized;
|
||||
var raycastDir = Mathf.Abs(enemy.transform.position.x - obj.transform.position.x) * right;
|
||||
|
||||
var p0 = obj.transform.position + Vector3.up * selfController.ArmsStandOffset;
|
||||
var p1 = obj.transform.position + Vector3.up * selfController.ArmsCrouchOffset;
|
||||
|
||||
string crouchingName = "crouchToShoot";
|
||||
if ( RaycastEnemy(p0, raycastDir, enemy)) {
|
||||
blackboard.SetValue(crouchingName, false);
|
||||
Debug.Log("WTF1");
|
||||
return BehaviorStatus.Success;
|
||||
} else if (RaycastEnemy(p1, raycastDir, enemy)) {
|
||||
Debug.Log("WTF2");
|
||||
blackboard.SetValue(crouchingName, true);
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
|
||||
|
||||
return BehaviorStatus.Fail;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private bool RaycastEnemy(Vector3 from, Vector3 d, PlayerController enemy) {
|
||||
var hits = Physics2D.RaycastAll(from, d, 100, Physics2D.DefaultRaycastLayers & ~skipRaycast.value);
|
||||
|
||||
Debug.Log("Hits: " + hits.Length);
|
||||
//find first collision which is not inside player head
|
||||
int firstOutsideId = -1;
|
||||
for (int i = 0; i < hits.Length; ++i) {
|
||||
if (hits[i].fraction > 0.0f) {
|
||||
firstOutsideId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log("Hits name: " + hits[firstOutsideId].transform.name);
|
||||
if (firstOutsideId >= 0) {
|
||||
var hit = hits[firstOutsideId];
|
||||
var g = hit.collider.GetComponent<PlayerController>();
|
||||
if (g != null && g == enemy) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e63b0fdce85d3e24dafa3251136a3f0c
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiCancelAim : AiBehavior {
|
||||
|
||||
private GameObject obj;
|
||||
private HumanController selfController;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (obj == null) {
|
||||
obj = blackboard.Obj;
|
||||
selfController = obj.GetComponent<HumanController>();
|
||||
}
|
||||
|
||||
if (!selfController.GunArmed) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
selfController.UnprepareItem();
|
||||
selfController.Crouch = false;
|
||||
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9f8f06b1ce7868419b3244bd9bd7b07
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiChase : AiBehavior {
|
||||
public float chaseTimeout = 5;
|
||||
private GameObject _obj;
|
||||
private HumanController _selfController;
|
||||
private bool _chasing;
|
||||
public RandomSoundPlayer endChasingSound;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
base.OnBegin(blackboard);
|
||||
if (_obj == null) {
|
||||
_obj = blackboard.Obj;
|
||||
_selfController = _obj.GetComponent<HumanController>();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEndChasing() {
|
||||
Debug.Log("Begin chasing");
|
||||
if (endChasingSound != null) {
|
||||
endChasingSound.Play();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBeginChasing() {
|
||||
Debug.Log("End chasing");
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
float time;
|
||||
if (!blackboard.GetValue(BlackboardVariables.LastSuspiciousTime, out time)) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
//check if chasing too long
|
||||
if (Time.time - time >= chaseTimeout) {
|
||||
if (_chasing) {
|
||||
_chasing = false;
|
||||
OnEndChasing();
|
||||
}
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//do chase!
|
||||
if (!_chasing) {
|
||||
_chasing = true;
|
||||
OnBeginChasing();
|
||||
}
|
||||
|
||||
|
||||
Vector3 chaseTarget;
|
||||
if (blackboard.GetValue(BlackboardVariables.LastEnemyPosition, out chaseTarget)) {
|
||||
if (_selfController != null) {
|
||||
float dir = Mathf.Sign(chaseTarget.x - _obj.transform.position.x);
|
||||
|
||||
if (Mathf.Abs(chaseTarget.x - _obj.transform.position.x) > 0.3f) {
|
||||
_selfController.UpdateMovement(dir * _selfController.maxWalkSpeed);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f00beff876f03384e8d2938e1e37b74f
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiDelay : AiBehavior {
|
||||
|
||||
public float delay = 1;
|
||||
private float _startTime;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
_startTime = Time.time;
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
return (_startTime + delay) > Time.time ? BehaviorStatus.Running : BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f55a86bc81f2e5447b675e80b521b328
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 75cff1be3576d61429363834ca95e010, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiEquipGun : AiBehavior {
|
||||
|
||||
private GameObject obj;
|
||||
private Inventory inventory;
|
||||
private HumanController selfController;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (obj == null) {
|
||||
blackboard.GetValue<GameObject>("gameobject", out obj);
|
||||
inventory = obj.GetComponent<Inventory>();
|
||||
selfController = obj.GetComponent<HumanController>();
|
||||
}
|
||||
|
||||
if (selfController.GetSocket(ItemSocketType.Hands).AttachedItem == null) {
|
||||
|
||||
var weapon = inventory.QueryItem(null, "Gun", null);
|
||||
if (weapon != null) {
|
||||
//Debug.LogFormat("Character {0} equipped weapon {1}", obj.name, weapon.description.Name);
|
||||
inventory.EquipItem(weapon);
|
||||
}
|
||||
else {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
}
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ad8bdf70f1755c44b33fb4286544f22
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,75 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
|
||||
public class AiFindEnemy : AiBehavior {
|
||||
|
||||
private GameObject obj;
|
||||
private PlayerSensor sensor;
|
||||
private HumanController selfController;
|
||||
public LayerMask skipMask;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
base.OnBegin(blackboard);
|
||||
if (obj == null) {
|
||||
obj = blackboard.Obj;
|
||||
sensor = obj.GetComponent<HumanoidAI>().viewSensor;
|
||||
selfController = obj.GetComponent<HumanController>();
|
||||
}
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
|
||||
bool playerInCone = sensor.Player != null;
|
||||
if (playerInCone) {
|
||||
var enemyEyes = sensor.Player.eyes.transform.position;
|
||||
var selfEyes = selfController.eyes.transform.position;
|
||||
|
||||
var from = new Vector3[] { selfEyes, selfEyes };
|
||||
var to = new Vector3[] { enemyEyes, sensor.Player.transform.position };
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
Debug.DrawLine(from[i], to[i], Color.gray);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
if (RaycastEnemy(from[i], to[i])) {
|
||||
blackboard.SetValue(BlackboardVariables.Enemy, sensor.Player);
|
||||
blackboard.SetValue(BlackboardVariables.LastEnemyPosition, sensor.Player.transform.position);
|
||||
|
||||
//Debug.Log("Raycast OK");
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
// Debug.Log("Raycast FAIL");
|
||||
}
|
||||
|
||||
blackboard.SetValue(BlackboardVariables.Enemy, null);
|
||||
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
private bool RaycastEnemy(Vector3 from, Vector3 to) {
|
||||
var hits = Physics2D.RaycastAll(from, (to - from).normalized, 100, (Physics2D.DefaultRaycastLayers & (~skipMask.value)));
|
||||
|
||||
//find first collision which is not inside player head
|
||||
int firstOutsideId = -1;
|
||||
for (int i = 0; i < hits.Length; ++i) {
|
||||
if (hits[i].fraction > 0.0f) {
|
||||
firstOutsideId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstOutsideId >= 0) {
|
||||
var hit = hits[firstOutsideId];
|
||||
var g = hit.collider.GetComponent<PlayerController>();
|
||||
if (g != null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a8a0b982bd08a5488a7c4999040b3e2
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiHitDetector : MonoBehaviour {
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ff1b2e8d41c5ab46bfc53c0c3cb1252
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiListenSounds : AiBehavior {
|
||||
private AiSoundListener _listener;
|
||||
private GameObject _obj;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
base.OnBegin(blackboard);
|
||||
|
||||
if (_obj == null) {
|
||||
_obj = blackboard.Obj;
|
||||
_listener = _obj.GetComponent<AiSoundListener>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var sound = _listener.LatestSound;
|
||||
blackboard.SetValue(BlackboardVariables.LastSound, sound);
|
||||
if (sound != null) {
|
||||
blackboard.SetValue(BlackboardVariables.LastEnemyPosition, sound.transform.position);
|
||||
blackboard.SetValue(BlackboardVariables.LastSuspiciousTime, Time.time);
|
||||
|
||||
}
|
||||
|
||||
return sound == null ? BehaviorStatus.Fail : BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e1f1556b659a8f458973a7c12bdc793
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,37 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiPatrol : AiBehavior {
|
||||
|
||||
public AiWaypoint[] patrolPoints;
|
||||
private int _targetPoint;
|
||||
private GameObject targetObject;
|
||||
private HumanController controller;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
base.OnBegin(blackboard);
|
||||
//_targetPoint = 0;
|
||||
blackboard.GetValue("gameobject", out targetObject);
|
||||
controller = targetObject.GetComponent<HumanController>();
|
||||
}
|
||||
|
||||
private void NextPoint() {
|
||||
_targetPoint = (_targetPoint + 1)%patrolPoints.Length;
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var point = patrolPoints[_targetPoint];
|
||||
var dir = (Vector2) point.transform.position - (Vector2) controller.transform.position;
|
||||
|
||||
if (controller.GetComponent<Health>().Dead) {
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
|
||||
if (Mathf.Abs(dir.x) < 0.1f) {
|
||||
NextPoint();
|
||||
} else {
|
||||
controller.UpdateMovement(Mathf.Sign(dir.x) * controller.maxWalkSpeed);
|
||||
}
|
||||
return BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0689ea7abd9f480449a528cc05fb0c0c
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiSaySomethingOnSound : AiBehavior {
|
||||
public RandomSoundPlayer player;
|
||||
private AiSound _lastSound;
|
||||
private float _lastSloundTime;
|
||||
public float minDelay = 3;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
AiSound sound;
|
||||
blackboard.GetValue(BlackboardVariables.LastSound, out sound);
|
||||
if (sound == null) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
if (Time.time - _lastSloundTime >= minDelay) {
|
||||
player.Play();
|
||||
_lastSloundTime = Time.time;
|
||||
Debug.Log("Saying!");
|
||||
}
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 05fb3d4c5b2994e48a8d5836b3d82b3f
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiUseItem : AiBehavior {
|
||||
private GameObject obj;
|
||||
private HumanController selfController;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (obj == null) {
|
||||
obj = blackboard.Obj;
|
||||
selfController = obj.GetComponent<HumanController>();
|
||||
}
|
||||
|
||||
var item = selfController.GetSocket(ItemSocketType.Hands).AttachedItem;
|
||||
if (item == null) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
bool shouldCrouch = false;
|
||||
blackboard.GetValue<bool>("crouchToShoot", out shouldCrouch);
|
||||
|
||||
selfController.Crouch = shouldCrouch;
|
||||
|
||||
|
||||
item.OnBeginUse();
|
||||
item.OnEndUse();
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e969b77faa0f304abbd0a3faa55a3f2
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,69 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class GameAction{
|
||||
public GameAction(){
|
||||
|
||||
}
|
||||
public GameAction(GameObject obj, string description){
|
||||
target = obj;
|
||||
text = description;
|
||||
}
|
||||
public GameObject target;
|
||||
public string text;
|
||||
public void Execute(GameObject caller){
|
||||
Debug.Log("Action on target: " + target.name);
|
||||
target.SendMessage("OnAction", new ActionArgs(this, caller), SendMessageOptions.DontRequireReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
public class ActionArgs{
|
||||
public GameAction Action;
|
||||
public GameObject Caller;
|
||||
|
||||
public ActionArgs(GameAction action, GameObject caller){
|
||||
Action = action;
|
||||
Caller = caller;
|
||||
}
|
||||
}
|
||||
|
||||
public class ActionController : MonoBehaviour {
|
||||
|
||||
public KeyCode actionKey = KeyCode.F;
|
||||
private static List<GameAction> actions = new List<GameAction>();
|
||||
public HudController hud;
|
||||
public GameObject player;
|
||||
public static AudioSource audio;
|
||||
|
||||
public static void AddAction(GameAction act){
|
||||
actions.Add(act);
|
||||
}
|
||||
|
||||
public static void RemoveAction(GameAction act){
|
||||
actions.Remove(act);
|
||||
}
|
||||
|
||||
void Start () {
|
||||
audio = GetComponent<AudioSource>();
|
||||
}
|
||||
|
||||
void OnGUI(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
if(actions != null && actions.Count > 0){
|
||||
hud.SetAction(string.Format("Press <color=#00ff00ff>{0}</color> to <color=#ffff00ff>{1}</color>", actionKey.ToString(), actions[0].text));
|
||||
if(Input.GetKeyDown(actionKey)){
|
||||
|
||||
actions[0].Execute(player);
|
||||
}
|
||||
}else{
|
||||
hud.SetAction("");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78f1e34cb17588640bbe6012299c7e37
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.Design;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class ActivateGameObject : MonoBehaviour {
|
||||
public Predicate activatePredicate;
|
||||
private bool _active = false;
|
||||
public GameObject target;
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if (!_active && activatePredicate.Check()) {
|
||||
_active = true;
|
||||
if (target != null) {
|
||||
target.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2161a48311a90e44ab40c2267eb901a9
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AddForce : MonoBehaviour {
|
||||
public GameObject[] targets;
|
||||
public float force = 10;
|
||||
public float minTorque;
|
||||
public float maxTorque;
|
||||
|
||||
void Start () {
|
||||
foreach (var target in targets) {
|
||||
var rb = target.GetComponent<Rigidbody2D>();
|
||||
rb.AddTorque(Random.Range(minTorque, maxTorque));
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1b63864005c3da842a7c4066ad278466
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,140 @@
|
||||
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor.VersionControl;
|
||||
using UnityEditorInternal;
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
|
||||
public class AdvancedGizmo {
|
||||
public static void DrawCircle(Vector3 center, Vector3 normal, float radius, Color color, int tesselation = 12, bool drawNormal = false, int diagonalCount = 0){
|
||||
if (tesselation < 3) {
|
||||
Debug.LogError("Invalid tesselation value! Should be >= 3");
|
||||
}
|
||||
Vector3 right = Vector3.right;
|
||||
if (Mathf.Abs (Vector3.Dot (right, normal)) > 0.999f) {
|
||||
right = Vector3.up;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Vector3 front = Vector3.Cross (right, normal).normalized;
|
||||
|
||||
Gizmos.color = color;
|
||||
|
||||
for(int i = 0; i < tesselation; ++i){
|
||||
//Gizmos.DrawLine(
|
||||
Gizmos.DrawLine(
|
||||
center + Quaternion.AngleAxis((360.0f / (float)tesselation) * (float)(i + 0), normal) * front * radius,
|
||||
center + Quaternion.AngleAxis((360.0f / (float)tesselation) * (float)(i + 1), normal) * front * radius
|
||||
);
|
||||
}
|
||||
|
||||
for (int i = 0; i < diagonalCount; ++i) {
|
||||
float a1 = (180.0f / (float)diagonalCount) * (float)(i);
|
||||
float a2 = a1 + 180.0f;
|
||||
|
||||
Gizmos.DrawLine(
|
||||
center + Quaternion.AngleAxis(a1, normal) * front * radius,
|
||||
center + Quaternion.AngleAxis(a2, normal) * front * radius
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawGrid(Vector3 center, float width, float height, int wSections = 1, int hSections = 1){
|
||||
Vector3 topLeft = new Vector3 (-width * 0.5f, 0, height * 0.5f);
|
||||
Vector3 wStep = new Vector3 (width / (float)wSections, 0, 0);
|
||||
Vector3 hStep = new Vector3 (0, 0, -height / (float)hSections);
|
||||
|
||||
|
||||
//draw horizontal lines
|
||||
for (int i = 0; i <= hSections; ++i) {
|
||||
Vector3 p1 = topLeft + hStep * (float)i + center;
|
||||
Debug.DrawLine(p1, p1 + new Vector3(width, 0, 0));
|
||||
}
|
||||
|
||||
//draw vertical lines
|
||||
for (int i = 0; i <= hSections; ++i) {
|
||||
Vector3 p1 = topLeft + wStep * (float)i + center;
|
||||
Debug.DrawLine(p1, p1 + new Vector3(0, 0, -height));
|
||||
}
|
||||
}
|
||||
|
||||
public delegate float fx(float x);
|
||||
public static void Plot2D(fx func, Matrix4x4 tranfsorm, Vector3 center, Vector3 xAxis, Vector3 yAxis, float xLen, float xScale, float yScale, int sampleCount, Color color){
|
||||
float xStep = xLen / (float)(sampleCount - 1);
|
||||
Vector3[] points = new Vector3[sampleCount];
|
||||
|
||||
for (int i = 0; i < sampleCount; ++i) {
|
||||
float x1 = (float)i * xStep;
|
||||
float arg1 = (x1 / xLen) * xScale;
|
||||
float y1 = func(arg1) * yScale;
|
||||
|
||||
points[i] = center + xAxis * x1 + yAxis * y1;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
Color backup = Handles.color;
|
||||
Handles.matrix = tranfsorm;
|
||||
Handles.color = color;
|
||||
Handles.DrawPolyLine(points);
|
||||
Handles.color = backup;
|
||||
Handles.matrix = Matrix4x4.identity;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void DrawMesh(Vector3[] vertices, int[] indices, bool drawNormals){
|
||||
for(int i = 0; i < indices.Length; i+=3){
|
||||
Vector3 v0 = vertices[indices[i + 0]];
|
||||
Vector3 v1 = vertices[indices[i + 1]];
|
||||
Vector3 v2 = vertices[indices[i + 2]];
|
||||
Debug.DrawLine(v0, v1);
|
||||
Debug.DrawLine(v1, v2);
|
||||
Debug.DrawLine(v2, v0);
|
||||
|
||||
if(drawNormals){
|
||||
Vector3 center = (v0 + v1 + v2) / 3.0f;
|
||||
Vector3 normal = Vector3.Cross((v1 - v0), (v2 - v0));
|
||||
Debug.DrawRay(center, normal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void DrawMesh(Vector3[] vertices, bool drawNormals){
|
||||
for(int i = 0; i < vertices.Length; i+=3){
|
||||
Vector3 v0 = vertices[i + 0];
|
||||
Vector3 v1 = vertices[i + 1];
|
||||
Vector3 v2 = vertices[i + 2];
|
||||
Debug.DrawLine(v0, v1);
|
||||
Debug.DrawLine(v1, v2);
|
||||
Debug.DrawLine(v2, v0);
|
||||
|
||||
if(drawNormals){
|
||||
Vector3 center = (v0 + v1 + v2) / 3.0f;
|
||||
Vector3 normal = Vector3.Cross((v1 - v0), (v2 - v0));
|
||||
Debug.DrawRay(center, normal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawTriangle(Vector3 v0, Vector3 v1, Vector3 v2, bool drawNormal){
|
||||
Debug.DrawLine(v0, v1);
|
||||
Debug.DrawLine(v1, v2);
|
||||
Debug.DrawLine(v2, v0);
|
||||
|
||||
if(drawNormal){
|
||||
Vector3 center = (v0 + v1 + v2) / 3.0f;
|
||||
Vector3 normal = Vector3.Cross((v1 - v0), (v2 - v0));
|
||||
Debug.DrawRay(center, normal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2e70bdd7e4a581d4cb371f1fa7b5b831
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,149 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class AdvancedMath {
|
||||
|
||||
public static float TriangleSquare(Vector3 va, Vector3 vb, Vector3 vc){
|
||||
float a = Vector3.Distance (va, vb);
|
||||
float b = Vector3.Distance (vb, vc);
|
||||
float c = Vector3.Distance (vc, va);
|
||||
float p = (a + b + c) * 0.5f;
|
||||
return Mathf.Sqrt (p * (p - a) * (p - b) * (p - c));
|
||||
}
|
||||
|
||||
|
||||
public static float CircleSegmentChordLength(float r, float angle) {
|
||||
return 2.0f*r*Mathf.Sin(angle*0.5f);
|
||||
}
|
||||
|
||||
public static float CircleSegmentHeight(float r, float angle) {
|
||||
return r * (1.0f - Mathf.Cos(angle * 0.5f));
|
||||
}
|
||||
|
||||
public static Vector3 PrismCenterOfMass(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 normal, float h0, float h1, float h2){
|
||||
|
||||
|
||||
Vector3 a = v0 + normal * (h0 * 0.5f);
|
||||
Vector3 c = v1 + normal * (h1 * 0.5f);
|
||||
Vector3 b = v2 + normal * (h2 * 0.5f);
|
||||
|
||||
Vector3 f = (a + b + c) / 3.0f;
|
||||
|
||||
return (h0 * ((3.0f * f + a) / 4.0f) + h1 * ((3.0f * f + c) / 4.0f) + h2 * ((3.0f * f + b) / 4.0f)) / (h0 + h1 + h2);
|
||||
}
|
||||
|
||||
public static float CylinderVolume(float r, float h){
|
||||
return Mathf.PI * (r * r) * h;
|
||||
}
|
||||
|
||||
public static Vector3 TriangleNormal(Vector3 va, Vector3 vb, Vector3 vc){
|
||||
Vector3 ba = vb - va;
|
||||
Vector3 ca = vc - va;
|
||||
return Vector3.Cross(ba, ca).normalized;
|
||||
}
|
||||
|
||||
public static Vector3 LineNearestPoint(Vector3 a, Vector3 b, Vector3 p){
|
||||
Vector3 n = (b - a).normalized;
|
||||
Vector3 pa = p - a;
|
||||
float d = Vector3.Dot (n, pa);
|
||||
return a + n * d;
|
||||
}
|
||||
|
||||
public static Vector2 LineNearestPoint(Vector2 a, Vector2 b, Vector2 p) {
|
||||
Vector2 n = (b - a).normalized;
|
||||
Vector2 pa = p - a;
|
||||
float d = Vector2.Dot(n, pa);
|
||||
return a + n * d;
|
||||
}
|
||||
|
||||
public static Vector2 SegmentNearestPoint(Vector2 a, Vector2 b, Vector2 p) {
|
||||
Vector2 n = (b - a);
|
||||
float dist = n.magnitude;
|
||||
n.Normalize();
|
||||
Vector2 pa = p - a;
|
||||
float d = Vector2.Dot(n, pa);
|
||||
d = Mathf.Clamp(d, 0, dist);
|
||||
return a + n * d;
|
||||
}
|
||||
|
||||
public static float LineDistance(Vector3 a, Vector3 b, Vector3 p){
|
||||
Vector3 lp = LineNearestPoint(a, b, p);
|
||||
return Vector3.Distance (lp, p);
|
||||
}
|
||||
|
||||
public static float Drag(float envDensity, float objVelocity, float dragCoeff, float referenceaArea){
|
||||
return (envDensity * (objVelocity * objVelocity) * dragCoeff * referenceaArea) * 0.5f;
|
||||
}
|
||||
|
||||
public static float SphereDrag(float envDensity, float objVelocity, float radius){
|
||||
return Drag (envDensity, objVelocity, 0.47f, Mathf.PI * (radius * radius));
|
||||
}
|
||||
|
||||
public static float BuoyantForce(float objectV, float objectDensity, float envDensity, float g){
|
||||
return objectV * envDensity * g * ((3.0f * objectDensity) / (envDensity + 2.0f * objectDensity));
|
||||
}
|
||||
public static float SphereSegmentVolume(float r, float h){
|
||||
return (Mathf.PI * (h * h) * (3.0f * r - h)) / 3.0f;
|
||||
}
|
||||
public static float SphereVolume(float r){
|
||||
return (4.0f * Mathf.PI * (r * r * r)) / 3.0f;
|
||||
}
|
||||
public static float SphereSurfaceArea(float r){
|
||||
return 4.0f * Mathf.PI * (r * r);
|
||||
}
|
||||
public static float PrismVolume(float baseArea, float h){
|
||||
return baseArea * h;
|
||||
}
|
||||
public static float ConeVolume(float r, float h){
|
||||
return (Mathf.PI * (r * r) * h) / 3.0f;
|
||||
}
|
||||
public static float PyramidVolume(float baseArea, float h){
|
||||
return (baseArea * h) / 3.0f;
|
||||
}
|
||||
public static float RightTriangleArea(float a, float b){
|
||||
return a * b * 0.5f;
|
||||
}
|
||||
public static float MeshVolume(Vector3[] srcVertices, int[] srcIndices, Matrix4x4 tf){
|
||||
Vector3[] vertices = new Vector3[srcVertices.Length];
|
||||
vertices[0] = tf.MultiplyPoint3x4(srcVertices[0]);
|
||||
Vector3 vMin = vertices [0];
|
||||
Vector3 vMax = vertices [0];
|
||||
for (int i = 1; i < vertices.Length; ++i) {
|
||||
vertices[i] = tf.MultiplyPoint3x4(srcVertices[i]);
|
||||
vMin = Vector3.Min(vMin, vertices[i]);
|
||||
vMax = Vector3.Max(vMax, vertices[i]);
|
||||
}
|
||||
|
||||
Plane p = new Plane (Vector3.up, vMax + Vector3.up);
|
||||
|
||||
//split mesh by plane
|
||||
List<Vector3> cuttedTriangles = AdvancedUtils.CutMesh (vertices, srcIndices, p);
|
||||
|
||||
float totalVolume = 0.0f;
|
||||
|
||||
for (int i = 0; i < cuttedTriangles.Count; i+=3) {
|
||||
Vector3 v0 = cuttedTriangles[i + 0];
|
||||
Vector3 v1 = cuttedTriangles[i + 1];
|
||||
Vector3 v2 = cuttedTriangles[i + 2];
|
||||
Vector3 normal = AdvancedMath.TriangleNormal(v0, v1, v2);
|
||||
|
||||
float sign = Mathf.Sign(Vector3.Dot (normal, p.normal));
|
||||
|
||||
float h0 = p.GetDistanceToPoint(v0);
|
||||
float h1 = p.GetDistanceToPoint(v1);
|
||||
float h2 = p.GetDistanceToPoint(v2);
|
||||
float avgH = (h0 + h1 + h2) / 3.0f;
|
||||
|
||||
Vector3 vp0 = v0 - p.normal * h0;
|
||||
Vector3 vp1 = v1 - p.normal * h1;
|
||||
Vector3 vp2 = v2 - p.normal * h2;
|
||||
|
||||
float triangleSquare = AdvancedMath.TriangleSquare(vp0, vp1, vp2);
|
||||
float prismVolume = AdvancedMath.PrismVolume(triangleSquare, avgH) * sign;
|
||||
totalVolume += prismVolume;
|
||||
}
|
||||
|
||||
return totalVolume;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 97f37310fbd73a04b95a7f1896dbc8ae
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,224 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
public static class AdvancedUtils {
|
||||
|
||||
public static GameObject CreateMeshObject(string name){
|
||||
GameObject result = new GameObject(name);
|
||||
MeshFilter meshFilter = (MeshFilter)result.AddComponent(typeof(MeshFilter));
|
||||
result.AddComponent(typeof(MeshRenderer));
|
||||
|
||||
Mesh m = new Mesh();
|
||||
m.name = result.name;
|
||||
meshFilter.sharedMesh = m;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static GameObject CreateMeshObject(){
|
||||
return CreateMeshObject ("Unnamed object");
|
||||
}
|
||||
|
||||
public static List<GameObject> GetChildren(this GameObject obj) {
|
||||
var tf = obj.GetComponent<Transform>();
|
||||
|
||||
var children = new List<GameObject>(tf.childCount);
|
||||
for (int i = 0; i < tf.childCount; ++i) {
|
||||
children.Add(tf.GetChild(i).gameObject);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void GenerateCylinder(float radius, float length, int sides, int heightSegments,float uTile, float vTile, out Vector3[] vertices, out int[] indices, out Vector2[] uvs, out Vector3[] normals){
|
||||
float yBottom = 0.0f;
|
||||
Vector3 up = Vector3.up;
|
||||
int ringVertexCount = (sides + 1);
|
||||
int ringCount = heightSegments + 1;
|
||||
int vertexCount = ringCount * ringVertexCount;
|
||||
vertices = new Vector3[vertexCount];
|
||||
uvs = new Vector2[vertices.Length];
|
||||
normals = new Vector3[vertices.Length];
|
||||
|
||||
//generate vertices
|
||||
for (int i = 0; i < ringVertexCount; ++i) {
|
||||
float angle = ((Mathf.PI * 2.0f) / (float)sides) * (float)(i);
|
||||
normals[i] = new Vector3(Mathf.Cos(angle), 0.0f, Mathf.Sin(angle));
|
||||
vertices[i] = new Vector3(normals[i].x * radius, yBottom, normals[i].z * radius);
|
||||
uvs[i] = new Vector2(((1.0f / (float)sides) * (float)i) * uTile, vTile);
|
||||
}
|
||||
|
||||
for (int i = 1; i < ringCount; ++i) {
|
||||
float ringY = (length / (float)heightSegments) * (float)i;
|
||||
int ringVertexOffset = ringVertexCount * i;
|
||||
for (int j = 0; j < ringVertexCount; ++j) {
|
||||
vertices[ringVertexOffset + j] = vertices[j];
|
||||
vertices[ringVertexOffset + j].y = ringY;
|
||||
uvs[ringVertexOffset + j] = new Vector2(uvs[j].x, (1.0f - ((1.0f / (float)heightSegments) * (float)i)) * vTile);
|
||||
normals[ringVertexOffset + j] = normals[j];
|
||||
}
|
||||
}
|
||||
|
||||
//generate indices
|
||||
int triangleCount = (2 * sides) * heightSegments;
|
||||
indices = new int[triangleCount * 3];
|
||||
int idOffset = 0;
|
||||
for (int i = 0; i < heightSegments; ++i) {
|
||||
int segmentVertexOffset = i * ringVertexCount;
|
||||
int nextSegmentVertexOffset = (i + 1) * ringVertexCount;
|
||||
|
||||
for (int j = 0; j < sides; ++j) {
|
||||
int sideOffset = segmentVertexOffset + j;
|
||||
|
||||
indices[idOffset + 0] = segmentVertexOffset + j;
|
||||
indices[idOffset + 2] = nextSegmentVertexOffset + j + 1;
|
||||
indices[idOffset + 1] = nextSegmentVertexOffset + j;
|
||||
|
||||
indices[idOffset + 3] = segmentVertexOffset + j;
|
||||
indices[idOffset + 5] = segmentVertexOffset + j + 1;
|
||||
indices[idOffset + 4] = nextSegmentVertexOffset + j + 1;
|
||||
|
||||
idOffset+=6;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] cubeIndices = new int[]{
|
||||
3,2,7,
|
||||
7,2,6,
|
||||
1,0,5,
|
||||
0,4,5,
|
||||
0,1,2,
|
||||
0,2,3,
|
||||
4,6,5,
|
||||
4,7,6,
|
||||
0,3,4,
|
||||
3,7,4,
|
||||
2,1,6,
|
||||
1,5,6
|
||||
};
|
||||
|
||||
public static Vector3[] cubeVertices = new[]{
|
||||
new Vector3(-0.5f, 0.5f, -0.5f),
|
||||
new Vector3(-0.5f, 0.5f, 0.5f),
|
||||
new Vector3(0.5f, 0.5f, 0.5f),
|
||||
new Vector3(0.5f, 0.5f, -0.5f),
|
||||
new Vector3(-0.5f, -0.5f, -0.5f),
|
||||
new Vector3(-0.5f, -0.5f, 0.5f),
|
||||
new Vector3(0.5f, -0.5f, 0.5f),
|
||||
new Vector3(0.5f, -0.5f, -0.5f),
|
||||
};
|
||||
|
||||
public static void CreateMeshCube(Matrix4x4 matrix, out Vector3[] vertices, out int[] indices){
|
||||
indices = new int[cubeIndices.Length];
|
||||
Array.Copy (cubeIndices, indices, cubeIndices.Length);
|
||||
|
||||
int vertexCount = cubeVertices.Length;
|
||||
vertices = new Vector3[vertexCount];
|
||||
for (int i = 0; i < vertexCount; ++i) {
|
||||
vertices[i] = matrix.MultiplyPoint3x4(cubeVertices[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static GameObject FindChild(GameObject parent, string childName){
|
||||
if (parent == null) {
|
||||
return null;
|
||||
}
|
||||
var tf = parent.transform.Find (childName);
|
||||
return tf == null ? null : tf.gameObject;
|
||||
}
|
||||
|
||||
public static List<Vector3> CutMesh(Vector3[] vertices, int[] indices, Plane p){
|
||||
List<Vector3> cuttedTriangles = new List<Vector3> ();
|
||||
|
||||
int cut = 0;
|
||||
int notCut = 0;
|
||||
|
||||
bool[] sideFlags = new bool[3];
|
||||
|
||||
for (int i = 0; i < indices.Length; i+=3) {
|
||||
int outside = 0;
|
||||
int lastOutsideId = 0;
|
||||
int lastInsideId = 0;
|
||||
for(int j = 0; j < 3; ++j){
|
||||
sideFlags[j] = p.GetSide(vertices[indices[i + j]]);
|
||||
if(sideFlags[j]){
|
||||
outside++;
|
||||
lastOutsideId = j;
|
||||
}else{
|
||||
lastInsideId = j;
|
||||
}
|
||||
}
|
||||
|
||||
if( outside ==3){
|
||||
notCut++;
|
||||
}else{
|
||||
cut++;
|
||||
}
|
||||
|
||||
switch(outside)
|
||||
{
|
||||
case 0:
|
||||
for(int j = 0; j < 3; ++j){
|
||||
cuttedTriangles.Add(vertices[indices[i + j]]);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
{
|
||||
Vector3 v0 = vertices[indices[i + lastOutsideId]];
|
||||
Vector3 v1 = vertices[indices[i + (lastOutsideId + 1) % 3]];
|
||||
Vector3 v2 = vertices[indices[i + (lastOutsideId + 2) % 3]];
|
||||
|
||||
float d0 = p.GetDistanceToPoint(v0);
|
||||
float d1 = p.GetDistanceToPoint(v1);
|
||||
float d2 = p.GetDistanceToPoint(v2);
|
||||
|
||||
float a = d0 / (d0 - d1);
|
||||
float b = d0 / (d0 - d2);
|
||||
|
||||
Vector3 v3 = v0 + (v1 - v0) * a;
|
||||
Vector3 v4 = v0 + (v2 - v0) * b;
|
||||
|
||||
|
||||
cuttedTriangles.Add(v2);
|
||||
cuttedTriangles.Add(v4);
|
||||
cuttedTriangles.Add(v3);
|
||||
|
||||
cuttedTriangles.Add(v2);
|
||||
cuttedTriangles.Add(v3);
|
||||
cuttedTriangles.Add(v1);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
Vector3 v0 = vertices[indices[i + lastInsideId]];
|
||||
Vector3 v1 = vertices[indices[i + (lastInsideId + 1) % 3]];
|
||||
Vector3 v2 = vertices[indices[i + (lastInsideId + 2) % 3]];
|
||||
|
||||
float d0 = p.GetDistanceToPoint(v0);
|
||||
float d1 = p.GetDistanceToPoint(v1);
|
||||
float d2 = p.GetDistanceToPoint(v2);
|
||||
|
||||
float a = d0 / (d0 - d1);
|
||||
float b = d0 / (d0 - d2);
|
||||
|
||||
Vector3 v3 = v0 + (v1 - v0) * a;
|
||||
Vector3 v4 = v0 + (v2 - v0) * b;
|
||||
|
||||
cuttedTriangles.Add(v0);
|
||||
cuttedTriangles.Add(v3);
|
||||
cuttedTriangles.Add(v4);
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return cuttedTriangles;
|
||||
}
|
||||
|
||||
/**/
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8cd8ee276a70096438bdfb17a8f9a607
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3fed1c662ae64c488842230c3376319
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
public class AIJob {
|
||||
public Action<HumanoidAI, AIJob> OnEnter;
|
||||
public Action<HumanoidAI, AIJob> OnExit;
|
||||
public Action<HumanoidAI, AIJob> OnUpdate;
|
||||
public Action<HumanoidAI, AIJob> OnFixedUpdate;
|
||||
public bool done;
|
||||
}
|
||||
|
||||
public class HumanoidAI : MonoBehaviour {
|
||||
public HumanController human;
|
||||
public float viewFov = 60;
|
||||
public float viewDistance = 2;
|
||||
public PlayerSensor viewSensor;
|
||||
|
||||
private Queue<AIJob> _actionQueue = new Queue<AIJob>();
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
|
||||
void OnValidate() {
|
||||
var pc = human.eyes.GetComponent<PolygonCollider2D>();
|
||||
if (pc == null) {
|
||||
pc = human.eyes.AddComponent<PolygonCollider2D>();
|
||||
|
||||
}
|
||||
|
||||
pc.isTrigger = true;
|
||||
int circleSegments = 5;
|
||||
|
||||
|
||||
var points = new Vector2[1 + (circleSegments + 1)];
|
||||
points[0] = new Vector2(0, 0);
|
||||
|
||||
for (int i = 0; i <= circleSegments; ++i) {
|
||||
float angle = viewFov/circleSegments*i;
|
||||
points[i + 1] = Quaternion.AngleAxis(-0.5f * viewFov + angle, Vector3.forward) * new Vector3(1, 0, 0) * viewDistance;
|
||||
}
|
||||
pc.points = points;
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
var pos = human.eyes.transform.position;
|
||||
var eyesTf = human.eyes.transform.localToWorldMatrix;
|
||||
var right = eyesTf.MultiplyVector(Vector3.right).normalized;
|
||||
|
||||
var d0 = Quaternion.AngleAxis(viewFov * 0.5f, Vector3.forward) * right;
|
||||
var d1 = Quaternion.AngleAxis(viewFov * -0.5f, Vector3.forward) * right;
|
||||
Gizmos.color = Color.blue;
|
||||
Gizmos.DrawLine(pos, pos + d0 * viewDistance);
|
||||
Gizmos.DrawLine(pos, pos + d1 * viewDistance);
|
||||
|
||||
int sections = 15;
|
||||
int innerSections = 5;
|
||||
for (int i = 0; i < sections; ++i) {
|
||||
|
||||
float angle0 = (viewFov / (float)sections * i) - (viewFov / 2);
|
||||
float angle1 = (viewFov / (float)sections * (i + 1)) - (viewFov / 2);
|
||||
|
||||
var p0 = Quaternion.AngleAxis(angle0, Vector3.forward) * right;
|
||||
var p1 = Quaternion.AngleAxis(angle1, Vector3.forward) * right;
|
||||
|
||||
|
||||
Gizmos.DrawLine(pos + p0 * viewDistance, pos + p1 * viewDistance);
|
||||
}
|
||||
|
||||
Gizmos.color = new Color(0,0,1,0.4f);
|
||||
for (int i = 0; i < sections; ++i) {
|
||||
|
||||
for (int j = 0; j < innerSections; ++j) {
|
||||
float dist = (viewDistance / (innerSections + 1)) * (j + 1);
|
||||
|
||||
float angle0 = (viewFov / (float)sections * i) - (viewFov / 2);
|
||||
float angle1 = (viewFov / (float)sections * (i + 1)) - (viewFov / 2);
|
||||
|
||||
var p0 = Quaternion.AngleAxis(angle0, Vector3.forward) * right;
|
||||
var p1 = Quaternion.AngleAxis(angle1, Vector3.forward) * right;
|
||||
|
||||
|
||||
Gizmos.DrawLine(pos + p0 * dist, pos + p1 * dist);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c78e7a6bf9dc52c429ebecae89101590
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class RotationAnimator : MonoBehaviour {
|
||||
public Vector3 Rotation;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
var rotation = transform.localRotation;
|
||||
rotation.eulerAngles += Rotation * Time.deltaTime;
|
||||
transform.localRotation = rotation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f6f17afb4739354fbe7dd27dd61b387
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AudioManager : MonoBehaviour {
|
||||
|
||||
public AudioSource inventorySource;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 938d56570e6193c4f95f39d6d8bef605
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
|
||||
public class AudioMixerController : MonoBehaviour {
|
||||
public AudioMixer ambientMixer;
|
||||
public string snapshotName = "Master";
|
||||
public float transitionTime = 1.0f;
|
||||
private PlayerSensor _sensor;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
_sensor = GetComponentInChildren<PlayerSensor>();
|
||||
_sensor.PlayerEnter += HandlePlayerEnter;
|
||||
}
|
||||
|
||||
void HandlePlayerEnter (PlayerSensor sensor, GameObject player)
|
||||
{
|
||||
var snap = ambientMixer.FindSnapshot(snapshotName);
|
||||
snap.TransitionTo(transitionTime);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b84623cdd36cf8448949e8dfa4f9071
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,50 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
[SelectionBase]
|
||||
public class BreakableObject : MonoBehaviour {
|
||||
public Health health;
|
||||
public RandomSoundPlayer player;
|
||||
public GameObject[] instantinateOnDestroy;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
if (health != null) {
|
||||
health.Destroyed += health_Destroyed;
|
||||
}
|
||||
}
|
||||
|
||||
void health_Destroyed() {
|
||||
Break();
|
||||
}
|
||||
|
||||
public void Break() {
|
||||
var thisRb = GetComponent<Rigidbody2D>();
|
||||
var children = gameObject.GetComponentsInChildren<BreakablePart>();
|
||||
foreach (var child in children) {
|
||||
var rb = child.gameObject.AddComponent<Rigidbody2D>();
|
||||
rb.mass = thisRb.mass/children.Count();
|
||||
child.transform.parent = null;
|
||||
}
|
||||
if (player != null) {
|
||||
player.Play(true);
|
||||
|
||||
}
|
||||
|
||||
if (instantinateOnDestroy != null && instantinateOnDestroy.Length > 0) {
|
||||
foreach (var o in instantinateOnDestroy) {
|
||||
var item = Instantiate(o);
|
||||
item.SetActive(true);
|
||||
item.transform.parent = null;
|
||||
}
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 512d83d6028109f48954f1ca49ede6df
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class BreakablePart : MonoBehaviour {
|
||||
|
||||
public float Mass = 1.0f;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 18841e63cecfe3c42a027f01f92d4f21
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,100 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public enum ButtonState{
|
||||
Error,
|
||||
Locked,
|
||||
Ready
|
||||
}
|
||||
|
||||
|
||||
public delegate void ButtonEventHandler();
|
||||
|
||||
public class Button : Trigger {
|
||||
public ButtonState state;
|
||||
public PlayerSensor sensor;
|
||||
private GameAction _pressAction;
|
||||
public bool interactive = true;
|
||||
public Predicate interactivePredicate;
|
||||
public event ButtonEventHandler Pressed;
|
||||
public bool locked = false;
|
||||
public Predicate lockedPredicate;
|
||||
public Predicate errorPredicate;
|
||||
public bool error;
|
||||
public string pressActionText = "Press the button";
|
||||
|
||||
protected virtual void OnPressed ()
|
||||
{
|
||||
var handler = Pressed;
|
||||
if (handler != null)
|
||||
handler ();
|
||||
}
|
||||
|
||||
void Start () {
|
||||
if(sensor != null){
|
||||
sensor.PlayerEnter += HandlePlayerEnter;
|
||||
sensor.PlayerExit += HandlePlayerExit;
|
||||
}else{
|
||||
Debug.LogWarningFormat("Button {0} sensor is null", name );
|
||||
}
|
||||
|
||||
_pressAction = new GameAction(gameObject, pressActionText);
|
||||
}
|
||||
|
||||
void HandlePlayerEnter (PlayerSensor sensor, GameObject player)
|
||||
{
|
||||
if(interactive){
|
||||
ActionController.AddAction(_pressAction);
|
||||
}
|
||||
}
|
||||
void HandlePlayerExit (PlayerSensor sensor, GameObject player)
|
||||
{
|
||||
ActionController.RemoveAction(_pressAction);
|
||||
}
|
||||
|
||||
void SetInteractive(bool val){
|
||||
if(interactive == val){
|
||||
return;
|
||||
}
|
||||
|
||||
if(val){
|
||||
if(sensor != null && sensor.Check()){
|
||||
ActionController.AddAction(_pressAction);
|
||||
}
|
||||
}else{
|
||||
ActionController.RemoveAction(_pressAction);
|
||||
}
|
||||
|
||||
interactive = val;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void OnAction(ActionArgs a){
|
||||
if(interactive){
|
||||
if(state == ButtonState.Ready){
|
||||
OnPressed();
|
||||
OnTrig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Update () {
|
||||
|
||||
if(interactivePredicate != null){
|
||||
SetInteractive(interactivePredicate.Check());
|
||||
}
|
||||
|
||||
if(errorPredicate.Check()){
|
||||
state = ButtonState.Error;
|
||||
}else if(lockedPredicate.Check()){
|
||||
state = ButtonState.Locked;
|
||||
}else{
|
||||
state = ButtonState.Ready;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c11b97f658532d47a170c6bfb1038ac
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,26 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class ButtonLedController : MonoBehaviour {
|
||||
public Led led;
|
||||
public Button button;
|
||||
public Color readyColor = Color.green;
|
||||
public Color lockedColor = Color.blue;
|
||||
public Color errorColor = Color.red;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if(button.state == ButtonState.Locked){
|
||||
led.color = lockedColor;
|
||||
}else if(button.state == ButtonState.Ready){
|
||||
led.color = readyColor;
|
||||
}else{
|
||||
led.color = errorColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f0d18d9e32ed85c4ebfba2ca0ca793b0
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ClosestPointDebug : MonoBehaviour {
|
||||
public Collider2D collider;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
if (enabled) {
|
||||
Vector2 pos = collider.offset + (Vector2)collider.transform.position;
|
||||
Vector3 p = new Vector3(pos.x, pos.y, 0);
|
||||
Gizmos.DrawLine(p, transform.position);
|
||||
|
||||
var closestPoint = collider.ClosestPointOnBounds(transform.position);
|
||||
AdvancedGizmo.DrawCircle(new Vector3(closestPoint.x, closestPoint.y, 0), Vector3.forward, 0.1f, Color.red, 48);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4535727b44d958d49858fb24d825b79b
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
public static class Matrix4x4Extension {
|
||||
public static Vector3 Scale(this Matrix4x4 m) {
|
||||
return new Vector3(((Vector3)m.GetColumn(0)).magnitude, ((Vector3)m.GetColumn(1)).magnitude, ((Vector3)m.GetColumn(2)).magnitude);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Collider2DExtension {
|
||||
|
||||
public static Vector2 ClosestPointOnBounds(this Collider2D collider, Vector2 point) {
|
||||
if (collider is CircleCollider2D) {
|
||||
var circle = collider as CircleCollider2D;
|
||||
var mat = circle.gameObject.transform.localToWorldMatrix;
|
||||
var scale = mat.Scale();
|
||||
var radiusScale = Mathf.Max(scale.x, scale.y);
|
||||
var radius = circle.radius*radiusScale;
|
||||
|
||||
var circlePos = (Vector2)circle.gameObject.transform.position + circle.offset;
|
||||
|
||||
return (point - circlePos).normalized * radius + (Vector2)collider.transform.position;
|
||||
|
||||
}else if (collider is BoxCollider2D) {
|
||||
var box = collider as BoxCollider2D;
|
||||
float hw = box.size.x * 0.5f;
|
||||
float hh = box.size.y * 0.5f;
|
||||
|
||||
Vector3[] corners = new Vector3[4] {
|
||||
new Vector3(-hw, hh, 0),
|
||||
new Vector3(hw, hh, 0),
|
||||
new Vector3(hw, -hh, 0),
|
||||
new Vector3(-hw, -hh, 0)
|
||||
};
|
||||
|
||||
Color[] c = new Color[4]{Color.red, Color.green, Color.blue, Color.white};
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
corners[i] = collider.transform.TransformPoint(corners[i]) + new Vector3(box.offset.x, box.offset.y, 0);
|
||||
corners[i].z = 0;
|
||||
}
|
||||
|
||||
Vector2[] points = new Vector2[4];
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
var p0 = corners[i];
|
||||
var p1 = corners[(i + 1) % 4];
|
||||
var pt = AdvancedMath.SegmentNearestPoint(p0, p1, (Vector3)point);
|
||||
points[i] = pt;
|
||||
}
|
||||
|
||||
return points.OrderBy(v => (point - v).magnitude).First();
|
||||
}
|
||||
return Vector3.zero;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 47ed87a0595b5064a971c8c266a6acf4
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class ComponentEnabler : MonoBehaviour {
|
||||
public Behaviour target;
|
||||
public Predicate enabledPredicate;
|
||||
public bool allowReenable = false;
|
||||
private bool _enabled = true;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if (!_enabled && !allowReenable) {
|
||||
return;
|
||||
}
|
||||
_enabled = enabledPredicate == null || enabledPredicate.Check();
|
||||
if (target != null) {
|
||||
target.enabled = _enabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c19fef4ffdf4dc044884190b63ef93bd
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,82 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class ContactDamage : MonoBehaviour {
|
||||
|
||||
public float minImpulse = 100.0f;
|
||||
public float maxImpulse = 300.0f;
|
||||
|
||||
public float maxDamage = 100.0f;
|
||||
private List<ContactDamage> _ignore = new List<ContactDamage>();
|
||||
|
||||
public void AddIgnoreObject(ContactDamage other) {
|
||||
_ignore.Add(other);
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
_ignore.Clear();
|
||||
}
|
||||
|
||||
void OnImpulse(float impulse, Vector2 point, GameObject impulseSource) {
|
||||
if (impulse >= minImpulse) {
|
||||
float k = 1.0f - (impulse - maxImpulse) / (minImpulse - maxImpulse);
|
||||
float damage = k * maxDamage;
|
||||
|
||||
var health = GetComponent<Health>();
|
||||
if (health != null) {
|
||||
health.ApplyDamage(damage, impulseSource, point);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnCollisionEnter2D(Collision2D col) {
|
||||
var otherHuman = col.transform.GetComponent<HumanController>();
|
||||
var thisHuman = GetComponent<HumanController>();
|
||||
if (otherHuman != null && thisHuman != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var otherDamageController = col.transform.GetComponent<ContactDamage>();
|
||||
if (otherDamageController != null){
|
||||
if (_ignore.Contains(otherDamageController)) {
|
||||
Debug.Log("Skip damage on " + name);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
otherDamageController.AddIgnoreObject(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
float impulse;
|
||||
var rb = GetComponent<Rigidbody2D>();
|
||||
var v1 = rb.GetWorldPointVelocityEx(col.contacts[0].point);
|
||||
Vector2 v2 = Vector2.zero;
|
||||
float m2;
|
||||
Vector2 relativeVelocity;
|
||||
|
||||
if (col.rigidbody != null && !col.rigidbody.isKinematic) {
|
||||
v2 = col.rigidbody.GetWorldPointVelocityEx(col.contacts[0].point);
|
||||
m2 = col.rigidbody.mass;
|
||||
}
|
||||
else {
|
||||
v2 = Vector2.zero;
|
||||
m2 = 0;
|
||||
}
|
||||
|
||||
relativeVelocity = v2 - v1;
|
||||
impulse = (rb.mass*v1 + m2*v2).magnitude;
|
||||
|
||||
|
||||
|
||||
Debug.LogFormat("Collision! Impulse = {0} on object {1} ({4}), relative velocity = {2}, self velocity = {3}", impulse, name, relativeVelocity.magnitude, v1.magnitude, col.transform.name);
|
||||
OnImpulse(impulse, col.contacts[0].point, col.gameObject);
|
||||
if (otherDamageController != null) {
|
||||
otherDamageController.OnImpulse(impulse, col.contacts[0].point, gameObject);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ad9db2b8c80a00a4688dcde780f548c9
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e07b0af2dd81d604a9572cd7f48a50c4
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class DebugPointVelocity : MonoBehaviour {
|
||||
private Physics2DEx _rb;
|
||||
public Vector3 offset = Vector3.up;
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
_rb = GetComponent<Physics2DEx>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
if (_rb == null) {
|
||||
_rb = GetComponent<Physics2DEx>();
|
||||
}
|
||||
|
||||
if (_rb != null) {
|
||||
var worldPoint = transform.localToWorldMatrix.MultiplyPoint(offset);
|
||||
var velocity = _rb.GetWorldPointVelocity(worldPoint);
|
||||
Gizmos.DrawLine(worldPoint, (Vector3)velocity + worldPoint);
|
||||
AdvancedGizmo.DrawCircle(worldPoint, Vector3.forward, 0.1f, Color.red);
|
||||
|
||||
var r = GetComponent<Rigidbody2D>();
|
||||
var v = r.GetPointVelocity(worldPoint);
|
||||
|
||||
Gizmos.color = Color.blue;
|
||||
Gizmos.DrawLine(worldPoint, (Vector3) v + worldPoint);
|
||||
|
||||
//Debug.LogFormat("Unity: {0}, Custom: {1}, Angular velocity: {2}", v.magnitude, velocity.magnitude, _rb.AngularVelocity);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 329ecaf192a2b654cab02fa37d8dd5f8
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,47 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public enum DetonatorState{
|
||||
Idle,
|
||||
Wait,
|
||||
Exploded
|
||||
}
|
||||
|
||||
public class DelayDetonator : MonoBehaviour {
|
||||
|
||||
public Explosive target;
|
||||
private AudioSource _source;
|
||||
public Predicate trigger;
|
||||
private DetonatorState _state;
|
||||
public float delay = 3.0f;
|
||||
private float timeElapsed = 0;
|
||||
|
||||
void Start () {
|
||||
if(target == null){
|
||||
target = GetComponentInChildren<Explosive>();
|
||||
}
|
||||
_source = GetComponent<AudioSource>();
|
||||
}
|
||||
|
||||
void Update () {
|
||||
if(_state == DetonatorState.Idle && trigger != null && trigger.Check()){
|
||||
_state = DetonatorState.Wait;
|
||||
if(_source != null){
|
||||
_source.Play();
|
||||
}
|
||||
}
|
||||
if(_state == DetonatorState.Wait){
|
||||
timeElapsed += Time.deltaTime;
|
||||
if(timeElapsed >= delay){
|
||||
_state = DetonatorState.Exploded;
|
||||
if(_source != null){
|
||||
_source.Stop();
|
||||
}
|
||||
if(target != null){
|
||||
target.Explode();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user