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 ModEffect { Speed, Border }
public class ActiveModification {
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 class Modification : Collectable {
public ModificationProperties Properties { get; set; }
@ -46,13 +20,8 @@ namespace Game {
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D other) {
Player p = other.GetComponent<Ball>().LastContactPlayer;
if (p != null) {
Destroy(gameObject);
ActiveModification mod = new() {Properties = Properties};
p.StartCoroutine(mod.Process(p.Panel, p.Modifications));
}
protected override void OnCollect(Player collector) {
collector.StartCoroutine(collector.ProcessModification(Properties));
}
}
}

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Netcode;
@ -18,7 +19,7 @@ namespace Game {
public VisualElement Panel { get; private set; }
public List<ActiveModification> Modifications { get; } = new();
private List<ModificationProperties> Modifications { get; } = new();
private int Score { get; set; }
@ -27,8 +28,8 @@ namespace Game {
private const float SpeedMultiplier = 1.5f;
protected float Speed {
get {
return Modifications.Where(m => m.Properties.effect == ModEffect.Speed)
.Aggregate(BaseSpeed, (current, speedMod) => current * speedMod.Properties.type switch {
return Modifications.Where(m => m.effect == ModEffect.Speed)
.Aggregate(BaseSpeed, (current, speedMod) => current * speedMod.type switch {
ModType.Buff => SpeedMultiplier,
ModType.Nerf => 1 / SpeedMultiplier,
_ => throw new ArgumentOutOfRangeException()
@ -41,8 +42,8 @@ namespace Game {
private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.1f;
protected float Border {
get {
return Mathf.Min(Modifications.Where(m => m.Properties.effect == ModEffect.Border)
.Aggregate(baseBorder, (current, borderMod) => current + borderMod.Properties.type switch {
return Mathf.Min(Modifications.Where(m => m.effect == ModEffect.Border)
.Aggregate(baseBorder, (current, borderMod) => current + borderMod.type switch {
ModType.Buff => borderSummand,
ModType.Nerf => -borderSummand,
_ => throw new ArgumentOutOfRangeException()
@ -70,6 +71,27 @@ namespace Game {
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() {
if (LeftEdge < -Border)
transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World);

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

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

Loading…
Cancel
Save