version 1.3
This commit is contained in:
67
Assets/Ignorance/Demo/PongChamp/Scripts/AtariPongBall.cs
Normal file
67
Assets/Ignorance/Demo/PongChamp/Scripts/AtariPongBall.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
namespace Ignorance.Examples.PongChamp
|
||||
{
|
||||
public class AtariPongBall : NetworkBehaviour
|
||||
{
|
||||
public float speed = 100;
|
||||
private Rigidbody2D rigidbody2d;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
rigidbody2d = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
base.OnStartServer();
|
||||
|
||||
// only simulate ball physics on server
|
||||
rigidbody2d.simulated = true;
|
||||
|
||||
// Serve the ball from left player
|
||||
rigidbody2d.velocity = Vector2.right * speed;
|
||||
}
|
||||
|
||||
float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
|
||||
{
|
||||
// ascii art:
|
||||
// || 1 <- at the top of the racket
|
||||
// ||
|
||||
// || 0 <- at the middle of the racket
|
||||
// ||
|
||||
// || -1 <- at the bottom of the racket
|
||||
return (ballPos.y - racketPos.y) / racketHeight;
|
||||
}
|
||||
|
||||
// only call this on server
|
||||
[ServerCallback]
|
||||
void OnCollisionEnter2D(Collision2D col)
|
||||
{
|
||||
// Note: 'col' holds the collision information. If the
|
||||
// Ball collided with a racket, then:
|
||||
// col.gameObject is the racket
|
||||
// col.transform.position is the racket's position
|
||||
// col.collider is the racket's collider
|
||||
|
||||
// did we hit a racket? then we need to calculate the hit factor
|
||||
if (col.transform.GetComponent<AtariPongBall>())
|
||||
{
|
||||
// Calculate y direction via hit Factor
|
||||
float y = HitFactor(transform.position,
|
||||
col.transform.position,
|
||||
col.collider.bounds.size.y);
|
||||
|
||||
// Calculate x direction via opposite collision
|
||||
float x = col.relativeVelocity.x > 0 ? 1 : -1;
|
||||
|
||||
// Calculate direction, make length=1 via .normalized
|
||||
Vector2 dir = new Vector2(x, y).normalized;
|
||||
|
||||
// Set Velocity with dir * speed
|
||||
rigidbody2d.velocity = dir * speed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a08d5ab1f59230458264367f00c54d8
|
||||
timeCreated: 1426602353
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Assets/Ignorance/Demo/PongChamp/Scripts/AtariPongRacket.cs
Normal file
25
Assets/Ignorance/Demo/PongChamp/Scripts/AtariPongRacket.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
namespace Ignorance.Examples.PongChamp
|
||||
{
|
||||
public class AtariPongRacket : NetworkBehaviour
|
||||
{
|
||||
public float speed = 1500;
|
||||
private Rigidbody2D rigidbody2d;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
rigidbody2d = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
// need to use FixedUpdate for rigidbody
|
||||
void FixedUpdate()
|
||||
{
|
||||
// only let the local player control the racket.
|
||||
// don't control other player's rackets
|
||||
if (isLocalPlayer)
|
||||
rigidbody2d.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b68eb08343b29a54dbd5ed7830fb9211
|
||||
timeCreated: 1426597826
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
49
Assets/Ignorance/Demo/PongChamp/Scripts/OnlineTimer.cs
Normal file
49
Assets/Ignorance/Demo/PongChamp/Scripts/OnlineTimer.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
using System.Diagnostics;
|
||||
using Debug = UnityEngine.Debug;
|
||||
|
||||
public class OnlineTimer : NetworkBehaviour
|
||||
{
|
||||
private Stopwatch stopwatch;
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Awake()
|
||||
{
|
||||
stopwatch = new Stopwatch();
|
||||
}
|
||||
|
||||
public override void OnStartClient()
|
||||
{
|
||||
stopwatch.Reset();
|
||||
stopwatch.Start();
|
||||
|
||||
Debug.Log("Stopwatch started!");
|
||||
|
||||
base.OnStartClient();
|
||||
}
|
||||
|
||||
public void OnDisable()
|
||||
{
|
||||
if(stopwatch.IsRunning)
|
||||
{
|
||||
System.TimeSpan ts = stopwatch.Elapsed;
|
||||
stopwatch.Stop();
|
||||
|
||||
Debug.Log("Stopwatch stopped: duration " + string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
|
||||
ts.Hours, ts.Minutes, ts.Seconds,
|
||||
ts.Milliseconds / 10));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (!stopwatch.IsRunning) return;
|
||||
|
||||
GUI.Box(new Rect(new Vector2(2, Screen.height - 36), new Vector2(320, 32)), "ONLINE TIME: " + string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
|
||||
stopwatch.Elapsed.Hours, stopwatch.Elapsed.Minutes, stopwatch.Elapsed.Seconds,
|
||||
stopwatch.Elapsed.Milliseconds / 10));
|
||||
}
|
||||
}
|
||||
11
Assets/Ignorance/Demo/PongChamp/Scripts/OnlineTimer.cs.meta
Normal file
11
Assets/Ignorance/Demo/PongChamp/Scripts/OnlineTimer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d0996ec573094c24890a4d4233ee871e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user