Bug Fixing

Es können keine blöcke mehr "in" einen selbst oder in andere spieler gebaut werden
This commit is contained in:
brausjonas
2022-05-13 18:00:50 +02:00
parent 04e06652f5
commit 7ccaebaf06
5 changed files with 93 additions and 10 deletions
@@ -32,7 +32,7 @@ TextureImporter:
maxTextureSize: 2048 maxTextureSize: 2048
textureSettings: textureSettings:
serializedVersion: 2 serializedVersion: 2
filterMode: 1 filterMode: 0
aniso: 1 aniso: 1
mipBias: 0 mipBias: 0
wrapU: 0 wrapU: 0
@@ -75,6 +75,18 @@ TextureImporter:
overridden: 0 overridden: 0
androidETC2FallbackOverride: 0 androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0 forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet: spriteSheet:
serializedVersion: 2 serializedVersion: 2
sprites: [] sprites: []
@@ -184,11 +184,13 @@ public class Client
world.EditBlock(chunk, positionInChunk, blockID); world.EditBlock(chunk, positionInChunk, blockID);
} }
//add a new player
if (data[0] == 5) if (data[0] == 5)
{ {
world.AddPlayer(data[1]); world.AddPlayer(data[1]);
} }
//update a players position
if (data[0] == 7) if (data[0] == 7)
{ {
byte[] positionASCII = new byte[data.Length - 2]; byte[] positionASCII = new byte[data.Length - 2];
@@ -207,11 +209,13 @@ public class Client
world.UpdatePlayerPosition(new Vector3(x, y, z), id); world.UpdatePlayerPosition(new Vector3(x, y, z), id);
} }
//remove a player from local world
if (data[0] == 4) if (data[0] == 4)
{ {
world.RemovePlayer(data[1]); world.RemovePlayer(data[1]);
} }
//update a players rotation
if (data[0] == 8) if (data[0] == 8)
{ {
byte[] rotationASCII = new byte[data.Length - 2]; byte[] rotationASCII = new byte[data.Length - 2];
@@ -234,6 +238,7 @@ public class Client
} }
//sends an edit block request to server
public void EditBlock(Vector3 positionInWorld, byte blockID) public void EditBlock(Vector3 positionInWorld, byte blockID)
{ {
ChunkCoord chunk = new ChunkCoord((int)(positionInWorld.x / Data.chunkWidth), ChunkCoord chunk = new ChunkCoord((int)(positionInWorld.x / Data.chunkWidth),
@@ -247,6 +252,7 @@ public class Client
Send(new byte[] { 3, (byte)chunk.x, (byte)chunk.z, xInChunk, yInChunk, zInChunk, blockID }); Send(new byte[] { 3, (byte)chunk.x, (byte)chunk.z, xInChunk, yInChunk, zInChunk, blockID });
} }
//default send method
private void Send(byte[] data) private void Send(byte[] data)
{ {
client.Send(data, data.Length); client.Send(data, data.Length);
@@ -33,6 +33,8 @@ public class Player : MonoBehaviour
private void Start() private void Start()
{ {
client = world.GetClient(); client = world.GetClient();
//lock the cursor
Cursor.lockState = CursorLockMode.Locked; Cursor.lockState = CursorLockMode.Locked;
camera = GetComponentInChildren<Camera>(); camera = GetComponentInChildren<Camera>();
transform.position = new Vector3((byte)transform.position.x, transform.position = new Vector3((byte)transform.position.x,
@@ -41,6 +43,7 @@ public class Player : MonoBehaviour
private void Jump() private void Jump()
{ {
//add velocity up
verticalMomentum = jumpForce; verticalMomentum = jumpForce;
isGrounded = false; isGrounded = false;
jumpRequest = false; jumpRequest = false;
@@ -48,6 +51,7 @@ public class Player : MonoBehaviour
private void FixedUpdate() private void FixedUpdate()
{ {
//if space was pressed
if (jumpRequest) if (jumpRequest)
{ {
Jump(); Jump();
@@ -56,6 +60,7 @@ public class Player : MonoBehaviour
CalculateVelocity(); CalculateVelocity();
transform.Translate(velocity, Space.World); transform.Translate(velocity, Space.World);
//check if position or rotation had changed and send an update
Vector3 position = transform.position; Vector3 position = transform.position;
Quaternion rotation = transform.rotation; Quaternion rotation = transform.rotation;
@@ -76,6 +81,7 @@ public class Player : MonoBehaviour
private void Update() private void Update()
{ {
//get the player input
horizontal = Input.GetAxisRaw("Horizontal"); horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical"); vertical = Input.GetAxisRaw("Vertical");
mouseX = Input.GetAxis("Mouse X"); mouseX = Input.GetAxis("Mouse X");
@@ -86,6 +92,7 @@ public class Player : MonoBehaviour
jumpRequest = true; jumpRequest = true;
} }
//get the camera up and down movement and clamp it
rotX -= mouseY * 1.4f; rotX -= mouseY * 1.4f;
rotX = Mathf.Clamp(rotX, -90, 90); rotX = Mathf.Clamp(rotX, -90, 90);
@@ -93,6 +100,7 @@ public class Player : MonoBehaviour
transform.Rotate(0, mouseX, 0); transform.Rotate(0, mouseX, 0);
//send destroy block request to server
if (Input.GetMouseButtonDown(0)) if (Input.GetMouseButtonDown(0))
{ {
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit)) if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit))
@@ -102,6 +110,7 @@ public class Player : MonoBehaviour
} }
} }
//send build block request to server
if (Input.GetMouseButtonDown(1)) if (Input.GetMouseButtonDown(1))
{ {
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit)) if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit))
@@ -114,24 +123,29 @@ public class Player : MonoBehaviour
private void CalculateVelocity() private void CalculateVelocity()
{ {
//check if player can accelerate any faster (there is a max speed)
if (verticalMomentum > gravity) if (verticalMomentum > gravity)
{ {
verticalMomentum += Time.fixedDeltaTime * gravity; verticalMomentum += Time.fixedDeltaTime * gravity;
} }
//add velocity for moving, falling and jumping
velocity = ((transform.forward * vertical) + (transform.right * horizontal)) * Time.fixedDeltaTime * walkSpeed; velocity = ((transform.forward * vertical) + (transform.right * horizontal)) * Time.fixedDeltaTime * walkSpeed;
velocity += Vector3.up * verticalMomentum * Time.fixedDeltaTime; velocity += Vector3.up * verticalMomentum * Time.fixedDeltaTime;
//check colliders on z axis
if ((velocity.z > 0 && front) || (velocity.z < 0 && back)) if ((velocity.z > 0 && front) || (velocity.z < 0 && back))
{ {
velocity.z = 0; velocity.z = 0;
} }
//check colliders on x axis
if ((velocity.x > 0 && right) || (velocity.x < 0 && left)) if ((velocity.x > 0 && right) || (velocity.x < 0 && left))
{ {
velocity.x = 0; velocity.x = 0;
} }
//check colliders on y axis
if (velocity.y < 0) if (velocity.y < 0)
{ {
velocity.y = CheckDownSpeed(velocity.y); velocity.y = CheckDownSpeed(velocity.y);
@@ -146,6 +160,7 @@ public class Player : MonoBehaviour
} }
} }
//check colliders for -y
private float CheckDownSpeed(float downSpeed) private float CheckDownSpeed(float downSpeed)
{ {
if ( if (
@@ -163,6 +178,7 @@ public class Player : MonoBehaviour
return downSpeed; return downSpeed;
} }
//check colliders for +y
private float CheckUpSpeed(float upSpeed) private float CheckUpSpeed(float upSpeed)
{ {
if ( if (
@@ -178,6 +194,7 @@ public class Player : MonoBehaviour
return upSpeed; return upSpeed;
} }
//check colliders for +z
public bool front public bool front
{ {
get get
@@ -194,6 +211,7 @@ public class Player : MonoBehaviour
} }
} }
//check colliders for -z
public bool back public bool back
{ {
get get
@@ -210,6 +228,7 @@ public class Player : MonoBehaviour
} }
} }
//check colliders for -x
public bool left public bool left
{ {
get get
@@ -226,6 +245,7 @@ public class Player : MonoBehaviour
} }
} }
//check colliders for +x
public bool right public bool right
{ {
get get
@@ -242,6 +262,7 @@ public class Player : MonoBehaviour
} }
} }
//stores update data for position and if a player should be removed from the world
public class PlayerPositionUpdateData public class PlayerPositionUpdateData
{ {
public PlayerPositionUpdateData(byte id, Vector3 position, bool destroy) public PlayerPositionUpdateData(byte id, Vector3 position, bool destroy)
@@ -256,6 +277,7 @@ public class Player : MonoBehaviour
public bool destroy; public bool destroy;
} }
//stores update data for rotations
public class PlayerRotationsUpdateData public class PlayerRotationsUpdateData
{ {
public byte id; public byte id;
+14 -1
View File
@@ -31,12 +31,14 @@ public class World : MonoBehaviour
private void Start() private void Start()
{ {
//get default information
name = PlayerPrefs.GetString("userName"); name = PlayerPrefs.GetString("userName");
ip = PlayerPrefs.GetString("serverIP"); ip = PlayerPrefs.GetString("serverIP");
port = int.Parse(PlayerPrefs.GetString("port")); port = int.Parse(PlayerPrefs.GetString("port"));
client = new Client(ip, port, this, name); client = new Client(ip, port, this, name);
} }
//removes a player from the game world
public void RemovePlayer(byte id) public void RemovePlayer(byte id)
{ {
playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, Vector3.zero, true)); playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, Vector3.zero, true));
@@ -44,12 +46,14 @@ public class World : MonoBehaviour
private void Update() private void Update()
{ {
//update 1 chunk in list
if (chunksToUpdate.Count > 0) if (chunksToUpdate.Count > 0)
{ {
ChunkCoord chunk = chunksToUpdate.Dequeue(); ChunkCoord chunk = chunksToUpdate.Dequeue();
chunks[chunk.x, chunk.z].Update(); chunks[chunk.x, chunk.z].Update();
} }
//create 1 player in list
if (playersToCreate.Count > 0) if (playersToCreate.Count > 0)
{ {
byte id = playersToCreate.Dequeue(); byte id = playersToCreate.Dequeue();
@@ -61,6 +65,7 @@ public class World : MonoBehaviour
otherPlayers.Add(id, player); otherPlayers.Add(id, player);
} }
//update 1 player position in list
if (playersPositionsToUpdate.Count > 0) if (playersPositionsToUpdate.Count > 0)
{ {
Player.PlayerPositionUpdateData data = playersPositionsToUpdate.Dequeue(); Player.PlayerPositionUpdateData data = playersPositionsToUpdate.Dequeue();
@@ -76,6 +81,7 @@ public class World : MonoBehaviour
} }
} }
//update 1 player rotation in list
if (playersRotationsToUpdate.Count > 0) if (playersRotationsToUpdate.Count > 0)
{ {
Player.PlayerRotationsUpdateData data = playersRotationsToUpdate.Dequeue(); Player.PlayerRotationsUpdateData data = playersRotationsToUpdate.Dequeue();
@@ -87,32 +93,37 @@ public class World : MonoBehaviour
} }
} }
//enqueue player position update to list
public void UpdatePlayerPosition(Vector3 position, byte id) public void UpdatePlayerPosition(Vector3 position, byte id)
{ {
playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, position, false)); playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, position, false));
} }
//enqueue player rotation update to list
public void UpdatePlayerRotation(Quaternion rotation, byte id) public void UpdatePlayerRotation(Quaternion rotation, byte id)
{ {
playersRotationsToUpdate.Enqueue(new Player.PlayerRotationsUpdateData(id, rotation)); playersRotationsToUpdate.Enqueue(new Player.PlayerRotationsUpdateData(id, rotation));
} }
//enqueue player create data to list
public void AddPlayer(byte id) public void AddPlayer(byte id)
{ {
playersToCreate.Enqueue(id); playersToCreate.Enqueue(id);
} }
//creates the array for chunk objects
public void CreateChunkArray() public void CreateChunkArray()
{ {
chunks = new Chunk[worldSize, worldSize]; chunks = new Chunk[worldSize, worldSize];
} }
//initialized a chunk
public void InitChunk(int x, int z, byte[,,] blocks) public void InitChunk(int x, int z, byte[,,] blocks)
{ {
chunks[x, z] = new Chunk(material, new ChunkCoord(x, z), this, blocks); chunks[x, z] = new Chunk(material, new ChunkCoord(x, z), this, blocks);
} }
//creates an array of chunks //initialized the chunk objects
public void CreateChunks() public void CreateChunks()
{ {
for (int x = 0; x < worldSize; x++) for (int x = 0; x < worldSize; x++)
@@ -206,6 +217,7 @@ public class World : MonoBehaviour
return blockData[blockID].textures; return blockData[blockID].textures;
} }
//get the perlin noise height
public byte GetHeight(int x, int z) public byte GetHeight(int x, int z)
{ {
float scale = 0.025f; float scale = 0.025f;
@@ -216,6 +228,7 @@ public class World : MonoBehaviour
return height; return height;
} }
//edit a block in local chunk storage
public void EditBlock(ChunkCoord chunk, Vector3 positionInChunk, byte blockID) public void EditBlock(ChunkCoord chunk, Vector3 positionInChunk, byte blockID)
{ {
byte xInChunk = (byte)(positionInChunk.x); byte xInChunk = (byte)(positionInChunk.x);
+30
View File
@@ -89,12 +89,14 @@ public class Server
} }
} }
//init message / world transfer
if (data[0] == 0) if (data[0] == 0)
{ {
Thread t = new Thread(TransferWorld); Thread t = new Thread(TransferWorld);
t.Start(endPoint); t.Start(endPoint);
} }
//get the world size (first message from client)
if (data[0] == 2) if (data[0] == 2)
{ {
Send(new byte[] { 2, (byte)world.worldSize }, endPoint); Send(new byte[] { 2, (byte)world.worldSize }, endPoint);
@@ -112,10 +114,27 @@ public class Server
playerPositions.Add(new Vector3(0, 0, 0)); playerPositions.Add(new Vector3(0, 0, 0));
} }
//edit a block in world
if (data[0] == 3) if (data[0] == 3)
{ {
ChunkCoord chunk = new ChunkCoord(data[1], data[2]); ChunkCoord chunk = new ChunkCoord(data[1], data[2]);
Vector3 positionInChunk = new Vector3(data[3], data[4], data[5]); Vector3 positionInChunk = new Vector3(data[3], data[4], data[5]);
Vector3 positionInWorld = new Vector3(chunk.x * Data.chunkWidth + positionInChunk.x,
positionInChunk.y, chunk.z * Data.chunkWidth + positionInChunk.z);
//check that there is no player on the block position
bool found = false;
foreach (Vector3 v in playerPositions)
{
if (((int)v.x == (int)positionInWorld.x && (int)v.z == (int)positionInWorld.z) &&
((int)v.y == (int)positionInWorld.y || (int)v.y + 1 == (int)positionInWorld.y))
{
found = true;
}
}
if (!found)
{
byte blockID = data[6]; byte blockID = data[6];
world.EditBlock(chunk, positionInChunk, blockID); world.EditBlock(chunk, positionInChunk, blockID);
@@ -129,7 +148,9 @@ public class Server
}, e); }, e);
} }
} }
}
//a player successfully joined the game...
if (data[0] == 6) if (data[0] == 6)
{ {
int id = GetPlayerID(endPoint); int id = GetPlayerID(endPoint);
@@ -139,12 +160,15 @@ public class Server
{ {
if (id != GetPlayerID(e)) if (id != GetPlayerID(e))
{ {
//create player objects of that player in every connected client
Send(new byte[]{5, (byte)GetPlayerID(e)}, endPoint); Send(new byte[]{5, (byte)GetPlayerID(e)}, endPoint);
//create player objects of all connected clients in the currently connected client
Send(new byte[]{5, (byte)GetPlayerID(endPoint)}, e); Send(new byte[]{5, (byte)GetPlayerID(endPoint)}, e);
} }
} }
} }
//update a players position on the server
if (data[0] == 7) if (data[0] == 7)
{ {
byte[] positionASCII = new byte[data.Length - 1]; byte[] positionASCII = new byte[data.Length - 1];
@@ -172,6 +196,7 @@ public class Server
send[i] = sendPositionASCII[i - 2]; send[i] = sendPositionASCII[i - 2];
} }
//send update to every other client
foreach (IPEndPoint e in players) foreach (IPEndPoint e in players)
{ {
if (id != GetPlayerID(e)) if (id != GetPlayerID(e))
@@ -181,6 +206,7 @@ public class Server
} }
} }
//update a players rotation on server
if (data[0] == 8) if (data[0] == 8)
{ {
byte[] rotationASCII = new byte[data.Length - 1]; byte[] rotationASCII = new byte[data.Length - 1];
@@ -207,6 +233,7 @@ public class Server
send[i] = sendRotationASCII[i - 2]; send[i] = sendRotationASCII[i - 2];
} }
//send update to every other client
foreach (IPEndPoint e in players) foreach (IPEndPoint e in players)
{ {
if (id != GetPlayerID(e)) if (id != GetPlayerID(e))
@@ -223,6 +250,7 @@ public class Server
} }
} }
//returns the id of an player
private int GetPlayerID(IPEndPoint endPoint) private int GetPlayerID(IPEndPoint endPoint)
{ {
int i = 0; int i = 0;
@@ -239,6 +267,7 @@ public class Server
return -1; return -1;
} }
//starts a tcp client for world transfer
private void TransferWorld(object o) private void TransferWorld(object o)
{ {
try try
@@ -290,6 +319,7 @@ public class Server
} }
} }
//default send function
private void Send(byte[] data, IPEndPoint endPoint) private void Send(byte[] data, IPEndPoint endPoint)
{ {
client.Send(data, data.Length, endPoint); client.Send(data, data.Length, endPoint);