Last active
July 26, 2018 19:40
-
-
Save shana/49dd59cb6cdbee0cfccdbb6762497158 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace GitHub.Unity { | |
public class Game | |
{ | |
private Texture2D texture1 = Utility.GetTextureFromColor(Color.blue); | |
private Texture2D paddleTexture; | |
private float wallThickness = 2f; | |
Rect paddle = new Rect(0, 0, 50, 10); | |
int direction = 1; | |
float paddleSpeed = 0.4f; | |
float ballSpeed = 1f; | |
int radius = 5; | |
Rect ball; | |
Rect?[,] bricks; | |
Vector2 topLeft, topRight, bottomLeft, bottomRight; | |
const int rows = 5; | |
int cols = 0; | |
const int brickSize = 30; | |
const int brickPadding = 1; | |
const int ballRadius = 20; | |
Rect board; | |
float ballAngle; | |
// Use this for initialization | |
public void Restart(Rect rect) | |
{ | |
board = rect; | |
paddleTexture = new Texture2D((int)rect.width, (int)rect.height); | |
topLeft = new Vector2(rect.x, rect.y); | |
topRight = new Vector2(rect.x + rect.width, rect.y); | |
bottomLeft = new Vector2(rect.x, rect.y + rect.height); | |
bottomRight = new Vector2(rect.x + rect.width, rect.y + rect.height); | |
var centerLeft = new Vector2(topLeft.x, bottomLeft.y / 2); | |
cols = Mathf.FloorToInt(rect.width / (brickSize + brickPadding)); | |
bricks = new Rect?[rows, cols]; | |
for (var row = 0; row < rows; row++) | |
{ | |
for (var col = 0; col < cols; col++) | |
{ | |
bricks[row, col] = new Rect( | |
centerLeft.x + brickSize * col + brickPadding * col, | |
centerLeft.y + brickSize * row + brickPadding * row, | |
brickSize, brickSize); | |
} | |
} | |
paddle.y = bottomLeft.y - paddle.height; | |
ball = new Rect(paddle.x + paddle.width / 2, paddle.y - ballRadius, ballRadius, ballRadius); | |
ballAngle = 270; | |
} | |
// Update is called once per frame | |
public void Update() | |
{ | |
paddle.x += direction * paddleSpeed; | |
if (paddle.x + paddle.width > topRight.x || paddle.x < topLeft.x) | |
{ | |
direction *= -1; | |
} | |
ball = MoveAngle(ball, ballAngle, ballSpeed); | |
for (var row = 0; row < rows; row++) | |
{ | |
for (var col = 0; col < cols; col++) | |
{ | |
if (bricks[row, col].HasValue) { | |
var brick = bricks[row, col].Value; | |
if (IsCollision(ball, brick)) { | |
bricks[row, col] = null; | |
ballAngle = Mathf.Repeat(ballAngle + 90, 360); | |
} | |
else { | |
GUI.DrawTexture(brick, texture1); | |
} | |
} | |
} | |
} | |
if (ball.x <= topLeft.x || ball.x >= topRight.x) { | |
ballAngle = Mathf.Repeat(ballAngle + 90, 360); | |
} | |
if (IsCollision(paddle, ball)) { | |
ballAngle = Mathf.Repeat(ballAngle + 90, 360); | |
} | |
GUI.DrawTexture(paddle, texture1); | |
GUI.DrawTexture(ball, Styles.SmallLogo); | |
} | |
bool IsCollision(Rect thing1, Rect thing2) { | |
var isInSpace = thing1.y >= thing2.y && thing1.y <= thing2.y + thing2.height; | |
isInSpace &= thing1.x >= thing2.x && thing1.x <= thing2.x + thing2.width; | |
return isInSpace; | |
} | |
Rect MoveAngle(Rect v, float angle, float speed) | |
{ | |
v.x += Mathf.Cos(angle * Mathf.Deg2Rad) * speed; | |
v.y += Mathf.Sin(angle * Mathf.Deg2Rad) * speed; | |
return v; | |
} | |
public void Click(Vector2 position) | |
{ | |
if (position.x > paddle.x) | |
{ | |
// move paddle right | |
direction = 1; | |
} else if (position.x < paddle.x) | |
{ | |
// move paddle left | |
direction = -1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment