You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.7 KiB

using System;
using UnityEngine;
[ExecuteInEditMode]
public class Dimensions : MonoBehaviour {
[Tooltip("How much height for player panels")]
public float panelHeightPercentage;
[Tooltip("How much height is the empty space between board and death zone")]
public float emptySpacePercentage;
[Tooltip("Aspect ratio of the playground")]
public float playGroundAspect;
public Camera mainCamera;
public EdgeCollider2D topC, bottomC, leftC, rightC;
public SpriteRenderer playGround;
public SpriteRenderer playerPanelTop;
public SpriteRenderer playerPanelBottom;
public SpriteRenderer background;
public static Dimensions Singleton;
private void OnEnable() {
Singleton = this;
}
public float top, bottom, left, right;
public float boardTop, boardBottom;
public float Width => right * 2;
public float Height => top * 2;
private LineRenderer topL, bottomL;
private void OnValidate() {
topL = topC.GetComponent<LineRenderer>();
bottomL = bottomC.GetComponent<LineRenderer>();
}
private void Update() {
float height = mainCamera.orthographicSize * 2;
float panelHeight = height * panelHeightPercentage / 100f;
float playGroundHeight = height - 2 * panelHeight;
float playGroundWidth = playGroundHeight * playGroundAspect;
float emptySpaceHeight = playGroundHeight * emptySpacePercentage / 100f;
top = playGroundHeight / 2;
bottom = -top;
right = playGroundWidth / 2;
left = -right;
boardTop = top - emptySpaceHeight;
boardBottom = -boardTop;
Vector2 v1 = new Vector3(left, 0);
Vector2 v2 = new Vector3(right, 0);
Vector2 v3 = new Vector3(0, top);
Vector2 v4 = new Vector3(0, bottom);
topC.transform.position = new Vector2(0, top);
bottomC.transform.position = new Vector2(0, bottom);
leftC.transform.position = new Vector2(left, 0);
rightC.transform.position = new Vector2(right, 0);
topC.points = new[] {v1, v2};
bottomC.points = new[] {v1, v2};
leftC.points = new[] {v3, v4};
rightC.points = new[] {v3, v4};
const float lineWidth = 0.5f;
Vector3 offset = Vector3.up * lineWidth / 2;
topL.positionCount = bottomL.positionCount = 2;
var v12 = new[] {(Vector3) v1 - offset, (Vector3) v2 - offset};
topL.SetPositions(v12);
v12 = new[] {(Vector3) v1 + offset, (Vector3) v2 + offset};
bottomL.SetPositions(v12);
playGround.transform.rotation = Quaternion.Euler(0, 0, 90);
playGround.size = new Vector2(playGroundHeight, playGroundWidth);
playerPanelTop.transform.position = new Vector2(0, (height + Height) / 4);
playerPanelBottom.transform.position = new Vector2(0, -(height + Height) / 4);
float allWidth = mainCamera.aspect * height;
playerPanelTop.size = playerPanelBottom.size = new Vector2(allWidth, panelHeight);
background.size = new Vector2(allWidth, playGroundHeight);
}
}