death zone handles balls, score text updated

main
Benjamin Kraft 1 year ago
parent 1a71516e55
commit 6c146a9cbd
  1. 2
      Assets/Scenes/Game.unity
  2. 6
      Assets/Scripts/DeathGate.cs
  3. 3
      Assets/Scripts/Game/AIPlayer.cs
  4. 13
      Assets/Scripts/Game/Ball.cs
  5. 33
      Assets/Scripts/Game/DeathGate.cs
  6. 0
      Assets/Scripts/Game/DeathGate.cs.meta
  7. 28
      Assets/Scripts/Game/GameManager.cs
  8. 18
      Assets/Scripts/Game/Player.cs
  9. 3
      Assets/Scripts/Game/Settings.cs
  10. 8
      Assets/Scripts/GameUI.cs
  11. 6
      Assets/UI Toolkit/PlayerUI.uxml

@ -452,6 +452,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2e929ccd8ac04a4fb91c00bda3b16bb5, type: 3}
m_Name:
m_EditorClassIdentifier:
side: 1
--- !u!1 &1462314553
GameObject:
m_ObjectHideFlags: 0
@ -648,6 +649,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2e929ccd8ac04a4fb91c00bda3b16bb5, type: 3}
m_Name:
m_EditorClassIdentifier:
side: 0
--- !u!1 &2109945866
GameObject:
m_ObjectHideFlags: 0

@ -1,6 +0,0 @@
using UnityEngine;
// Graphical bonus to make ball going through it look cool
public class DeathGate : MonoBehaviour {
}

@ -144,8 +144,7 @@ namespace Game {
}
private float GetTargetPosition() {
var balls = GameManager.Singleton.Balls.Where(b => b.IsAlive);
var approaching = balls.Where(BallApproaches).ToList();
var approaching = GameManager.Singleton.Balls.Where(BallApproaches).ToList();
while (approaching.Count > 0) {

@ -16,9 +16,8 @@ namespace Game {
get => transform.localScale.x * Collider.radius;
set => transform.localScale = new Vector3(1, 1, 1) * value * 2;
}
public bool IsAlive { get; private set; } = true;
private void OnEnable() {
private void Awake() {
Rb = GetComponent<Rigidbody2D>();
Collider = GetComponent<CircleCollider2D>();
}
@ -27,12 +26,6 @@ namespace Game {
Rb.velocity = new Vector2(0, 25);
Rb.position = new Vector2(Random.Range(-2, 2), 0);
}
private void FixedUpdate() {
//TODO remove this and use triggers for death zone
if (Rb.position.y > Dimensions.Singleton.top || Rb.position.y < Dimensions.Singleton.bottom) {
IsAlive = false;
}
}
}
}

@ -0,0 +1,33 @@
using System;
using UnityEngine;
namespace Game {
public class DeathGate : MonoBehaviour {
public Side side;
private Player thisPlayer, otherPlayer;
private void Start() {
Player p1 = GameManager.Singleton.Player1;
Player p2 = GameManager.Singleton.Player2;
if (p1.Side == side) {
thisPlayer = p1;
otherPlayer = p2;
} else {
thisPlayer = p2;
otherPlayer = p1;
}
}
private void OnTriggerEnter2D(Collider2D other) {
otherPlayer.GainScore();
Ball ball = other.GetComponent<Ball>();
GameManager.Singleton.RemoveBall(ball);
GameManager.Singleton.SpawnBall();
}
}
}

@ -24,6 +24,8 @@ namespace Game {
public ModificationProperties[] modifications;
public List<Ball> Balls { get; } = new();
public Player Player1 { get; private set; }
public Player Player2 { get; private set; }
private static void Tests() {
var v1 = new Vector2(1, 3);
@ -57,7 +59,7 @@ namespace Game {
private void Awake() {
Settings.Type = Type.Hybrid;
Settings.AIDifficulty = Difficulty.VeryHard;
Settings.AIDifficulty = Difficulty.VeryEasy;
var ball = Instantiate(ballPrefab).GetComponent<Ball>();
Balls.Add(ball);
@ -74,7 +76,7 @@ namespace Game {
((AIPlayer) p1).Difficulty = Settings.AIDifficulty;
((AIPlayer) p2).Difficulty = Settings.AIDifficulty;
break;
case Type.Real:
case Type.RealOnline:
p1 = p1Obj.AddComponent<RealPlayer>();
p2 = p2Obj.AddComponent<RealPlayer>();
((RealPlayer) p1).isThisClient = true;
@ -91,6 +93,9 @@ namespace Game {
p1.Side = Side.Bottom;
p2.Side = Side.Top;
Player1 = p1;
Player2 = p2;
Tests();
}
@ -108,14 +113,19 @@ namespace Game {
mod.Properties = properties;
}
private void FixedUpdate() {
foreach (var ball in Balls.Where(b => !b.IsAlive))
Destroy(ball.gameObject);
public void SpawnBall() {
Vector2 position = Vector2.zero;
Vector2 velocity = Vector2.up;
var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent<Ball>();
var rb = ball.Rb;
rb.velocity = velocity;
int count = Balls.RemoveAll(b => !b.IsAlive);
for (int _ = 0; _ < count; _++) {
Balls.Add(Instantiate(ballPrefab).GetComponent<Ball>());
}
Balls.Add(ball);
}
public void RemoveBall(Ball ball) {
Balls.Remove(ball);
Destroy(ball.gameObject);
}
}

@ -1,7 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Transactions;
using Unity.Netcode;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
@ -14,14 +17,15 @@ namespace Game {
public Side Side { get; set; }
private int score;
protected bool goingLeft, goingRight;
public VisualElement Panel { get; private set; }
public List<ActiveModification> Modifications { get; } = new();
[SerializeField]
public int score = 0;
// Units per second
protected float Speed => 15;
@ -37,6 +41,15 @@ namespace Game {
protected float Y => transform.position.y;
public void GainScore() {
score++;
UpdatePanel();
}
private void UpdatePanel() {
Panel.Q<TextElement>("score").text = score.ToString();
}
protected void ClampInsideBorders() {
if (LeftEdge < -Border)
transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World);
@ -61,6 +74,7 @@ namespace Game {
if (Side == Side.Top)
transform.Rotate(transform.forward, 180);
Panel = GameUI.Singleton.PlayerPanel(Side);
UpdatePanel();
}
private void OnCollisionEnter2D(Collision2D other) {

@ -1,6 +1,7 @@
namespace Game {
public enum Type { Real, Hybrid, AI }
public enum Type { RealOnline, RealOffline, Hybrid, AI }
public enum Difficulty { VeryEasy, Easy, Medium, Hard, VeryHard }
public struct Settings {
public static Type Type;

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Game;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
@ -26,7 +27,6 @@ public class GameUI : MonoBehaviour {
};
}
// 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);
@ -49,11 +49,7 @@ public class GameUI : MonoBehaviour {
}
void PreparePlayerPanels() {
var players = PlayerPanels;
Assert.AreEqual(players.Count, 2);
float heightPercentage = Dimensions.Singleton.panelHeightPercentage;
foreach (var playerPanel in players)
playerPanel.style.height = Length.Percent(heightPercentage);
PlayerPanel(Side.Top).style.height = PlayerPanel(Side.Bottom).style.height = Length.Percent(heightPercentage);
}
}

@ -1,9 +1,9 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/UI%20Toolkit/styles.uss?fileID=7433441132597879392&amp;guid=a6abab2f6d20ba823bad41d2081810e9&amp;type=3#styles" />
<Style src="project://database/Assets/UI%20Toolkit/player_panel.uss?fileID=7433441132597879392&amp;guid=102b2cb1b84d8883abde9356b92d807e&amp;type=3#player_panel" />
<ui:Button text="Right" display-tooltip-when-elided="true" name="btn_right" />
<ui:Button text="Left" display-tooltip-when-elided="true" name="btn_left" />
<ui:VisualElement class="mods_list" />
<ui:Label text="0" display-tooltip-when-elided="true" name="score" />
<ui:Label display-tooltip-when-elided="true" name="score" binding-path="m_Score" />
<ui:VisualElement class="mods_list" />
<ui:Button text="Left" display-tooltip-when-elided="true" name="btn_left" />
<ui:Button text="Right" display-tooltip-when-elided="true" name="btn_right" />
</ui:UXML>

Loading…
Cancel
Save