From 1fa6f56408c8e8124eed7e9a33c0045a5dfcdad6 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Thu, 6 Apr 2023 21:57:50 +0200 Subject: [PATCH] Basic AI with no function --- Assets/Prefabs/Player.prefab | 95 ++++++++------------- Assets/Scenes/Game.unity | 125 +--------------------------- Assets/Scripts/BorderSize.cs | 3 + Assets/Scripts/Game/GameManager.cs | 6 ++ Assets/Scripts/Game/Modification.cs | 15 ++++ Assets/Scripts/Game/Player.cs | 84 ++++++++++++++++--- 6 files changed, 136 insertions(+), 192 deletions(-) create mode 100644 Assets/Scripts/Game/Modification.cs diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index d314fa8..213cdcb 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -12,7 +12,6 @@ GameObject: - component: {fileID: 5402279313309450414} - component: {fileID: 5402279313309450412} - component: {fileID: 1666507220592599477} - - component: {fileID: 1287955658} - component: {fileID: 1287955657} m_Layer: 0 m_Name: Player @@ -139,65 +138,43 @@ PolygonCollider2D: m_AutoTiling: 0 m_Points: m_Paths: - - - {x: 0.07769512, y: 0.48959255} - - {x: -0.0012524575, y: 0.49665928} - - {x: -0.07263627, y: 0.49400043} - - {x: -0.15234375, y: 0.47265625} - - {x: -0.22255954, y: 0.44318295} - - {x: -0.29296875, y: 0.40234375} - - {x: -0.34777647, y: 0.3533802} - - {x: -0.40234375, y: 0.29296875} - - {x: -0.4367497, y: 0.23835087} - - {x: -0.47265625, y: 0.15234375} - - {x: -0.48305663, y: 0.11670399} - - {x: -0.49476454, y: 0.050439835} - - {x: -0.49564582, y: 0.020850182} - - {x: -0.49582514, y: -0.020039558} - - {x: -0.4916248, y: -0.06787586} - - {x: -0.48282516, y: -0.11522102} - - {x: -0.47265625, y: -0.15234375} - - {x: -0.44339067, y: -0.22507} - - {x: -0.40234375, y: -0.29296875} - - {x: -0.3491057, y: -0.3549738} - - {x: -0.29296875, y: -0.40234375} - - {x: -0.22167481, y: -0.4452963} - - {x: -0.15234375, y: -0.47265625} - - {x: -0.07007347, y: -0.49577236} + - - {x: 0.18382387, y: -0.4653309} + - {x: 0.3455563, y: -0.35962877} + - {x: 0.43450892, y: -0.23867823} + - {x: 0.4880277, y: -0.10583779} + - {x: 0.496528, y: -0.05821988} + - {x: 0.5, y: 0} + - {x: 0.49400452, y: 0.06542152} + - {x: 0.49400452, y: 0.06542152} + - {x: 0.48730403, y: 0.1032284} + - {x: 0.4831206, y: 0.12148502} + - {x: 0.4726707, y: 0.15627386} + - {x: 0.44537786, y: 0.21957007} + - {x: 0.4052527, y: 0.28797257} + - {x: 0.36020362, y: 0.3420072} + - {x: 0.28025484, y: 0.4060052} + - {x: 0.20846313, y: 0.44862068} + - {x: 0.11702546, y: 0.48358205} + - {x: 0.059628937, y: 0.4899849} + - {x: 0, y: 0.5} + - {x: -0.11932226, y: 0.48170108} + - {x: -0.1808045, y: 0.46216008} + - {x: -0.2496787, y: 0.4281767} + - {x: -0.3099577, y: 0.38689625} + - {x: -0.37254146, y: 0.32798705} + - {x: -0.411548, y: 0.28249228} + - {x: -0.461751, y: 0.19125547} + - {x: -0.488247, y: 0.10687556} + - {x: -0.4957525, y: 0.062132556} + - {x: -0.5, y: 0} + - {x: -0.48705873, y: -0.11048966} + - {x: -0.465915, y: -0.18640542} + - {x: -0.4403743, y: -0.2305677} + - {x: -0.42426294, y: -0.26128066} + - {x: -0.35971022, y: -0.3503273} + - {x: -0.2509045, y: -0.42578417} + - {x: -0.14749898, y: -0.4756718} - {x: 0, y: -0.5} - - {x: 0.08312563, y: -0.4915352} - - {x: 0.15234375, y: -0.47265625} - - {x: 0.22586307, y: -0.44487286} - - {x: 0.29296875, y: -0.40234375} - - {x: 0.3528148, y: -0.35042953} - - {x: 0.40234375, y: -0.29296875} - - {x: 0.43940634, y: -0.23136902} - - {x: 0.45904493, y: -0.18834782} - - {x: 0.47265625, y: -0.15234375} - - {x: 0.48268673, y: -0.11542511} - - {x: 0.49256375, y: -0.06293583} - - {x: 0.49749509, y: 0.0016698837} - - {x: 0.49538845, y: 0.04973793} - - {x: 0.4866397, y: 0.106570244} - - {x: 0.47265625, y: 0.15234375} - - {x: 0.45948133, y: 0.19455719} - - {x: 0.4361129, y: 0.24171734} - - {x: 0.40234375, y: 0.29296875} - - {x: 0.3540154, y: 0.34922886} - - {x: 0.29296875, y: 0.40234375} - - {x: 0.23607399, y: 0.43893242} - - {x: 0.15234375, y: 0.47265625} ---- !u!114 &1287955658 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 5402279313309450415} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: b045411c168541488d756b99a27aab04, type: 3} - m_Name: - m_EditorClassIdentifier: --- !u!114 &1287955657 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index e30422f..18f4a8d 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -345,67 +345,6 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1001 &844814161 -PrefabInstance: - m_ObjectHideFlags: 0 - serializedVersion: 2 - m_Modification: - m_TransformParent: {fileID: 0} - m_Modifications: - - target: {fileID: 1287955657, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: GlobalObjectIdHash - value: 1155120388 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_RootOrder - value: 6 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalPosition.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalPosition.y - value: -15 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalPosition.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.w - value: 1 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.y - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalEulerAnglesHint.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalEulerAnglesHint.y - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalEulerAnglesHint.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450415, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_Name - value: Player - objectReference: {fileID: 0} - m_RemovedComponents: [] - m_SourcePrefab: {fileID: 100100000, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} --- !u!1 &948416250 GameObject: m_ObjectHideFlags: 0 @@ -693,7 +632,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 5 + orthographic size: 20 m_Depth: 0 m_CullingMask: serializedVersion: 2 @@ -724,67 +663,6 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1001 &1740903482 -PrefabInstance: - m_ObjectHideFlags: 0 - serializedVersion: 2 - m_Modification: - m_TransformParent: {fileID: 0} - m_Modifications: - - target: {fileID: 1287955657, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: GlobalObjectIdHash - value: 1663989227 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_RootOrder - value: 7 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalPosition.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalPosition.y - value: 15 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalPosition.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.w - value: 1 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.y - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalRotation.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalEulerAnglesHint.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalEulerAnglesHint.y - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450413, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_LocalEulerAnglesHint.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 5402279313309450415, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} - propertyPath: m_Name - value: Player (1) - objectReference: {fileID: 0} - m_RemovedComponents: [] - m_SourcePrefab: {fileID: 100100000, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} --- !u!1 &2069256770 GameObject: m_ObjectHideFlags: 0 @@ -870,6 +748,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ballPrefab: {fileID: 7041796185663956342, guid: d67d5f2db5f0b3d9d8298731f423a9b6, type: 3} + playerPrefab: {fileID: 5402279313309450415, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} --- !u!4 &2109945868 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/BorderSize.cs b/Assets/Scripts/BorderSize.cs index 895cce2..8f573c5 100644 --- a/Assets/Scripts/BorderSize.cs +++ b/Assets/Scripts/BorderSize.cs @@ -8,8 +8,11 @@ public class BorderSize : MonoBehaviour { private EdgeCollider2D top, bottom, left, right; public Transform playGround; + + public static BorderSize Singleton; private void OnEnable() { + Singleton = this; top = transform.Find("Top").GetComponent(); bottom = transform.Find("Bottom").GetComponent(); left = transform.Find("Left").GetComponent(); diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index b52d6fb..4f60d76 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -8,12 +8,18 @@ namespace Game { public class GameManager : NetworkBehaviour { public Object ballPrefab; + public Object playerPrefab; private void Start() { //var ball = Instantiate(ballPrefab, Vector2.zero, Quaternion.identity).GetComponent(); var ball = FindObjectOfType(typeof(Ball)); var rb = ball.GetComponent(); rb.velocity = new Vector2(0, 10); + + var p1 = Instantiate(playerPrefab, new Vector2(0, BorderSize.Singleton.y1), Quaternion.identity); + var p2 = Instantiate(playerPrefab, new Vector2(0, BorderSize.Singleton.y2), Quaternion.identity); + p1.AddComponent(); + p2.AddComponent(); } } diff --git a/Assets/Scripts/Game/Modification.cs b/Assets/Scripts/Game/Modification.cs new file mode 100644 index 0000000..becf739 --- /dev/null +++ b/Assets/Scripts/Game/Modification.cs @@ -0,0 +1,15 @@ +using UnityEngine; + +namespace Game { + public class Modification : MonoBehaviour { + + } + + public class SpeedModification : Modification { + + } + + public class BorderModification : Modification { + + } +} diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index 9103ef5..23f1bc0 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -2,22 +2,86 @@ using System; using Unity.Netcode; using UnityEngine; using UnityEngine.InputSystem; +using UnityEngine.PlayerLoop; +using Random = UnityEngine.Random; namespace Game { public class Player : NetworkBehaviour { - private void FixedUpdate() { - Vector2 add = Vector2.zero; + + private int score; + + protected bool GoingLeft, GoingRight; + + // Units per second + private float baseSpeed = 10; + + // Unit distance from zero + private float baseBorder = 7; + + private SpeedModification speedModification; + private BorderModification borderModification; + + private float GetSpeed() { + return baseSpeed; + } + + private float GetBorder() { + return baseBorder; + } + + private float LeftSide() { + return X() - transform.localScale.x / 2; + } + + private float RightSide() { + return X() + transform.localScale.x / 2; + } + + protected float X() { + return transform.position.x; + } + + protected void TryMove(float h) { + Vector2 trans = new Vector2((GoingLeft ? -1 : 0) + (GoingRight ? 1 : 0), 0); + trans *= baseSpeed * h; + transform.Translate(trans); + Debug.Log(trans.magnitude); + if (LeftSide() < -baseBorder) + transform.Translate(Vector2.right * (-baseBorder - LeftSide())); + if (RightSide() > baseBorder) + transform.Translate(Vector2.left * (RightSide() - baseBorder)); + } + } + + public class AIPlayer : Player { + public enum Difficulty { + VeryEasy, Easy, Medium, Hard, VeryHard + } + + private Difficulty difficulty = Difficulty.VeryEasy; + + private float GetTargetPosition() { - var keyboard = Keyboard.current; - if (keyboard.aKey.isPressed) { - add.x -= 1; - } + + return 0; + } - if (keyboard.dKey.isPressed) { - add.x += 1; - } + private void FixedUpdate() { + float target = GetTargetPosition(); + const float h = 1; + GoingLeft = target < X() - h; + GoingRight = target > X() + h; + TryMove(Time.fixedDeltaTime); + } + } + + public class RealPlayer : Player { + private void FixedUpdate() { + var keyboard = Keyboard.current; + GoingLeft = keyboard.aKey.isPressed; + GoingRight = keyboard.dKey.isPressed; - transform.Translate(add / 10f); + TryMove(Time.fixedDeltaTime); } } }