/* Thor Server 2.0 Project - Codenamed "Stockholm" Copyright 2008 - 2009 Joe Hegarty This file is part of The Thor Server 2.0 Project - Codenamed "Stockholm". The Thor Server 2.0 Project - Codenamed "Stockholm" is free software: you can redistribute it and/or modify it under the terms of the Microsoft Public License The Thor Server Project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Microsoft Public License for more details. You should have received a copy of the Microsoft Public License along with The Thor Server Project. If not, see http://www.opensource.org/licenses/ms-pl.html */ using System; using System.Collections.Generic; using System.Linq; using StockholmLibrary.Game.Flats; using StockholmLibrary.Game.Sessions; using StockholmLibrary.Game.Packets.Client; using StockholmLibrary.Game.Packets.Server; using StockholmLibrary.Net; using StockholmLibrary.Data; using StockholmLibrary.Util; namespace StockholmLibrary.Game.Entities.Components { //------------------------------------ public class ECFloorObjectsCollection : EntityComponent { public List FloorObjectCollection = new List(); public object ObjectListLock = new object(); //********************************************** public override void FlatInitiailise(Flat flat) { InitialiseFlatObjects(flat); } //********************************************** public override void FlatDeinitiailise(Flat flat) { } //********************************************** private void InitialiseFlatObjects(Flat flat) { StockholmDbDataContext db = Stockholm.Get().Database; var furniQuery = from p in db.Furnis where p.RoomID == flat.Room.RoomID select p; foreach (var furni in furniQuery) { CreateFloorObject(furni); } } //********************************************** private void CreateFloorObject(Furni furni) { IsometricPoint location = new IsometricPoint((int) furni.PositionX, (int) furni.PositionY, (int) furni.PositionZ); int orientation = (int) furni.Orientation; string spriteName = furni.FurniDefinition.Sprite; int physicsObjectId = furni.FurniDefinition.PhysicsObjectID; int furniId = furni.FurniID; Entity entity = GetParentEntity().AddChild("FloorObject"); ECFloorObject floorObject = entity.AddEC(); ECPhysics physics = entity.AddEC(); floorObject.Initialise(furniId, spriteName); physics.InitialiseLocation(location, orientation); physics.InitialisePhysicsObject(furni.FurniDefinition.PhysicsObjectID); lock (ObjectListLock) { FloorObjectCollection.Add(entity); } } //********************************************** public override void PostInitialise() { lock (ObjectListLock) { foreach (var entity in FloorObjectCollection) { ECPhysics physics = null; if (entity.GetEC(ref physics)) { physics.RegisterWithFlatPhysics(); } } } } //********************************************** private List GetFloorObjectList() { List result = new List(); lock (ObjectListLock) { foreach (var entity in FloorObjectCollection) { ECFloorObject floorObject = null; if (entity.GetEC(ref floorObject)) { result.Add(floorObject.GetFloorObject()); } } } return result; } //********************************************** public override void UserInitiailise(Session userSession) { RegisterSessionHandler(userSession, EClientPacket.G_OBJS, new PacketRoutingInfo.ProcessPacketDelegate(HandleGetFloorObjects)); } //********************************************** public override void UserDeinitiailise(Session userSession) { CleanupSessionHandlers(userSession); } //********************************************** private void HandleGetFloorObjects(ClientPacket packet) { packet.Peer.EnqueuePacket(new FloorObjectsPacket(GetFloorObjectList())); } } }