public override void setSpriteFromVelocity() { if (attacking) { if (facingLeft) { StateChange sc = new StateChange(); sc.type = StateChangeType.CHANGE_SPRITE; sc.intProperties.Add(StateProperties.ENTITY_ID, id); sc.intProperties.Add(StateProperties.FRAME_WIDTH, attackFrameWidth); sc.stringProperties.Add(StateProperties.SPRITE_NAME, textureAttackLeft); notifyStateChangeListeners(sc); } else if (!facingLeft) { StateChange sc = new StateChange(); sc.type = StateChangeType.CHANGE_SPRITE; sc.intProperties.Add(StateProperties.ENTITY_ID, id); sc.intProperties.Add(StateProperties.FRAME_WIDTH, attackFrameWidth); sc.stringProperties.Add(StateProperties.SPRITE_NAME, textureAttackRight); notifyStateChangeListeners(sc); } } else { base.setSpriteFromVelocity(); } }
public static StateChange createDeleteStateChange(int id) { StateChange sc = new StateChange(); sc.type = StateChangeType.DELETE_ENTITY; sc.intProperties.Add(StateProperties.ENTITY_ID, id); return sc; }
public static StateChange createSetPlayerStateChange(int id) { StateChange sc = new StateChange(); sc.type = StateChangeType.SET_PLAYER; sc.intProperties.Add(StateProperties.ENTITY_ID, id); return sc; }
public static StateChange createScaleStateChange(int id, float value) { StateChange sc = new StateChange(); sc.type = StateChangeType.CHANGE_SCALE; sc.intProperties.Add(StateProperties.ENTITY_ID, id); sc.doubleProperties.Add(StateProperties.SCALE, value); return sc; }
public static StateChange createHealthBarStateChange(int id, int health_bar_id) { StateChange sc = new StateChange(); sc.type = StateChangeType.CREATE_HEALTH_BAR; sc.intProperties.Add(StateProperties.ENTITY_ID, id); sc.intProperties.Add(StateProperties.HEALTH_BAR_ID, health_bar_id); return sc; }
public static StateChange changeHealthStateChange(int id, int health) { StateChange sc = new StateChange(); sc.type = StateChangeType.CHANGE_HEALTH; sc.intProperties.Add(StateProperties.ENTITY_ID, id); sc.intProperties.Add(StateProperties.HEALTH, health); return sc; }
public static StateChange createChangeSpriteStateChange(int id, int frameWidth, String texture, int frameTime) { StateChange sc = new StateChange(); sc.type = StateChangeType.CHANGE_SPRITE; sc.intProperties.Add(StateProperties.ENTITY_ID, id); sc.intProperties.Add(StateProperties.FRAME_WIDTH, frameWidth); sc.intProperties.Add(StateProperties.FRAME_TIME, frameTime); sc.stringProperties.Add(StateProperties.SPRITE_NAME, texture); return sc; }
public static StateChange createEntityStateChange(int entity_id, int posX, int posY, int frameWidth, int frameTime, String textureName, float scale, int drawPriority) { StateChange sc = new StateChange(); sc.type = StateChangeType.CREATE_ENTITY; sc.intProperties.Add(StateProperties.ENTITY_ID, entity_id); sc.intProperties.Add(StateProperties.POSITION_X, posX); sc.intProperties.Add(StateProperties.POSITION_Y, posY); sc.intProperties.Add(StateProperties.DRAW_PRIORITY, drawPriority); sc.intProperties.Add(StateProperties.FRAME_WIDTH, frameWidth); sc.intProperties.Add(StateProperties.FRAME_TIME, frameTime); sc.stringProperties.Add(StateProperties.SPRITE_NAME, textureName); sc.doubleProperties.Add(StateProperties.SCALE, scale); return sc; }
public bool handleStateChange(StateChange s) { if (s.type == StateChangeType.DELETE_ENTITY) { destroy(); return true; } else if (s.type == StateChangeType.MOVED) { int x = s.intProperties[StateProperties.POSITION_X]; int y = s.intProperties[StateProperties.POSITION_Y] - 45; this.worldPosBack = new Vector2(x, y); } else if (s.type == StateChangeType.CHANGE_HEALTH) { this.health = s.intProperties[StateProperties.HEALTH]; } return false; // return false if we can take more events }
public STCPacket(StateChange s) { this.ptype = PacketType.STC; this.addHeader(ptype); this.addContent(s.getPacketData()); this.finalize(); }
// This should never create a state change public void applyStateChange(StateChange s) { int entity = s.intProperties[StateProperties.ENTITY_ID]; // If there is a health bar for this entity, pass the event along if (healthBars.ContainsKey(entity)) { healthBars[entity].handleStateChange(s); } if (s.type != StateChangeType.CREATE_ENTITY && !entities.ContainsKey(entity)) { if (s.type == StateChangeType.SET_PLAYER) // Can't ignore this case, need to save the ID for when we get it { usersEntity = entity; } else if (s.type == StateChangeType.CREATE_HEALTH_BAR) { healthBarWaiters.Add(entity); } return; // Otherwise, we have to discard this event and wait until the guy shows up } if (s.type == StateChangeType.CREATE_HEALTH_BAR) { attachHealthBar(entities[entity]); } else if (s.type == StateChangeType.MOVED) { if (entity == usersEntity) { isMoving = true; } int pos_x = s.intProperties[StateProperties.POSITION_X]; int pos_y = s.intProperties[StateProperties.POSITION_Y]; entities[entity].worldPosBack = new Vector2(pos_x, pos_y); // do a change without triggering a statechange } else if (s.type == StateChangeType.CREATE_ENTITY) { int pos_x = s.intProperties[StateProperties.POSITION_X]; int pos_y = s.intProperties[StateProperties.POSITION_Y]; int frame_width = s.intProperties[StateProperties.FRAME_WIDTH]; int frame_time = s.intProperties[StateProperties.FRAME_TIME]; int draw_priority = s.intProperties[StateProperties.DRAW_PRIORITY]; String texture_name = s.stringProperties[StateProperties.SPRITE_NAME]; float scale = (float)s.doubleProperties[StateProperties.SCALE]; Entity e = new Entity(context, pos_x, pos_y, frame_width, frame_time, texture_name, scale); addEntity(draw_priority, e, e.id); // Set users player if it didn't exist yet if (e.id == usersEntity) { usersPlayer = e; } // Attach health bar if we were waiting for this if(healthBarWaiters.Contains(e.id)) { attachHealthBar(e); } } else if (s.type == StateChangeType.DELETE_ENTITY) { removeEntity(entity); } else if (s.type == StateChangeType.SET_PLAYER) { usersPlayer = entities[entity]; } else if (s.type == StateChangeType.CHANGE_SPRITE) { int frame_width = s.intProperties[StateProperties.FRAME_WIDTH]; String texture_name = s.stringProperties[StateProperties.SPRITE_NAME]; entities[entity].changeAnimation(frame_width, texture_name); } else if (s.type == StateChangeType.CHANGE_SCALE) { float lscale = (float)s.doubleProperties[StateProperties.SCALE]; entities[entity].scaleBack = lscale; } }
public void handleStateChange(StateChange s) { changes.Add(s); }
public static StateChange getStateData(byte[] b) { StateChange ret = new StateChange(b); return ret; }
//Main routine, this does all the processing private void ClientstartFunc() { // Hold here until the server information has been provided ready.WaitOne(); // Event Loop // Pull packets out of the network layer and handle them while (this.go) { Packet newPacket = nw.getNext(); // This is a blocking call! // Handle timeout if (newPacket == null) { Console.WriteLine("Timeout on receive"); switch (curState) { case cState.TRYCONNECT: // Did not receive the expected HANDSHAKE message // Restart the handshake this.handshake(); break; case cState.CONNECTED: // The server may have died, ping the server to find out //this.pingServer(); //TODO Should probably not silently ignore this.... lock (this) { if (this.sw_ping.IsRunning) { //this.curState = cState.DISCONNECTED; this.sw_ping.Stop(); this.lastPing = 501; } } break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } } else { //Console.Write("Received packet of type: "); //Console.WriteLine(newPacket.ptype); // Handle the new packet switch (newPacket.ptype) { case Packet.PacketType.CMD: Console.WriteLine("Should not be getting CMD packets from the server..."); Environment.Exit(1); break; case Packet.PacketType.HANDSHAKE: Console.WriteLine("Handshake received from the server"); switch (curState) { case cState.TRYCONNECT: // The connection has succeeded! this.startPing(); this.curState = cState.CONNECTED; break; case cState.CONNECTED: // Repeat? This can be ignored ( I hope...) break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.STC: //Console.WriteLine("STC received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: // Marshall the state change packet into an object StateChange newSTC = new StateChange(newPacket.data); // Add the state change object to the buffer for the UI lock (this.buffer) { buffer.Enqueue(newSTC); } break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.MSC: //Console.WriteLine("MSC received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: // Marshall the state change packet into an object MenuState newMSC = new MenuState(newPacket.data); // Add the state change object to the buffer for the UI lock (this.menuBuffer) { menuBuffer.Enqueue(newMSC); } break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.SYNC: //Console.WriteLine("SYNC received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: syncServer(); break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.PING: //Console.WriteLine("PING received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: lock (this) { sw_ping.Stop(); this.lastPing = sw_ping.ElapsedMilliseconds; } //Console.WriteLine("Ping"+lastPing); break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; default: Console.WriteLine("Unknown packet type from the server..."); Environment.Exit(1); break; } } } }
public void notifyStateChangeListeners(StateChange sc) { foreach (StateChangeListener l in slListeners) { l.handleStateChange(sc); } }