refactored modifications

main
Benjamin Kraft 1 year ago
parent 456759c6e4
commit de502ca3de
  1. 3
      Assets/Scripts/Game/Ball.cs
  2. 2
      Assets/Scripts/Game/GameManager.cs
  3. 52
      Assets/Scripts/Game/Modification.cs
  4. 15
      Assets/Scripts/Game/Player.cs
  5. 36
      Assets/Scripts/GameUI.cs
  6. 2
      Assets/UI Toolkit/player_panel.uss

@ -1,6 +1,7 @@
using System;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;
namespace Game {
@ -9,6 +10,8 @@ namespace Game {
private CircleCollider2D Collider { get; set; }
public Player LastContactPlayer { get; set; }
public float Radius {
get => transform.localScale.x * Collider.radius;
set => transform.localScale = new Vector3(1, 1, 1) * value * 2;

@ -52,7 +52,7 @@ namespace Game {
Assert.AreApproximatelyEqual(rs.y, 0.5f);
}
private void Start() {
private void Awake() {
Settings.Type = Type.Hybrid;
Settings.AIDifficulty = Difficulty.VeryHard;

@ -1,15 +1,55 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UIElements;
namespace Game {
public class Modification : MonoBehaviour {
public enum ModType { Nerf, Buff }
public enum ModEffect { Speed, Border }
public struct ModificationProperties {
public ModType Type { get; set; }
public ModEffect Effect { get; set; }
public float PickupDuration { get; set; }
public float ActiveDuration { get; set; }
}
public class SpeedModification : Modification {
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);
yield return new WaitForSeconds(Properties.ActiveDuration);
element.RemoveFromHierarchy();
modifications.Remove(this);
}
}
public class Modification : NetworkBehaviour {
public class BorderModification : Modification {
public ModificationProperties Properties { get; set; }
private IEnumerator Start() {
yield return new WaitForSeconds(Properties.PickupDuration);
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D other) {
Player p = other.GetComponent<Ball>().LastContactPlayer;
if (p != null) {
Destroy(gameObject);
ActiveModification mod = new() {Properties = Properties};
StartCoroutine(mod.Process(p.Panel, p.Modifications));
}
}
}
}

@ -1,7 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace Game {
@ -15,14 +18,15 @@ namespace Game {
protected bool goingLeft, goingRight;
public VisualElement Panel { get; private set; }
public List<ActiveModification> Modifications { get; } = new();
// Units per second
protected float Speed => 15;
// Unit distance from zero
protected float Border => Dimensions.Singleton.Width / 2;
private SpeedModification speedModification;
private BorderModification borderModification;
protected float Width => transform.localScale.x;
@ -56,6 +60,11 @@ namespace Game {
transform.position = new Vector2(0, y);
if (Side == Side.Top)
transform.Rotate(transform.forward, 180);
Panel = GameUI.Singleton.PlayerPanel(Side);
}
private void OnCollisionEnter2D(Collision2D other) {
other.gameObject.GetComponent<Ball>().LastContactPlayer = this;
}
}

@ -1,21 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Game;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
public class GameUI : MonoBehaviour {
private UIDocument document;
private void OnEnable() {
public static GameUI Singleton;
private void Awake() {
Singleton = this;
document = GetComponent<UIDocument>();
PreparePlayerPanels();
}
private List<VisualElement> PlayerPanels => document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")).ToList();
public VisualElement PlayerPanel(Side side) {
return side switch {
Side.Top => PlayerPanels[0],
Side.Bottom => PlayerPanels[1],
_ => throw new ArgumentOutOfRangeException(nameof(side), side, null)
};
}
// TODO create correct visual element, attach it to panel with background-image and return it
public static VisualElement AddModification(VisualElement playerPanel, ActiveModification modification) {
VisualElement listElement = ModsList(playerPanel, modification.Properties.Type);
return listElement;
}
private static VisualElement ModsList(VisualElement player, ModType type) {
return type switch {
ModType.Buff => player.Children().First(e => e.ClassListContains("mods_list")),
ModType.Nerf => player.Children().Last(e => e.ClassListContains("mods_list")),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
}
void PreparePlayerPanels() {
var players = document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel"));
var players = PlayerPanels;
Assert.AreEqual(players.Count, 2);
float heightPercentage = Dimensions.Singleton.panelHeightPercentage;
foreach (var playerPanel in players)
playerPanel.style.height = Length.Percent(heightPercentage);
}

@ -19,7 +19,7 @@ Button {
margin-bottom: 5px;
}
.modification {
.mods_list > VisualElement {
width: 100%;
flex-grow: 1;
background-image: url('project://database/Packages/com.unity.2d.sprite/Editor/ObjectMenuCreation/DefaultAssets/Textures/HexagonFlat-Top.png?fileID=2747690628134850419&guid=16037dbd798344f57a526d53d48f5ead&type=3#Hexagon Flat-Top');

Loading…
Cancel
Save