mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
pong: NetworkManagerPong handles ball spawn and despawn
This commit is contained in:
parent
7e9b3efa7d
commit
d72a10b4a9
@ -219,7 +219,7 @@ Transform:
|
|||||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_Children: []
|
m_Children: []
|
||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_RootOrder: 4
|
m_RootOrder: 3
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
--- !u!1 &753891880
|
--- !u!1 &753891880
|
||||||
GameObject:
|
GameObject:
|
||||||
@ -327,67 +327,6 @@ BoxCollider2D:
|
|||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
m_Size: {x: 50, y: 1}
|
m_Size: {x: 50, y: 1}
|
||||||
m_EdgeRadius: 0
|
m_EdgeRadius: 0
|
||||||
--- !u!1001 &1140775523
|
|
||||||
PrefabInstance:
|
|
||||||
m_ObjectHideFlags: 0
|
|
||||||
serializedVersion: 2
|
|
||||||
m_Modification:
|
|
||||||
m_TransformParent: {fileID: 0}
|
|
||||||
m_Modifications:
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_LocalPosition.x
|
|
||||||
value: 0
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_LocalPosition.y
|
|
||||||
value: 0
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_LocalPosition.z
|
|
||||||
value: 0
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_LocalRotation.x
|
|
||||||
value: 0
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_LocalRotation.y
|
|
||||||
value: 0
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_LocalRotation.z
|
|
||||||
value: 0
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_LocalRotation.w
|
|
||||||
value: 1
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
propertyPath: m_RootOrder
|
|
||||||
value: 3
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 114290021321007948, guid: 5f7a7f34494ed40268eff49dbf9168bf,
|
|
||||||
type: 3}
|
|
||||||
propertyPath: m_SceneId
|
|
||||||
value: 14060562
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 50354248948880112, guid: 5f7a7f34494ed40268eff49dbf9168bf,
|
|
||||||
type: 3}
|
|
||||||
propertyPath: m_Simulated
|
|
||||||
value: 0
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 50354248948880112, guid: 5f7a7f34494ed40268eff49dbf9168bf,
|
|
||||||
type: 3}
|
|
||||||
propertyPath: m_CollisionDetection
|
|
||||||
value: 1
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
- target: {fileID: 50354248948880112, guid: 5f7a7f34494ed40268eff49dbf9168bf,
|
|
||||||
type: 3}
|
|
||||||
propertyPath: m_Interpolate
|
|
||||||
value: 1
|
|
||||||
objectReference: {fileID: 0}
|
|
||||||
m_RemovedComponents: []
|
|
||||||
m_SourcePrefab: {fileID: 100100000, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
|
||||||
--- !u!1 &1346799726
|
--- !u!1 &1346799726
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@ -728,7 +667,7 @@ Transform:
|
|||||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_Children: []
|
m_Children: []
|
||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_RootOrder: 5
|
m_RootOrder: 4
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
--- !u!1 &1575697329
|
--- !u!1 &1575697329
|
||||||
GameObject:
|
GameObject:
|
||||||
@ -945,9 +884,12 @@ MonoBehaviour:
|
|||||||
type: 3}
|
type: 3}
|
||||||
autoCreatePlayer: 1
|
autoCreatePlayer: 1
|
||||||
playerSpawnMethod: 1
|
playerSpawnMethod: 1
|
||||||
spawnPrefabs: []
|
spawnPrefabs:
|
||||||
|
- {fileID: 1080679924113744, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
||||||
leftRacketSpawn: {fileID: 473997961}
|
leftRacketSpawn: {fileID: 473997961}
|
||||||
rightRacketSpawn: {fileID: 1397990096}
|
rightRacketSpawn: {fileID: 1397990096}
|
||||||
|
ballPrefab: {fileID: 1080679924113744, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
|
||||||
|
ball: {fileID: 0}
|
||||||
--- !u!114 &1886246553
|
--- !u!114 &1886246553
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
@ -10,24 +10,7 @@ public override void OnStartServer()
|
|||||||
{
|
{
|
||||||
// only simulate ball physics on server
|
// only simulate ball physics on server
|
||||||
GetComponent<Rigidbody2D>().simulated = true;
|
GetComponent<Rigidbody2D>().simulated = true;
|
||||||
}
|
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
|
||||||
|
|
||||||
[ServerCallback]
|
|
||||||
void Update()
|
|
||||||
{
|
|
||||||
Rigidbody2D rb = GetComponent<Rigidbody2D>();
|
|
||||||
|
|
||||||
// if less than two players then reset ball
|
|
||||||
if (NetworkManager.singleton.numPlayers < 2)
|
|
||||||
{
|
|
||||||
rb.velocity = Vector2.zero;
|
|
||||||
transform.position = Vector2.zero;
|
|
||||||
}
|
|
||||||
// if two players joined, then enable velocity if not done yet
|
|
||||||
else if (NetworkManager.singleton.numPlayers == 2 && rb.velocity == Vector2.zero)
|
|
||||||
{
|
|
||||||
rb.velocity = Vector2.right * speed;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
|
float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
|
||||||
|
@ -8,11 +8,31 @@ public class NetworkManagerPong : NetworkManager
|
|||||||
{
|
{
|
||||||
public Transform leftRacketSpawn;
|
public Transform leftRacketSpawn;
|
||||||
public Transform rightRacketSpawn;
|
public Transform rightRacketSpawn;
|
||||||
|
public GameObject ballPrefab;
|
||||||
|
public GameObject ball;
|
||||||
|
|
||||||
public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
|
public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
|
||||||
{
|
{
|
||||||
|
// add player at correct spawn position
|
||||||
Transform start = numPlayers == 0 ? leftRacketSpawn : rightRacketSpawn;
|
Transform start = numPlayers == 0 ? leftRacketSpawn : rightRacketSpawn;
|
||||||
GameObject player = Instantiate(playerPrefab, start.position, start.rotation);
|
GameObject player = Instantiate(playerPrefab, start.position, start.rotation);
|
||||||
NetworkServer.AddPlayerForConnection(conn, player);
|
NetworkServer.AddPlayerForConnection(conn, player);
|
||||||
|
|
||||||
|
// spawn ball if two players
|
||||||
|
if (numPlayers == 2)
|
||||||
|
{
|
||||||
|
ball = Instantiate(ballPrefab);
|
||||||
|
NetworkServer.Spawn(ball);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnServerDisconnect(NetworkConnection conn)
|
||||||
|
{
|
||||||
|
// destroy ball
|
||||||
|
if (ball != null)
|
||||||
|
NetworkServer.Destroy(ball);
|
||||||
|
|
||||||
|
// call base functionality (actually destroys the player)
|
||||||
|
base.OnServerDisconnect(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user