/* 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.Threading; using System.Threading.Tasks; using StockholmLibrary.Game.Entities.Components; using StockholmLibrary.Game.Sessions; using StockholmLibrary.Game.Flats; using StockholmLibrary.Util; using System.Reflection; namespace StockholmLibrary.Game.Entities { //------------------------------------ public class Entity { Entity _parentEntity; string _name; List _children; List _entityComponents; Flat _flat = null; object _childLock = new object(); //********************************************** public Entity(string name, Entity parent = null) { _name = name; _children = new List(); _entityComponents = new List(); _parentEntity = parent; } //********************************************** public Entity AddChild(string name) { Entity entity = new Entity(name, this); lock (_childLock) { _children.Add(entity); } return entity; } //********************************************** public T AddEC() where T : EntityComponent, new() { if (!HasEC()) { T newComponent = new T(); _entityComponents.Add(newComponent); newComponent.Initialise(this); if (_flat != null) newComponent.FlatInitiailise(_flat); return newComponent; } return null; } //********************************************** public EntityComponent AddEC(string ecName) { try { EntityComponent newComponent = (EntityComponent)Activator.CreateInstance(null, "StockholmLibrary.Game.Entities.Components." + ecName).Unwrap(); _entityComponents.Add(newComponent); newComponent.Initialise(this); return newComponent; } catch { return null; } } //********************************************** public bool HasEC() where T : EntityComponent { foreach (var ec in _entityComponents) if (ec is T) return true; return false; } //********************************************** public bool GetEC(ref T component) where T : EntityComponent { foreach (var ec in _entityComponents) { if (ec is T) { component = (T)ec; return true; } } component = null; return false; } //********************************************** public void FlatInitialise(Flat flat) { _flat = flat; foreach (var ec in _entityComponents) { ec.FlatInitiailise(flat); } } //********************************************** public void PostInitialise() { foreach (var ec in _entityComponents) { ec.PostInitialise(); } } //********************************************** public void UserInitialise(Session user) { foreach (var ec in _entityComponents) { ec.UserInitiailise(user); } } //********************************************** public void FlatDeinitialise(Flat flat) { lock (_childLock) { foreach (var child in _children) { child.FlatDeinitialise(flat); } } foreach (var ec in _entityComponents) { ec.FlatDeinitiailise(flat); } } //********************************************** public void UserDeinitialise(Session user) { lock (_childLock) { foreach (var child in _children) { child.UserDeinitialise(user); } } foreach (var ec in _entityComponents) { ec.UserDeinitiailise(user); } } //********************************************** public IsometricPoint GetPosition() { ECPhysics ecPhysics = null; if (GetEC(ref ecPhysics)) { return ecPhysics.Position; } return null; } //********************************************** public void SetPosition(IsometricPoint point) { ECPhysics ecPhysics = null; if (GetEC(ref ecPhysics)) { ecPhysics.Position.Copy(point); } } //********************************************** public int GetOrientation() { ECPhysics ecPhysics = null; if (GetEC(ref ecPhysics)) { return ecPhysics.Orientation; } return 0; } //********************************************** public void SetOrientation(int orientation) { ECPhysics ecPhysics = null; if (GetEC(ref ecPhysics)) { ecPhysics.Orientation = orientation; } } //********************************************** public Entity GetParentEntity() { return _parentEntity; } //********************************************** public void Pulse() { lock (_childLock) { int numberOfChildren = _children.Count; Parallel.For(0, numberOfChildren, (i) => { _children[i].Pulse(); } ); } int numberOfComponents = _entityComponents.Count; Parallel.For(0, numberOfComponents, (i) => { _entityComponents[i].Pulse(); } ); } } }