private int comparePriority(int x, int y) { if (tmpmap[x] == null) { if (tmpmap[y] == null) { return(0); } else { return(-1); } } else { if (tmpmap[y] == null) { return(1); } else { double dist_ = GameGeometry.point_distance(tmppos, tmpmap[x].pos), dist2_ = GameGeometry.point_distance(tmppos, tmpmap[y].pos); return(dist_.CompareTo(dist2_)); } } }
public void update(GameWorld gameWorld) { if (gameWorld.entityMap.ContainsKey(entityId)) { if (clientHandler.Connected) { GameEntity ent_ = gameWorld.entityMap[entityId]; float dir = ent_.direction; if (inputMap.getInput("left") == 1f) //strafe left { ent_.spd.Add(GameGeometry.lengthdir(Convert.ToSingle(ent_.base_spd.Y), dir - 90)); } if (inputMap.getInput("right") == 1f) //strafe right { ent_.spd.Add(GameGeometry.lengthdir(Convert.ToSingle(ent_.base_spd.Y), dir + 90)); } if (inputMap.getInput("forward") == 1f) //forward { ent_.spd.Add(GameGeometry.lengthdir(Convert.ToSingle(ent_.base_spd.X), dir)); } if (inputMap.getInput("backward") == 1f) //backward { ent_.spd.Add(GameGeometry.lengthdir(Convert.ToSingle(ent_.base_spd.Z), dir + 180)); } if (inputMap.getInput("up") == 1f) //jump { GamePoint3D d1 = ent_.pos + new GamePoint3D(-ent_.size.X, -ent_.size.X, -1), d2 = ent_.pos + new GamePoint3D(ent_.size.X, ent_.size.X, ent_.size.Y - 1); if (gameWorld.checkCollision(d1, d2)) { ent_.spd.Z += 8d; } } if (inputMap.getInput("down") == 1f) //crouch { ent_.spd.Z -= 1d; //currently just makes the player fall faster? } ent_.direction = inputMap.getInput("view_x"); ent_.pitch = inputMap.getInput("view_y"); } else { gameWorld.removeEntity(entityId); gameWorld.removeClient(gameWorld.getPlayer(clientHandler.Socket)); } ulong tmpStep = mainProgram.gameSteps; uint pos_ = 1; foreach (int thing in priorityList) { if (tmpStep % pos_ == 0 && !updatedQueue.Contains(thing)) { updatedQueue.Enqueue(thing); } pos_++; } } }
public void sendUpdates(List <int> alreadyDone = null) { if (alreadyDone == null) { alreadyDone = new List <int>(); } try { foreach (KeyValuePair <int, GameClient> pair in clientMap) { if (!alreadyDone.Contains(pair.Key)) { int count = pair.Value.updatedQueue.Count; BufferStream buff_ = new BufferStream(12 + (52 * count), 1); buff_.Seek(0); buff_.Write((ushort)0); buff_.Write(pair.Value.updatedQueue.Count); int updates = GameGeometry.clamp(96 - count, 0, 96); while (updates < 96) { int ent_ = pair.Value.updatedQueue.Dequeue(); if (entityMap.ContainsKey(ent_)) { GameEntity ent = entityMap[ent_]; buff_.Write(ent.id); buff_.Write(ent.pos.X); buff_.Write(ent.pos.Y); buff_.Write(ent.pos.Z); buff_.Write(ent.size.X); buff_.Write(ent.size.Y); buff_.Write(ent.direction); buff_.Write(ent.pitch); updates++; } } sendToClient(pair.Value, buff_); buff_.Deallocate(); alreadyDone.Add(pair.Key); } } } catch (InvalidOperationException) { mainProgram.WriteLine("error-client map probably changed"); sendUpdates(alreadyDone); } }
public bool checkCollision(GamePoint3D d1, GamePoint3D d2) { try { foreach (KeyValuePair <int, GameObject> pair in objectMap) { GamePoint3D p1 = pair.Value.position, p2 = pair.Value.position + pair.Value.size; if (GameGeometry.cube_in_cube(d1, d2, p1, p2)) { return(true); } } } catch (InvalidOperationException) { //needs to try again since the objectMap was altered return(checkCollision(d1, d2)); } return(false); }
private static void event_received(TcpClientHandler client, BufferStream readBuffer) { int plid = gameWorld.getPlayer(client.Socket); //this following line is probably absolutely essential to deal with the GMS packets readBuffer.Seek(12); BufferStream buff_ = null; //get the message id, so we know what to do with the packet ushort msgid; readBuffer.Read(out msgid); switch (msgid) { case 0: //client controls update /* so how this works, is that the control info is sent in a single byte, where each bit represents a movement or something, seen below * 0000000X - forward * 000000X0 - backward * 00000X00 - strafe left * 0000X000 - strafe right * 000X0000 - up ~jump * 00X00000 - down ~crouch */ byte in_; readBuffer.Read(out in_); bool[] actions = GameGeometry.parse_binary(in_); if (plid >= 0) { gameWorld.clientMap[plid].inputMap.setInput("forward", actions[0]); gameWorld.clientMap[plid].inputMap.setInput("backward", actions[1]); gameWorld.clientMap[plid].inputMap.setInput("left", actions[2]); gameWorld.clientMap[plid].inputMap.setInput("right", actions[3]); gameWorld.clientMap[plid].inputMap.setInput("up", actions[4]); gameWorld.clientMap[plid].inputMap.setInput("down", actions[5]); } break; case 1: //client view update //we should be getting two floats, one for direction and one for pitch float dir_; readBuffer.Read(out dir_); float pit_; readBuffer.Read(out pit_); if (plid >= 0) { gameWorld.clientMap[plid].inputMap.setInput("view_x", dir_); gameWorld.clientMap[plid].inputMap.setInput("view_y", pit_); } break; case 2: //client sent back a ping GameClient client_ = gameWorld.getClientFromSocket(client.Socket); client_.pingWatch.Stop(); mainProgram.WriteLine("Socket " + client.Socket.ToString() + " ping is " + client_.pingWatch.ElapsedMilliseconds); break; case 3: //client is disconnecting buff_ = new BufferStream(2, 1); buff_.Write((ushort)7); gameWorld.sendToClient(client, buff_); client.Connected = false; break; case 4: //client requested an entity int entId; readBuffer.Read(out entId); buff_ = new BufferStream(64, 1); buff_.Write((ushort)4); buff_.Write(gameWorld.entityMap[entId].id); buff_.Write(gameWorld.entityMap[entId].pos.X); buff_.Write(gameWorld.entityMap[entId].pos.Y); buff_.Write(gameWorld.entityMap[entId].pos.Z); buff_.Write(gameWorld.entityMap[entId].size.X); buff_.Write(gameWorld.entityMap[entId].size.Y); buff_.Write(gameWorld.entityMap[entId].direction); buff_.Write(gameWorld.entityMap[entId].pitch); gameWorld.sendToClient(client, buff_); break; default: mainProgram.WriteLine("invalid packet received"); break; } if (buff_ != null) { buff_.Deallocate(); } }
public bool update(GameWorld gameWorld) { spd.Z -= 0.5; GamePoint3D sz3_ = new GamePoint3D(size.X, size.X, -size.Y); GamePoint3D d1 = pos + new GamePoint2D(-size.X, -size.X), d2 = pos + new GamePoint3D(size.X, size.X, size.Y); if (spd.X != 0d) { GamePoint3D t_ = new GamePoint3D(spd.X, 0d, 0d); while (gameWorld.checkCollision(d1 + t_, d2 + t_)) { if (Math.Abs(spd.X) > precision) { spd.X = 0; break; } spd.X -= precision * Math.Sign(spd.X); t_.X = spd.X; } } if (spd.Y != 0d) { GamePoint3D t_ = new GamePoint3D(0d, spd.Y, 0d); while (gameWorld.checkCollision(d1 + t_, d2 + t_)) { if (Math.Abs(spd.Y) > precision) { spd.Y = 0; break; } spd.Y -= precision * Math.Sign(spd.Y); t_.Y = spd.Y; } } if (spd.Z != 0d) { GamePoint3D t_ = new GamePoint3D(0d, 0d, spd.Z); while (gameWorld.checkCollision(d1 + t_, d2 + t_)) { if (Math.Abs(spd.Z) > precision) { spd.Z = 0; break; } spd.Z -= precision * Math.Sign(spd.Z); t_.Z = spd.Z; } } if (Math.Abs(spd.X) <= spd_deadzone.X) { spd.X = 0; } if (Math.Abs(spd.Y) <= spd_deadzone.Y) { spd.Y = 0; } if (Math.Abs(spd.Z) <= spd_deadzone.Z) { spd.Z = 0; } pos += spd; spd = spd.Divide(frc); direction = direction % 360f; pitch = GameGeometry.clamp(pitch, -89f, 89f); bool updateSelf = false; if (pos != previous_pos || previous_direction != direction || previous_pitch != pitch) { updateSelf = true; } previous_pos = pos; return(updateSelf); }