public CMDPacket(Command s) { this.ptype = PacketType.CMD; this.addHeader(ptype); this.addContent(s.getPacketData()); this.finalize(); }
//Netcode testing suite... or just a template public static void Main(string[] args) { //while (true) //{ // if (s.getCMD().Count > 0) // { // foreach (Command c in s.getCMD()) // { // Console.WriteLine(c.ct); // Console.WriteLine(c.entity_id); // Console.WriteLine(c.direction.X); // Console.WriteLine(c.direction.Y); // Console.WriteLine(c.position.X); // Console.WriteLine(c.position.Y); // Console.WriteLine("==========="); // } // } // List<StateChange> l = new List<StateChange>(); // StateChange st = new StateChange(); // st.type = StateChangeType.DELETE_ENTITY; // st.intProperties[StateProperties.FRAME_WIDTH] = 123; // st.stringProperties[StateProperties.SPRITE_NAME] = "I'm the Baconator"; // l.Add(st); // s.broadcastSC(l); // Thread.Sleep(2000); //} c.connect("127.0.0.1", 9999); c1.connect("127.0.0.1", 9999); c2.connect("127.0.0.1", 9999); c3.connect("127.0.0.1", 9999); Timer t = new Timer(NetTest.doPing, new AutoResetEvent(false), 0, 2000); while (true) { List<Command> l = new List<Command>(); Command cm = new Command(); cm.ct = CommandType.ATTACK; cm.direction.X = 3.14159F; cm.direction.Y = 3.14159F; cm.position.X = 3.14159F; cm.position.Y = 3.14159F; l.Add(cm); c.sendCMD(l); c1.sendCMD(l); c2.sendCMD(l); c3.sendCMD(l); Thread.Sleep(10); } }
public override void UpdateAI(GameTime time) { List<PlayerCharacter> targets = context.gameState.players; // Find closest target Entity target = null; float currentLength = 0; foreach (Entity e in targets) { float sl = (e.worldPosition - worldPosition).Length(); if (target == null || sl < currentLength) { currentLength = sl; target = e; } } // Move towards target velocity = (target.worldPosition - worldPosition); if (Math.Abs(velocity.Y) < 5) { if (base.startAttack(time)) { int vel = 1; if (facingLeft) vel = -1; Command c = new Command(); c.ct = CommandType.GOBLIN_ATTACK; c.entity_id = id; c.position = worldPosition; c.direction = new Vector2(vel, 0); context.commandBuffer.Add(c); } } if(Math.Abs(velocity.X) < 80) { velocity.X = 0; } velocity.Y *= 2; // Bias towards moving up and down velocity.Normalize(); velocity *= 2; }
private void runThis() { Packet p; while (this.go) { p = nw.getNext(); if (p == null) { foreach (IPEndPoint ep in connections.Keys) { SYNCPacket ps = new SYNCPacket(); ps.Dest = ep; this.nw.commitPacket(ps); } continue; } //Console.WriteLine(p.ptype); switch (p.ptype) { case Packet.PacketType.HANDSHAKE: if (!connections.ContainsKey(p.Dest)) { Console.WriteLine("Server - New connection from: " + p.Dest); connections[p.Dest] = new ConnectionID(p.Dest); Console.WriteLine("Server - Added Connection: " + connections[p.Dest].ID); HandshakePacket hs = new HandshakePacket(); hs.Dest = p.Dest; nw.commitPacket(hs); } break; case Packet.PacketType.STC: //Console.WriteLine("Server - Receivd State Change from client... who do they think they are?"); Environment.Exit(1); break; case Packet.PacketType.SYNC: if (connections.ContainsKey(p.Dest)) { //Console.WriteLine("Server - SYNC Reply from: " + connections[p.Dest].ID); } else { Console.WriteLine("Server - ERROR Unregistered SYNC"); } break; case Packet.PacketType.PING: if (connections.ContainsKey(p.Dest)) { //Console.WriteLine("Server - Ping from connection: " + connections[p.Dest].ID); PingPacket ps = new PingPacket(); ps.Dest = p.Dest; nw.commitPacket(ps); //ACK the ping } else { Console.WriteLine("Server ERROR - Unregistered PING"); } break; case Packet.PacketType.CMD: //Actually handle this //Console.WriteLine("Server - Got CMD from: " + connections[p.Dest].ID); Command cmd = new Command(p.data); lock (commandQ) this.commandQ.Add(cmd); break; case Packet.PacketType.MSC: //Actually handle this //Console.WriteLine("Server - Got MSC from: " + connections[p.Dest].ID); MenuState msc = new MenuState(p.data); if (connections.ContainsKey(p.Dest)) { ConnectionID cid = connections[p.Dest]; Tuple<ConnectionID, MenuState> newMQ = new Tuple<ConnectionID, MenuState>(cid, msc); lock (mscQ) this.mscQ.Enqueue(newMQ); } break; } this.globalStats.rcvdPkts++; } }
/// <summary> /// Lets the game respond to player input. Unlike the Update method, /// this will only be called when the gameplay screen is active. /// </summary> public override void HandleInput(InputState input) { if (input == null) throw new ArgumentNullException("input"); if (gameState.usersPlayer == null) { return; } // Look up inputs for the active player profile. KeyboardState keyboardState = input.currentKeyboardState; GamePadState gamePadState = input.currentGamePadState; // The game pauses either if the user presses the pause button, or if // they unplug the active gamepad. This requires us to keep track of // whether a gamepad was ever plugged in, because we don't want to pause // on PC if they are playing with a keyboard and have no gamepad at all! bool gamePadDisconnected = !gamePadState.IsConnected && input.gamePadWasConnected; if (input.IsPauseGame() || gamePadDisconnected) { pauseSoundEffect.Play(); ScreenManager.AddScreen(new PauseMenuScreen()); } else { /* ===== COPY PASTE PLAYER 2 ====== */ // Otherwise move the player position. /*Vector2 p2movement = Vector2.Zero; if (keyboardState.IsKeyDown(Keys.A)) p2movement.X--; if (keyboardState.IsKeyDown(Keys.D)) p2movement.X++; if (keyboardState.IsKeyDown(Keys.W)) p2movement.Y--; if (keyboardState.IsKeyDown(Keys.S)) p2movement.Y++; if (p2movement.Length() > 1) p2movement.Normalize(); if (keyboardState.IsKeyDown(Keys.T)) { Command c = new Command(); c.entity_id = secondPlayer.id; c.direction = p2movement; c.ct = CommandType.SHOOT; c.position = secondPlayer.worldPosition; commandBuffer.Add(c); } Command c3 = new Command(); c3.entity_id = secondPlayer.id; c3.direction = p2movement; c3.ct = CommandType.MOVE; commandBuffer.Add(c3);*/ /* ===== COPY PASTE PLAYER 2 ====== */ // Otherwise move the player position. Vector2 movement = Vector2.Zero; if (keyboardState.IsKeyDown(Keys.Left)) movement.X--; if (keyboardState.IsKeyDown(Keys.Right)) movement.X++; if (keyboardState.IsKeyDown(Keys.Up)) movement.Y--; if (keyboardState.IsKeyDown(Keys.Down)) movement.Y++; Vector2 thumbstick = gamePadState.ThumbSticks.Left; movement.X += thumbstick.X; movement.Y -= thumbstick.Y; if (movement.Length() > 1) movement.Normalize(); if ((keyboardState.IsKeyDown(Keys.Z) || gamePadState.Triggers.Right > 0.5) && attackButtonOK) { Command c = new Command(); c.entity_id = gameState.usersPlayer.id; c.direction = movement; c.ct = CommandType.SHOOT; c.position = gameState.usersPlayer.worldPosition; commandBuffer.Add(c); //Console.WriteLine("Shoot"); attackButtonOK = false; } else if (keyboardState.IsKeyUp(Keys.Z) || gamePadState.Triggers.Right < 0.5) { attackButtonOK = true; } if ((keyboardState.IsKeyDown(Keys.X) || gamePadState.Triggers.Left > 0.5) && attackButtonOK) { Command c = new Command(); c.entity_id = gameState.usersPlayer.id; c.ct = CommandType.ATTACK; c.position = gameState.usersPlayer.worldPosition; c.direction = movement; commandBuffer.Add(c); attackButtonOK = false; } else if (keyboardState.IsKeyUp(Keys.X) || gamePadState.Triggers.Left < 0.5) { attackButtonOK = true; } if (keyboardState.IsKeyDown(Keys.R) && canCreate && isServer) { gameState.createEnemy(1280 / 2, 720 / 2 + 200, Enemy.Type.Goblin); canCreate = false; } else if (keyboardState.IsKeyUp(Keys.R)) { canCreate = true; } //if (movement != Vector2.Zero || gameState.isMoving) //{ Command c2 = new Command(); c2.entity_id = gameState.usersPlayer.id; c2.direction = movement; c2.ct = CommandType.MOVE; commandBuffer.Add(c2); gameState.isMoving = false; //} } }
// Move this to GameState at some point public void applyCommand(Command c, GameTime g) { if (c.ct == CommandType.MOVE) { Entity e = gameState.entities[c.entity_id]; e.velocity = c.direction * 3; } else if (c.ct == CommandType.GOBLIN_ATTACK) { gameState.createBolt((int) c.position.X, (int)c.position.Y, c.direction * 6); } else if (c.ct == CommandType.SHOOT) { PlayerCharacter attacker = (PlayerCharacter)gameState.entities[c.entity_id]; Vector2 vel = c.direction; if (vel == Vector2.Zero) { if (attacker.facingLeft) { vel = new Vector2(-1, 0); } else { vel = new Vector2(1, 0); } } Vector2 velocity = vel * 8; if (bulletExists) { PlayerCharacter shooter = (PlayerCharacter)gameState.entities[c.entity_id]; shooter.fireBullet(velocity); } else { Vector2 pos = c.position; gameState.createBullet((int)pos.X, (int)pos.Y, velocity); bulletExists = true; } } else if (c.ct == CommandType.ATTACK) { PlayerCharacter attacker = (PlayerCharacter)gameState.entities[c.entity_id]; bool success = attacker.startAttack(g); Vector2 vel = c.direction; if (vel == Vector2.Zero) { if (attacker.facingLeft) { vel = new Vector2(-1, 0); } else { vel = new Vector2(1, 0); } } if (gameState.entities[c.entity_id] is Wizard) { Vector2 pos = c.position; if (success) gameState.createMageAttack((int)pos.X, (int)pos.Y, vel * 5); } else if (gameState.entities[c.entity_id] is Doctor) { bool hor = false; Vector2 offset; Doctor d = (Doctor)gameState.entities[c.entity_id]; Vector2 pos = c.position; if (!d.facingLeft) { offset = new Vector2(80, 0); } else { offset = new Vector2(-80, 0); } pos += offset; if (success) gameState.createDoctorWall(c.entity_id, (int)pos.X, (int)pos.Y, hor); } else if (gameState.entities[c.entity_id] is Rogue) { Vector2 pos = c.position + new Vector2(0, 15); if (success) gameState.createRogueAttack((int)pos.X, (int)pos.Y, vel * 8); } else if (gameState.entities[c.entity_id] is JarCat) { Vector2 pos = c.position - new Vector2(0, 30); if (success) gameState.createLaser((int)pos.X, (int)pos.Y, vel * 5); } } }