better collectables and modifications

main
Benjamin Kraft 1 year ago
parent 071180c59d
commit 896ab23c09
  1. 17
      Assets/Scripts/Game/Collectable.cs
  2. 3
      Assets/Scripts/Game/Collectable.cs.meta
  3. 37
      Assets/Scripts/Game/Modification.cs
  4. 32
      Assets/Scripts/Game/Player.cs
  5. 6
      Assets/Scripts/GameUI.cs
  6. 1
      README.md

@ -0,0 +1,17 @@
using System;
using Unity.Netcode;
using UnityEngine;
namespace Game {
public abstract class Collectable : NetworkBehaviour {
private void OnTriggerEnter2D(Collider2D other) {
Player player = other.GetComponent<Ball>().LastContactPlayer;
if (player != null) {
Destroy(gameObject);
OnCollect(player);
}
}
protected abstract void OnCollect(Player collector);
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3b0f221ad4da44aabc6b7164ed4a11be
timeCreated: 1681499802

@ -10,33 +10,7 @@ namespace Game {
public enum ModType { Nerf, Buff } public enum ModType { Nerf, Buff }
public enum ModEffect { Speed, Border } public enum ModEffect { Speed, Border }
public class ActiveModification { public class Modification : Collectable {
public ModificationProperties Properties { get; set; }
public IEnumerator Process(VisualElement panel, List<ActiveModification> modifications) {
modifications.Add(this);
VisualElement element = GameUI.AddModification(panel, this);
float doNothingTime = Properties.activeDuration * 0.7f;
float animateTime = Properties.activeDuration * 0.3f;
yield return new WaitForSeconds(doNothingTime);
float current = 0;
while (current < animateTime) {
current += Time.deltaTime;
element.style.opacity = new StyleFloat(1 - current / animateTime);
yield return null;
}
element.RemoveFromHierarchy();
modifications.Remove(this);
}
}
public class Modification : NetworkBehaviour {
public ModificationProperties Properties { get; set; } public ModificationProperties Properties { get; set; }
@ -46,13 +20,8 @@ namespace Game {
Destroy(gameObject); Destroy(gameObject);
} }
private void OnTriggerEnter2D(Collider2D other) { protected override void OnCollect(Player collector) {
Player p = other.GetComponent<Ball>().LastContactPlayer; collector.StartCoroutine(collector.ProcessModification(Properties));
if (p != null) {
Destroy(gameObject);
ActiveModification mod = new() {Properties = Properties};
p.StartCoroutine(mod.Process(p.Panel, p.Modifications));
}
} }
} }
} }

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Unity.Netcode; using Unity.Netcode;
@ -18,7 +19,7 @@ namespace Game {
public VisualElement Panel { get; private set; } public VisualElement Panel { get; private set; }
public List<ActiveModification> Modifications { get; } = new(); private List<ModificationProperties> Modifications { get; } = new();
private int Score { get; set; } private int Score { get; set; }
@ -27,8 +28,8 @@ namespace Game {
private const float SpeedMultiplier = 1.5f; private const float SpeedMultiplier = 1.5f;
protected float Speed { protected float Speed {
get { get {
return Modifications.Where(m => m.Properties.effect == ModEffect.Speed) return Modifications.Where(m => m.effect == ModEffect.Speed)
.Aggregate(BaseSpeed, (current, speedMod) => current * speedMod.Properties.type switch { .Aggregate(BaseSpeed, (current, speedMod) => current * speedMod.type switch {
ModType.Buff => SpeedMultiplier, ModType.Buff => SpeedMultiplier,
ModType.Nerf => 1 / SpeedMultiplier, ModType.Nerf => 1 / SpeedMultiplier,
_ => throw new ArgumentOutOfRangeException() _ => throw new ArgumentOutOfRangeException()
@ -41,8 +42,8 @@ namespace Game {
private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.1f; private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.1f;
protected float Border { protected float Border {
get { get {
return Mathf.Min(Modifications.Where(m => m.Properties.effect == ModEffect.Border) return Mathf.Min(Modifications.Where(m => m.effect == ModEffect.Border)
.Aggregate(baseBorder, (current, borderMod) => current + borderMod.Properties.type switch { .Aggregate(baseBorder, (current, borderMod) => current + borderMod.type switch {
ModType.Buff => borderSummand, ModType.Buff => borderSummand,
ModType.Nerf => -borderSummand, ModType.Nerf => -borderSummand,
_ => throw new ArgumentOutOfRangeException() _ => throw new ArgumentOutOfRangeException()
@ -70,6 +71,27 @@ namespace Game {
Panel.Q<TextElement>("score").text = Score.ToString(); Panel.Q<TextElement>("score").text = Score.ToString();
} }
public IEnumerator ProcessModification(ModificationProperties properties) {
Modifications.Add(properties);
VisualElement element = GameUI.AddModification(Panel, properties);
float doNothingTime = properties.activeDuration * 0.7f;
float animateTime = properties.activeDuration * 0.3f;
yield return new WaitForSeconds(doNothingTime);
float current = 0;
while (current < animateTime) {
current += Time.deltaTime;
element.style.opacity = new StyleFloat(1 - current / animateTime);
yield return null;
}
element.RemoveFromHierarchy();
Modifications.Remove(properties);
}
protected void ClampInsideBorders() { protected void ClampInsideBorders() {
if (LeftEdge < -Border) if (LeftEdge < -Border)
transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World); transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World);

@ -25,12 +25,12 @@ public class GameUI : MonoBehaviour {
}; };
} }
public static VisualElement AddModification(VisualElement playerPanel, ActiveModification modification) { public static VisualElement AddModification(VisualElement playerPanel, ModificationProperties properties) {
VisualElement listElement = ModsList(playerPanel, modification.Properties.type); VisualElement listElement = ModsList(playerPanel, properties.type);
VisualElement newElement = new() { VisualElement newElement = new() {
style = { style = {
backgroundImage = Background.FromSprite(modification.Properties.image) backgroundImage = Background.FromSprite(properties.image)
} }
}; };
listElement.Add(newElement); listElement.Add(newElement);

@ -6,6 +6,5 @@
- NewBall - NewBall
- Wormhole - Wormhole
- Border visuals - Border visuals
- Ball visuals
### Menu ### Menu

Loading…
Cancel
Save