117 lines
4.1 KiB
C#
117 lines
4.1 KiB
C#
![]() |
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using System.Net;
|
|||
|
using System;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Text;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
public class DataSender : MonoBehaviour
|
|||
|
{
|
|||
|
Socket sock;
|
|||
|
Socket avatarSock;
|
|||
|
|
|||
|
IPAddress serverAddr;
|
|||
|
EndPoint serverEndPoint;
|
|||
|
EndPoint endPoint;
|
|||
|
|
|||
|
byte[] bufRecv = new byte[30];
|
|||
|
|
|||
|
public GameObject enemy;
|
|||
|
|
|||
|
public GameObject ball;
|
|||
|
|
|||
|
public Canvas canvas;
|
|||
|
|
|||
|
public Text myCounter;
|
|||
|
public Text enemyCounter;
|
|||
|
|
|||
|
[HideInInspector]
|
|||
|
public bool isHit = false;
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
|
|||
|
byte[] bufPort = new byte[8];
|
|||
|
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|||
|
|
|||
|
serverAddr = canvas.GetComponent<GetServerIp>().GetIP();
|
|||
|
serverEndPoint = new IPEndPoint(serverAddr, 8080);
|
|||
|
IPEndPoint endPointIP = new IPEndPoint(IPAddress.Any, 0);
|
|||
|
endPoint = (EndPoint)endPointIP;
|
|||
|
|
|||
|
sock.SendTo(Encoding.ASCII.GetBytes("UNITY"), serverEndPoint);
|
|||
|
sock.ReceiveFrom(bufPort, ref endPoint);
|
|||
|
serverEndPoint = new IPEndPoint(serverAddr, int.Parse(Encoding.UTF8.GetString(bufPort)));
|
|||
|
|
|||
|
sock.ReceiveTimeout = 1000;
|
|||
|
sock.SendTimeout = 1000;
|
|||
|
}
|
|||
|
|
|||
|
void OnCollisionEnter(Collision collider)
|
|||
|
{
|
|||
|
if (collider.collider.name.Contains("Ball"))
|
|||
|
{
|
|||
|
isHit = true;
|
|||
|
Destroy(collider.gameObject);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void Update()
|
|||
|
{
|
|||
|
byte[] buf = new byte[30];
|
|||
|
Buffer.BlockCopy(BitConverter.GetBytes(transform.position.x), 0, buf, 0 * sizeof(float), sizeof(float));
|
|||
|
Buffer.BlockCopy(BitConverter.GetBytes(transform.position.y), 0, buf, 1 * sizeof(float), sizeof(float));
|
|||
|
Buffer.BlockCopy(BitConverter.GetBytes(transform.position.z), 0, buf, 2 * sizeof(float), sizeof(float));
|
|||
|
|
|||
|
Buffer.BlockCopy(BitConverter.GetBytes(transform.rotation.x), 0, buf, 3 * sizeof(float), sizeof(float));
|
|||
|
Buffer.BlockCopy(BitConverter.GetBytes(transform.rotation.y), 0, buf, 4 * sizeof(float), sizeof(float));
|
|||
|
Buffer.BlockCopy(BitConverter.GetBytes(transform.rotation.z), 0, buf, 5 * sizeof(float), sizeof(float));
|
|||
|
Buffer.BlockCopy(BitConverter.GetBytes(transform.rotation.w), 0, buf, 6 * sizeof(float), sizeof(float));
|
|||
|
if (Input.GetKeyDown(KeyCode.Space))
|
|||
|
{
|
|||
|
Vector3 myPos = gameObject.transform.position;
|
|||
|
GameObject mySpawnedBall = Instantiate(ball, myPos + Vector3.forward, Quaternion.identity);
|
|||
|
mySpawnedBall.GetComponent<Rigidbody>().AddForce(80f * Vector3.forward);
|
|||
|
|
|||
|
buf[28] = (byte)'S';
|
|||
|
}
|
|||
|
|
|||
|
else buf[28] = (byte)'N';
|
|||
|
|
|||
|
if (isHit) {
|
|||
|
buf[29] = (byte)'H';
|
|||
|
enemyCounter.text = (int.Parse(enemyCounter.text) + 1).ToString();
|
|||
|
}
|
|||
|
|
|||
|
else buf[29] = (byte)'N';
|
|||
|
isHit = false;
|
|||
|
|
|||
|
sock.SendTo(buf, serverEndPoint);
|
|||
|
|
|||
|
sock.ReceiveFrom(bufRecv, ref endPoint);
|
|||
|
Vector3 pos = Vector3.zero;
|
|||
|
Quaternion rot = Quaternion.identity;
|
|||
|
pos.x = BitConverter.ToSingle(bufRecv, 0 * sizeof(float));
|
|||
|
pos.y = BitConverter.ToSingle(bufRecv, 1 * sizeof(float));
|
|||
|
pos.z = BitConverter.ToSingle(bufRecv, 2 * sizeof(float));
|
|||
|
|
|||
|
rot.x = BitConverter.ToSingle(bufRecv, 3 * sizeof(float));
|
|||
|
rot.y = BitConverter.ToSingle(bufRecv, 4 * sizeof(float));
|
|||
|
rot.z = BitConverter.ToSingle(bufRecv, 5 * sizeof(float));
|
|||
|
rot.w = BitConverter.ToSingle(bufRecv, 6 * sizeof(float));
|
|||
|
if (bufRecv[28] == (byte)'S')
|
|||
|
{
|
|||
|
Vector3 posEnemy = enemy.transform.position;
|
|||
|
GameObject enemySpawnedBall = Instantiate(ball, posEnemy + Vector3.forward, Quaternion.identity);
|
|||
|
enemySpawnedBall.GetComponent<Rigidbody>().AddForce(80f * Vector3.forward);
|
|||
|
}
|
|||
|
if (bufRecv[29] == (byte)'H') myCounter.text = (int.Parse(myCounter.text) + 1).ToString();
|
|||
|
|
|||
|
enemy.transform.position = pos;
|
|||
|
enemy.transform.rotation = rot;
|
|||
|
}
|
|||
|
}
|