Last active
May 20, 2018 14:35
-
-
Save FleshMobProductions/f7d64caa0064ed38806c336c0ee9ce70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using TrueSync; | |
using System; | |
using System.Collections.Generic; | |
namespace TF2018 | |
{ | |
public class ShockwavePrototype : MonoBehaviour | |
{ | |
public FP destructionTime = 4f; | |
public FP speed = 7f; | |
public FP maxDamage = 25; | |
public FP minDamage = 5; | |
public FP maxKnockback = 4; | |
public FP minKnockback = 1; | |
private List<PlayerCharacterTS2D> hitCharacters; | |
//private PlayerCharacterTS2D[] allCharacters; | |
private FP radius; | |
private FP lifeTime; | |
private TSVector origin; | |
public void SetOrigin(TSVector origin) | |
{ | |
this.origin = origin; | |
} | |
public void AwakeCustom() | |
{ | |
//allCharacters = FindObjectsOfType<PlayerCharacterTS2D>(); | |
//int allCharactersLength = allCharacters != null ? allCharacters.Length : 0; | |
hitCharacters = new List<PlayerCharacterTS2D>(); | |
radius = 0; | |
lifeTime = 0; | |
} | |
public void OnDrawGizmos() | |
{ | |
Color newCol = Color.red; | |
//newCol.a = 0.5f; | |
Gizmos.color = newCol; | |
Gizmos.DrawSphere(origin.ToVector(), (float)radius); | |
} | |
public void UpdateCustom(FP deltaTime, PlayerCharacterTS2D[] allCharacters) | |
{ | |
lifeTime += deltaTime; | |
radius += speed * deltaTime; | |
if (lifeTime >= destructionTime) | |
{ | |
Destroy(this.gameObject); | |
return; | |
} | |
FP ratio = (destructionTime - lifeTime) / destructionTime; | |
FP radiusSquared = radius * radius; | |
for (int i = 0; i < allCharacters.Length; i++) | |
{ | |
PlayerCharacterTS2D character = allCharacters[i]; | |
if (!hitCharacters.Contains(character)) | |
{ | |
TSVector charPos = character.GetCharacterCenter(); | |
//check if overlapping is possible: | |
if (FP.FastAbs(charPos.x - origin.x) <= radius && FP.FastAbs(charPos.y - origin.y) <= radius) | |
{ | |
TSVector distanceVecToExplosionCenter = charPos - origin; | |
FP distanceToExplosionCenterSquared = distanceVecToExplosionCenter.sqrMagnitude; | |
//comparing the squared distances is enough and more efficient | |
if (radius > distanceToExplosionCenterSquared) | |
{ | |
//add character to hitCharacters, so it won't be checked again for collision with the shockwave | |
hitCharacters.Add(character); | |
FP knockbackToApply = TSMath.Lerp(minKnockback, maxKnockback, ratio); | |
FP damageToApply = TSMath.Lerp(minDamage, maxDamage, ratio); | |
TSVector knockbackVec = distanceVecToExplosionCenter.normalized * knockbackToApply; | |
character.AddRigidbodyVelocity(knockbackVec); | |
character.ReceiveDamage(damageToApply, false); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment