Skip to content

Commit

Permalink
Evolution shader effect added. Fully functional but will soft-rework
Browse files Browse the repository at this point in the history
not perfectly structured. also part of PlayerBoardMan.cs at the moment, would like a separate PlayerEvolutionMan.cs
  • Loading branch information
mariuskilian committed May 7, 2020
1 parent 2a75e62 commit cbdc9f9
Show file tree
Hide file tree
Showing 41 changed files with 422 additions and 67 deletions.
1 change: 1 addition & 0 deletions Assets/Materials/BoardUnits/001Bulbasaur/BodyB.mat
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Material:
- _DissolveThickness: 0.05
- _DstBlend: 0
- _EnvironmentReflections: 1
- _EvoAlphaFade: 1
- _EvoFade: 0
- _EvoSize: 200
- _Fade: 0.5
Expand Down
Binary file modified Assets/Photon/PhotonBolt/assemblies/bolt.user.dll
Binary file not shown.
68 changes: 68 additions & 0 deletions Assets/Photon/PhotonBolt/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,74 @@
"MecanimDirection": 1,
"SnapMagnitude": 10.0
}
},
{
"Name": "ShaderEvoAlphaFade",
"Enabled": true,
"ReplicationMode": 1,
"Priority": 1,
"PropertyType": {
"$type": "Bolt.Compiler.PropertyTypeFloat, bolt.compiler",
"Compression": {
"MinValue": -2048,
"MaxValue": 2048,
"Accuracy": 0.01,
"Pack": 100.0,
"Read": 0.01,
"Shift": 2048.0,
"BitsRequired": 19
}
},
"AssetSettings": {
"$type": "Bolt.Compiler.PropertyStateSettings, bolt.compiler",
"ExtrapolationErrorTolerance": 0.25,
"_ExtrapolationCorrectionFrames": 6,
"ExtrapolationMaxFrames": 9,
"SnapMagnitude": 10.0
}
},
{
"Name": "ShaderEvoFade",
"Enabled": true,
"ReplicationMode": 1,
"Priority": 1,
"PropertyType": {
"$type": "Bolt.Compiler.PropertyTypeFloat, bolt.compiler",
"Compression": {
"MinValue": -2048,
"MaxValue": 2048,
"Accuracy": 0.01,
"Pack": 100.0,
"Read": 0.01,
"Shift": 2048.0,
"BitsRequired": 19
}
},
"AssetSettings": {
"$type": "Bolt.Compiler.PropertyStateSettings, bolt.compiler",
"ExtrapolationErrorTolerance": 0.25,
"_ExtrapolationCorrectionFrames": 6,
"ExtrapolationMaxFrames": 9,
"SnapMagnitude": 10.0
}
},
{
"Name": "LayerIdx",
"Enabled": true,
"Expanded": true,
"ReplicationMode": 1,
"Priority": 1,
"PropertyType": {
"$type": "Bolt.Compiler.PropertyTypeInteger, bolt.compiler",
"MaxValue": 255
},
"AssetSettings": {
"$type": "Bolt.Compiler.PropertyStateSettings, bolt.compiler",
"ExtrapolationErrorTolerance": 0.25,
"_ExtrapolationCorrectionFrames": 6,
"ExtrapolationMaxFrames": 9,
"SnapMagnitude": 10.0
}
}
],
"PacketMaxBits": 512,
Expand Down
4 changes: 2 additions & 2 deletions Assets/Photon/PhotonBolt/resources/BoltRuntimeSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ MonoBehaviour:
commandRedundancy: 6
commandPingMultiplier: 1.25
useNetworkSimulation: 1
simulatedLoss: 0.13
simulatedLoss: 0
simulatedPingMean: 100
simulatedPingJitter: 50
simulatedRandomFunction: 0
Expand All @@ -48,7 +48,7 @@ MonoBehaviour:
debugStartPort: 50538
debugStartMapName: Ingame
debugPlayAsServer: 0
showDebugInfo: 1
showDebugInfo: 0
overrideTimeScale: 1
logUncaughtExceptions: 0
debugEditorMode: 2
Expand Down
15 changes: 13 additions & 2 deletions Assets/Scripts/Other/EntityBehaviours/BoardUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public override void Attached() {

state.AddCallback("Position", () => transform.position = state.Position);
state.AddCallback("Rotation", () => transform.rotation = state.Rotation);
state.AddCallback("LayerIdx", UpdateLayerIdx);

InitUnit();
}
Expand All @@ -29,13 +30,15 @@ private void InitUnit() {
if (BoltNetwork.IsServer) {
UnitBehaviours = new List<UnitComponent> {
gameObject.AddComponent<BoardUnitRandomGesture>(),
gameObject.AddComponent<BoardUnitBoardAnimations>()
gameObject.AddComponent<BoardUnitBoardAnimations>(),
gameObject.AddComponent<BoardUnitEvolutionShaderEffect>()
};
}
else if (BoltNetwork.IsClient) {
if (entity.HasControl) AddCollisionPlane();
UnitBehaviours = new List<UnitComponent> {
gameObject.AddComponent<BoardUnitCarryAnimation>()
gameObject.AddComponent<BoardUnitCarryAnimation>(),
gameObject.AddComponent<BoardUnitEvolutionShaderEffect>()
};
}
}
Expand All @@ -62,4 +65,12 @@ public void SetPositionAndRotation(Vector3 position, Quaternion rotation, bool l
state.Rotation = transform.rotation;
}

private void UpdateLayerIdx() {
gameObject.layer = state.LayerIdx;
for (int childIdx = 0; childIdx < transform.childCount; childIdx++)
transform.GetChild(childIdx).gameObject.layer = state.LayerIdx;
}

public void SetClickable(bool clickable) { state.LayerIdx = (clickable) ? LayerMask.NameToLayer("Units") : 0; }

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,95 @@
using UnityEngine;
using System.Collections;
using static GameInfo;

public class BoardUnitEvolutionShaderEffect : UnitShaderEffects {

public float ShaderEvoFade {
get { return This<BoardUnit>().state.ShaderEvoFade; }
set { This<BoardUnit>().state.ShaderEvoFade = Mathf.Clamp(value, 0f, 1f); }
}
public float ShaderEvoAlphaFade {
get {return This<BoardUnit>().state.ShaderEvoAlphaFade; }
set { This<BoardUnit>().state.ShaderEvoAlphaFade = Mathf.Clamp(value, 0f, 1f); }
}

private readonly string
EvoFade = "_EvoFade"
EvoFade = "_EvoFade",
EvoAlphaFade = "_EvoAlphaFade"
;

bool IsEvolvedUnit = false;
private bool isEvolving = false;

private float speed = 0.5f;

private new void Awake() {
base.Awake();
This<BoardUnit>().state.AddCallback("ShaderEvoFade", UpdateEvoParameters);
This<BoardUnit>().state.AddCallback("ShaderEvoAlphaFade", UpdateEvoParameters);
}

private void Start() { if (BoltNetwork.IsServer) { SubscribeLocalEventHandlers(); } }

private IEnumerator EvolveUnit() {
yield return new WaitForSeconds(2 * speed * ShaderEvoFade);
ShaderEvoAlphaFade = 1f;
isEvolving = true;
while (isEvolving) {
ShaderEvoFade += speed * Time.deltaTime;
if (ShaderEvoFade == 1f) isEvolving = false;
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds(0.1f);
isEvolving = true;
while (isEvolving) {
ShaderEvoAlphaFade -= speed * Time.deltaTime;
if (ShaderEvoAlphaFade == 0f) isEvolving = false;
yield return new WaitForEndOfFrame();
}
}

private IEnumerator SpawnEvolvedUnit() {
ShaderEvoFade = 1f;
ShaderEvoAlphaFade = 1f;
ShaderEvoAlphaFade = 0f;
isEvolving = true;
while (isEvolving) {
ShaderEvoAlphaFade += speed * Time.deltaTime;
if (ShaderEvoAlphaFade == 1f) isEvolving = false;
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds(0.1f);
isEvolving = true;
while (isEvolving) {
ShaderEvoFade -= speed * Time.deltaTime;
if (ShaderEvoFade == 0f) isEvolving = false;
yield return new WaitForEndOfFrame();
}
}

private void UpdateEvoParameters() {
foreach (var m in Materials) {
m.SetFloat(EvoFade, ShaderEvoFade);
m.SetFloat(EvoAlphaFade, ShaderEvoAlphaFade);
}
}

private void SubscribeLocalEventHandlers() {
Player player = This<BoardUnit>().Owner;
var board = player.GetPlayerMan<PlayerBoardMan>();
board.EvolvingUnitEvent += HandleEvolvingUnitEvent;
board.SpawnedEvolvedUnitEvent += HandleSpawnedEvolvedUnitEvent;
}

private void HandleEvolvingUnitEvent(BoardUnit unit) {
if (!IsThis<BoardUnit>(unit)) return;
StopAllCoroutines();
StartCoroutine(EvolveUnit());
}

private void Update() {

private void HandleSpawnedEvolvedUnitEvent(BoardUnit unit) {
if (!IsThis<BoardUnit>(unit)) return;
StartCoroutine(SpawnEvolvedUnit());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private readonly string

private void Update() {
if (spawned) return;
float speed = 0.5f;
foreach (Material m in Materials) {
float speed = 0.5f;
m.SetFloat(AlphaFade, m.GetFloat(AlphaFade) + speed * Time.deltaTime);
if (m.GetFloat(AlphaFade) >= 2f + 0.05f) spawned = true;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Other/UnitComponents/UnitShaderEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class UnitShaderEffects : UnitComponent {

protected List<Material> Materials;

private new void Awake() {
protected new void Awake() {
base.Awake();
Materials = new List<Material>();
foreach (var m in gameObject.GetComponentInChildren<Renderer>().materials) {
Expand Down
50 changes: 44 additions & 6 deletions Assets/Scripts/Server/PerPlayer/PlayerManagers/PlayerBoardMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bolt;
using System;
using System.Collections.Generic;
using System.Collections;

public class PlayerBoardMan : PlayerManager {

Expand Down Expand Up @@ -73,24 +74,59 @@ private UnitContainer FindOrCreateUnitContainer(BoardUnit unit) {
private void CheckForEvolution(BoardUnit unit) {
if (unit.evolution == null) return;
List<Tile> Tiles = FindOrCreateUnitContainer(unit).CheckForEvolution(unit.evolutionChain);
if (Tiles != null) Evolve(Tiles);
if (Tiles != null) StartCoroutine(Evolve(Tiles));
}

private void Evolve(List<Tile> Tiles) {
BoardUnit evolvedUnit = SpawnUnit(Tiles[0].CurrentUnit.evolution);
foreach (Tile t in Tiles) {
BoardUnit unit = t.ClearTile();
BoltNetwork.Destroy(unit.gameObject);
private IEnumerator Evolve(List<Tile> Tiles) {

BoardUnit firstUnit = Tiles[0].CurrentUnit;
BoardUnit lastUnit = Tiles[Tiles.Count - 1].CurrentUnit;
List<BoardUnit> Units = new List<BoardUnit>();

// Start evolution for base units
foreach (var t in Tiles) {
t.CurrentUnit.SetClickable(false);
EvolvingUnitEvent?.Invoke(t.CurrentUnit);
Units.Add(t.CurrentUnit);
yield return new WaitForSeconds(0.2f); // Tiny delay between each of them
}

// Wait until last one is ready
while (lastUnit.state.ShaderEvoFade < 1f) yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.1f); // Then wait just a little bit more

// Spawn evolution
BoardUnit evolvedUnit = SpawnUnit(firstUnit.evolution);
evolvedUnit.SetClickable(false);
yield return new WaitForEndOfFrame();
Tiles[0].ClearTile();
Tiles[0].FillTile(evolvedUnit);
SpawnedEvolvedUnitEvent?.Invoke(evolvedUnit);

// Wait until it has spawned
while (evolvedUnit.state.ShaderEvoAlphaFade < 1f) yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.1f); // Then wait just a little bit more

// Despawn base units once last one is invisible
while (lastUnit.state.ShaderEvoAlphaFade > 0f) yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.1f); // Then wait just a little bit more ;)

foreach (var unit in Units) { if (unit != firstUnit) unit.CurrentTile.ClearTile(); BoltNetwork.Destroy(unit.gameObject); }

// Check if this unit can evolve
CheckForEvolution(evolvedUnit);

// Wait for evolved unit to be done spawning
while (evolvedUnit.state.ShaderEvoFade > 0f) yield return new WaitForEndOfFrame();
evolvedUnit.SetClickable(true);
}
#endregion

private BoardUnit SpawnUnit(BoardUnit unitPrefab) {
var unitEntity = BoltNetwork.Instantiate(unitPrefab.gameObject);
unitEntity.AssignControl(player.Connection);
BoardUnit unit = unitEntity.GetComponent<BoardUnit>();
unit.SetClickable(true);
unit.SetOwner(player);
FindOrCreateUnitContainer(unit).TryAddUnit(unit);
return unit;
Expand All @@ -105,6 +141,8 @@ public bool CanSpawnUnit(BoardUnit unit) {
#region Local Events
public Action<BoardUnit> UnitPlacedEvent;
public Action<BoardUnit> UnitTeleportedEvent;
public Action<BoardUnit> EvolvingUnitEvent;
public Action<BoardUnit> SpawnedEvolvedUnitEvent;
#endregion

#region Local Event Handlers
Expand Down
Loading

0 comments on commit cbdc9f9

Please sign in to comment.