Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emmanuel_Delgado: PongRL Week 1 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Assets/Source/Scripts/Pong/Ball/PongBall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public partial class PongBall {
private readonly Action OnScore;
private readonly Action OnRebound;

/// <summary>
/// constructor for the ball object and give the ball object the logic if how the ball will react when a player
/// scores a goal or how the ball should change its velocity vector when it rebounds the paddle or boundary of the viewport
/// </summary>
/// <param name="sprite"></param>
public PongBall(GameObject sprite) {
OnScore = () => {
//* Attacker Scored Goal
Expand Down Expand Up @@ -68,6 +73,10 @@ public static PongBall FromPrefab(GameObject prefab) {
return pongBall;
}

/// <summary>
/// initialize the ball and let a player serve the ball so it heads toward opponnet
/// </summary>
/// <param name="server"></param>
public void Initialize(Player server) {
// First, reset the ball (just in case)
Reset();
Expand Down Expand Up @@ -134,11 +143,18 @@ public void Update() {
//TODO: feed to players?
}


/// <summary>
/// destroys the ball when a player scores so it doesn't go off the screen
/// </summary>
public void DestroyBall() {
ballSprite.gameObj.SetActive(false);
ballSprite.controller.HaltTrajectory(); // stop the ball from going off the screen
}

/// <summary>
/// reset the position of the ball
/// </summary>
public void Reset() {
// set position to start position
ballSprite.transform.localPosition = GetStartLocalPosition();
Expand All @@ -147,18 +163,29 @@ public void Reset() {
ballSprite.gameObj.SetActive(true);
}

/// <summary>
/// set the attacker for who hit the ball last so goals are scored properly?
/// </summary>
/// <param name="atkr"></param>
private void SetAttacker(Player atkr) {
attacker = atkr;

// Now that the attacker has been set, the ball will be headed towards the "rebounder", or in other words, the other player
ballSprite.controller.Rebounder = attacker.Opponent.AsRebounder();
}

/// <summary>
/// update the ball to swap the attacker
/// </summary>
private void SwapAttacker() {
SetAttacker(attacker.Opponent);
attackerDesire = !attackerDesire;
}

/// <summary>
/// set the dimmensions of the ball?
/// </summary>
/// <param name="viewportY"></param>
public void SetLocalScaleFromVPY(float viewportY) {
Vector3 bgScale = BG_TRANSFORM.localScale;

Expand All @@ -171,6 +198,12 @@ public void SetLocalScaleFromVPY(float viewportY) {
//Debug.Log("LocalScale: " + sprite.transform.localScale);
}

/// <summary>
/// sets the starting position for the ball, center of screen
/// </summary>
/// <returns>
/// returns the starting position of the ball
/// </returns>
public static Vector3 GetStartLocalPosition() {
return ToLocal(GameConstants.BALL_START_POSITION);
}
Expand Down
15 changes: 13 additions & 2 deletions Assets/Source/Scripts/Pong/Ball/_Pong-Ball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ namespace Pong.Ball {
* Player lastTouchedBy
* Destroys the ball and increments points
*/

/// <summary>
/// partial class to be later fully defined. Controls how the ball will behave when a player scores a goal
/// or the ball rebounds off a paddle. May also include how to update the balls postion
/// </summary>
public partial class PongBall {}

/*
* Handle collisions -> adjust trajectory
speedX = GameCache.BALL_SPEED_VP
velocityY = ForceAdjustment => Equation (due to possible derivatives)
velocityY = ForceAdjustment => Equation (due to possible derivatives)
*/

/// <summary>
/// define the parital class to be later defined for how the ball will behave
/// </summary>
public partial class PongBallController : MonoBehaviour {}

/*public static class BallStatus {
Expand All @@ -36,7 +45,9 @@ public static bool IsGoal(int ballStatus) {
return Mathf.Abs(ballStatus) == GOAL;
}
}*/

/// <summary>
/// store who socred the last goal
/// </summary>
public static class BallGoal {
public const bool LEFT = true;
public const bool RIGHT = false;
Expand Down
22 changes: 20 additions & 2 deletions Assets/Source/Scripts/Pong/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public partial class GameManager : MonoBehaviour

// CONTEXT: public => reference in the Unity Editor
public string player1Name = PlayerData.NO_NAME, player2Name = PlayerData.NO_NAME;
public float playerSpeedVP = 1.00f; // per second; travel 100% vertical screen size in one second
public float ballSpeedVP = 0.45f; // per second; travel 45% horizontal screen size in one second
public float playerSpeedVP = 1.0f; // per second; travel 100% vertical screen size in one second
public float ballSpeedVP = .45f; // per second; travel 45% horizontal screen size in one second
public float ballServeMaxAngle = (3f / 7f) * Mathf.PI;
public float ballBounceMaxAngle = (3f / 7f) * Mathf.PI;
public uint scoreToWin = GameConstants.DEFAULT_WIN_SCORE;
Expand All @@ -29,6 +29,9 @@ public partial class GameManager : MonoBehaviour
public GameObject backgroundSprite; // reference a GameObject in the Scene
public TMP_Text player1scoreText, player2scoreText; // reference in Scene

/// <summary>
/// for debugging purposes?
/// </summary>
void Awake()
{
// Hello World message
Expand All @@ -38,6 +41,10 @@ void Awake()
//Debug.Log(GameConstants.LEFT_PADDLE_START_POSITION);
}

/// <summary>
/// starts the game and stores important data in GameCache.{attribute}. Initialize the player and give the player data.
/// Initialize the ball object and serve it so gameplay can begin
/// </summary>
void Start()
{
// Cache Desired Global Variables
Expand Down Expand Up @@ -65,6 +72,11 @@ void Start()
}

// Update is called once per frame
/// <summary>
/// update the player information and the all information every frame.
/// update players first so you don't have instances where the ball seemed like it was in one place but then the ball was
/// updated before the player so the player missed it. This can help solve this issue
/// </summary>
void Update()
{
// Player Updates
Expand All @@ -75,6 +87,12 @@ void Update()
ball.Update(); // didn't call this before the player updates for a better user experience
}

/// <summary>
/// display the score board
/// </summary>
/// <returns>
/// return as string that contains both players score so it can be displayed
/// </returns>
public string GetCurrentScore() {
return player1.GetScoreboard().GetScore() + "-" + player2.GetScoreboard().GetScore();
}
Expand Down
20 changes: 20 additions & 0 deletions Assets/Source/Scripts/Pong/GamePlayer/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public partial class Player {
private Player opponent;

// load from data
/// <summary>
/// Intialize a player with their data, how to control the player, and give the paddles a boundary box so
/// the ball can bounce off it
/// </summary>
/// <param name="playerData"></param>
/// <param name="sprite"></param>
/// <param name="controls"></param>
/// <param name="scoreboard"></param>
public Player(PlayerData playerData, GameObject sprite, PlayerControls controls, Scoreboard scoreboard) {
this.playerData = playerData;
this.scoreboard = scoreboard;
Expand Down Expand Up @@ -85,13 +93,20 @@ public Player Opponent {
set { opponent = value; }
}

/// <summary>
/// update the information of the player's padldle. For example update the velocity and acceleration when the player
/// wants to switch the direction of the paddle of the player no long is moving the paddle
/// </summary>
public void Update() {
forceMap.PaddleVelocity = ToLocal(playerSprite.controller.GetViewportMotionTracker().velocity).y;
forceMap.PaddleAcceleration = ToLocal(new Vector2(0f, playerSprite.controller.GetViewportMotionTracker().Y_Acceleration)).y;

//TODO: playerData.feed(...);
}

/// <summary>
/// update the player's score when they socre a goal
/// </summary>
public void ScorePoint() {
// Game: score point
scoreboard.ScorePoint();
Expand All @@ -105,6 +120,11 @@ public Rebounder AsRebounder() {
return new Rebounder(forceMap, playerSprite.gameObj.GetComponent<RectangularBodyFrame>());
}

/// <summary>
/// set the paddles dimensions base off percentages of the viewport
/// </summary>
/// <param name="vpXThickness"></param>
/// <param name="vpYLength"></param>
public void SetLocalPaddleDimensionsFromVP(float vpXThickness, float vpYLength) {
Vector3 bgScale = GameCache.BG_TRANSFORM.localScale;

Expand Down
14 changes: 12 additions & 2 deletions Assets/Source/Scripts/Pong/GamePlayer/_Pong-GamePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
using UnityEngine;

namespace Pong.GamePlayer {
/// <summary>
/// future class to be defined of how player information/actoins will be stored
/// </summary>
public partial class Player {}

/// <summary>
/// playerData class derived from ScriptableObjet to be later deifined
/// </summary>
public partial class PlayerData : ScriptableObject {}
//public partial class AIPlayerData : PlayerData {}

/// <summary>
/// partial class on how the player will be controlled
/// </summary>
public partial class PlayerController : MonoBehaviour {}

/// <summary>
/// class stores keys players will use to control their paddle
/// </summary>
public class PlayerControls {
public readonly KeyCode Up, Down;
public PlayerControls(KeyCode up, KeyCode down) {
Expand Down
11 changes: 9 additions & 2 deletions Assets/Source/Scripts/Pong/_Pong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using Pong.GamePlayer;

namespace Pong {
/// <summary>
/// defines global game constants that will be used for the player, ball, and win conditions
/// </summary>
public static class GameConstants {
// must be >= 1 because velocity is required
public const uint BALL_Y_MAX_DERIVATIVE = 3; // velocity + (acceleration, acceleration')
Expand Down Expand Up @@ -39,7 +42,9 @@ public static class GameConstants {

public static readonly char[] WINDOWS_BANNED_CHARS = {'\\', '/', ':', '*', '?', '\"', '<', '>', '|'};
}

/// <summary>
/// stores the values of the game that other functions will call when initialzing the player and ball
/// </summary>
public static class GameCache {
// cached at the beginning of GameManager
public static Transform BG_TRANSFORM;
Expand All @@ -54,7 +59,9 @@ public static class GameCache {
// A single hotkey (likely M) will mute the sounds
public static bool MUTE_SOUNDS = false; // when turned on, audio won't be played
}

/// <summary>
/// vectors used to update the player's and ball's position?
/// </summary>
public static class GameHelpers {
public static Vector2 ToVector2(Vector3 vector) {
return new Vector2(vector.x, vector.y);
Expand Down