initial commit

This commit is contained in:
2021-03-18 13:57:21 +01:00
commit e11972ef37
82 changed files with 6086 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallMovement : MonoBehaviour
{
[Range(0,1)]
public float movementSpeed = 1f;
public Vector2 currentMotion = new Vector2(0,-1);
void FixedUpdate()
{
var x = this.transform.position.x;
var y = this.transform.position.y;
var z = this.transform.position.z;
this.transform.position = new Vector3(
x + (this.currentMotion.x * this.movementSpeed),
y,
z + (this.currentMotion.y * this.movementSpeed)
);
}
public void BounceTo(Vector2 dir)
{
this.currentMotion = Vector2.Reflect(this.currentMotion,dir);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ee3144bc9ae9565bcad02924bd49be38
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

56
Assets/Scripts/Block.cs Normal file
View File

@@ -0,0 +1,56 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
public Material[] stageMat;
public int hitPoints = 1;
public GameObject powerupPrefab;
void Start()
{
setMat();
}
// Called by Bouncer when hit by ball
public void OnHit()
{
this.hitPoints--;
Gamemaster.instance.AddPoints(1);
if (this.hitPoints <= 0)
{
var randomNum = Random.Range(0,1f);
if (randomNum >= 0.2){
Instantiate(this.powerupPrefab,this.transform.position,Quaternion.identity);
}
// Delete block
Object.Destroy(this.gameObject);
}else
{
setMat();
}
}
private void setMat()
{
Material mat = null;
if (this.hitPoints <= 1)
{
mat = this.stageMat[0];
}else if (this.hitPoints <= 2)
{
mat = this.stageMat[1];
}else
{
mat = this.stageMat[2];
}
if (mat != null)
{
this.GetComponent<MeshRenderer>().material = mat;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3f8a9841f21e35bc2a147f260744d5e0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,75 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockBouncer : MonoBehaviour
{
private Block parent;
void Start(){
parent = this.GetComponent<Block>();
}
void OnTriggerEnter(Collider other)
{
var ballMovement = other.GetComponent<BallMovement>();
if (ballMovement != null){
var contractPoint = this.GetComponent<Collider>().ClosestPointOnBounds(other.transform.position);
var vec = bounceVector(new Vector2(contractPoint.x,contractPoint.z));
Debug.DrawRay(contractPoint,new Vector3(vec.x,1,vec.y),Color.green,10);
ballMovement.BounceTo(vec);
this.parent.OnHit();
}
}
// Warning: Shitty code
private Vector2 bounceVector(Vector2 colPos)
{
var pos = new Vector2(this.transform.position.x,this.transform.position.z);
var hWidth = this.GetComponent<Collider>().bounds.size.x / 2f;
var hHight = this.GetComponent<Collider>().bounds.size.z / 2f;
var topRight = pos + new Vector2(-hWidth,-hHight);
var topLeft = pos + new Vector2(hWidth,-hHight);
var botRight = pos + new Vector2(-hWidth,hHight);
var botLeft = pos + new Vector2(hWidth,hHight);
if (colPos.y >= (pos.y - hHight)){
// 1 2 3
if (colPos.x >= (pos.x + hWidth)){
// 1
return Vector2.left;
}else if (colPos.x <= (pos.x - hHight)){
// 3
return Vector2.right;
}else{
// 2
return Vector2.down;
}
}else if (colPos.y >= (pos.y + hHight)){
// 6 7 8
if (colPos.x >= (pos.x + hWidth)){
// 6
Debug.Log("6");
}else if (colPos.x <= (pos.x - hHight)){
// 8
Debug.Log("8");
}else{
// 7
return Vector2.up;
}
}else{
// 4 5
if (colPos.x >= pos.x){
// 4
Debug.Log("4");
}else{
// 5
Debug.Log("5");
}
}
return new Vector2(0f,0f);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9d5d7f683f2d461daa072c8d869ebdfb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverArea : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
var ballMovement = other.GetComponent<BallMovement>();
if (ballMovement != null){
// Ball lost
Gamemaster.instance.LostBall();
}
Object.Destroy(other.gameObject);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e2d8f3d57700e04c5906a5881866e6b6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,75 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gamemaster : MonoBehaviour
{
public static Gamemaster instance;
public GameObject textPoints;
public GameObject textBalls;
public GameObject ballPrefab;
public GameObject paddle;
private int points = 0;
private int balls = 3;
private float slomoTime = -1f;
void Start()
{
Gamemaster.instance = this;
updateText();
}
void Update(){
if (slomoTime >= 0){
slomoTime += Time.deltaTime;
if (slomoTime >= 5){
Time.timeScale = 1f;
slomoTime = -1f;
}
}
}
private void updateText(){
this.textPoints.GetComponent<TextMesh>().text = this.points.ToString();
this.textBalls.GetComponent<TextMesh>().text = this.balls.ToString();
}
public void LostBall(){
this.balls--;
if (this.balls < 0){
gameOver();
}else{
updateText();
spawnNewBall();
}
}
public void AddPoints(int points){
this.points += points;
updateText();
}
public void Slomo()
{
Time.timeScale = 0.6f;
this.slomoTime = 0;
}
public void PaddleSizePowerup()
{
var scale = this.paddle.transform.localScale;
this.paddle.transform.localScale = new Vector3(scale.x + 0.4f,scale.y,scale.z);
}
private void spawnNewBall(){
var pos = this.paddle.transform.position;
var newBall = Instantiate(this.ballPrefab,new Vector3(pos.x,1,pos.z - 1),Quaternion.identity);
}
private void gameOver(){
Debug.Log("Game over");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 369ece4d4ffc1a949ae794e00914c2c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PaddleBouncer : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
var ballMovement = other.GetComponent<BallMovement>();
if (ballMovement != null){
var ballPos = other.transform.position;
var centerOffset = ballPos.x - this.transform.position.x;
var relOffset = (centerOffset / this.GetComponent<Collider>().bounds.size.x);
var bouceVector = new Vector2(-relOffset,1);
ballMovement.BounceTo(bouceVector.normalized);
return;
}
var powerup = other.GetComponent<Powerup>();
if (powerup != null)
{
powerup.Activate();
return;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9d85861f5df24b338b99628eff34e5cd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PaddleMovement : MonoBehaviour
{
[Range(0,2)]
public float movementSpeed = 1f;
private float currentMotion = 0f;
public float leftBound = -4.8f;
public float rightBound = 4.8f;
void FixedUpdate(){
var x = this.transform.position.x;
var y = this.transform.position.y;
var z = this.transform.position.z;
var paddleSize = this.GetComponent<BoxCollider>().bounds.size.x;
var newX = x + (-this.currentMotion * this.movementSpeed);
this.transform.position = new Vector3(
Mathf.Clamp(
newX,
leftBound + (paddleSize / 2),
rightBound - (paddleSize / 2)
),
y,
z
);
}
void OnRightLeft(InputValue value)
{
this.currentMotion = value.Get<float>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2a92634fc8378a95b867df4e88c07377
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

28
Assets/Scripts/Powerup.cs Normal file
View File

@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Powerup : MonoBehaviour
{
[Range(0,1)]
public float movementSpeed = 0.5f;
public void FixedUpdate()
{
var x = this.transform.position.x;
var y = this.transform.position.y;
var z = this.transform.position.z;
this.transform.position = new Vector3(x,y,z + movementSpeed);
}
public void Activate()
{
var randomNum = Random.Range(0,1f);
if (randomNum >= 0.5){
Gamemaster.instance.Slomo();
}else{
Gamemaster.instance.PaddleSizePowerup();
}
Object.Destroy(this.gameObject);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c540b6f98f3a38b98865a4e44e6a844
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallBouncer : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
var ballMovement = other.GetComponent<BallMovement>();
if (ballMovement != null)
{
// Debug.DrawRay(this.transform.position,this.transform.forward,Color.green,10);
var forward = this.transform.forward;
ballMovement.BounceTo(new Vector2(forward.x,forward.z));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c8b55467734a6caa5ac615f71be7a68d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: