private void OnFire(GameEvent e) { // Get entity which fired. var entityId = (int)e.EventData; var weaponEntity = this.weaponEntities.GetEntity(entityId); if (weaponEntity == null) { return; } // Check if bullets left. if (weaponEntity.Weapon.BulletCount <= 0) { // No bullets left. return; } // Shoot. var projectileEntityId = this.Shoot(entityId, weaponEntity); // Decrease bullets. --weaponEntity.Weapon.BulletCount; // Fire event. this.EventManager.QueueEvent( WeaponEvent.Fired, new WeaponFiredData { WeaponEntityId = entityId, BulletEntityId = projectileEntityId }); }
private void OnMove(GameEvent e) { MoveData data = (MoveData)e.EventData; InputEntity inputEntity = this.inputEntities.GetEntity(data.EntityId); if (inputEntity == null) { return; } // Adjust move direction. if (data.Enable) { inputEntity.Input.MoveDirections = (MoveDirection)inputEntity.Input.MoveDirections.OrOption(data.Direction, typeof(MoveDirection)); } else { inputEntity.Input.MoveDirections = (MoveDirection) inputEntity.Input.MoveDirections.AndComplementOption(data.Direction, typeof(MoveDirection)); } // Adjust velocity. inputEntity.Movement.Velocity = GetDirection(inputEntity.Input.MoveDirections) * inputEntity.Movement.MaxSpeed; }
private void OnPlayerCreated(GameEvent e) { var playerEntityId = (int)e.EventData; // Get components of player that contain data. this.weaponComponent = this.EntityManager.GetComponent<WeaponComponent>(playerEntityId); // Update data. this.UpdateBulletCounts(); }
private void OnFire(GameEvent e) { var entityId = (int)e.EventData; var inputEntity = this.inputEntities.GetEntity(entityId); if (inputEntity == null) { return; } // Forward action to weapon feature. this.EventManager.QueueEvent(WeaponAction.Fire, entityId); }
private void OnSave(GameEvent e) { string path = (string)e.EventData; List<SerializedEntity> savedEntities = this.EntityManager.Entities.Select( entityId => new SerializedEntity { EntityId = entityId, Blueprint = this.EntityManager.Save(entityId) }) .ToList(); Savegame savegame = new Savegame { SavedEntities = savedEntities }; XmlSerializer xmlSerializer = new XmlSerializer(typeof(Savegame)); Stream stream = File.Open(path, FileMode.Truncate); xmlSerializer.Serialize(stream, savegame); }
private void OnReload(GameEvent e) { // Get entity which should be reloaded. var entityId = (int)e.EventData; var weaponEntity = this.weaponEntities.GetEntity(entityId); if (weaponEntity == null) { return; } // Reload weapon if it isn't full of bullets already. if (weaponEntity.Weapon.BulletCount >= weaponEntity.Weapon.MaxBulletCount) { return; } weaponEntity.Weapon.BulletCount = weaponEntity.Weapon.MaxBulletCount; this.EventManager.QueueEvent(WeaponEvent.Reloaded, entityId); }
private void OnLoad(GameEvent e) { string path = (string)e.EventData; XmlSerializer xmlSerializer = new XmlSerializer(typeof(Savegame)); Stream stream = File.Open(path, FileMode.Open); Savegame savegame = (Savegame)xmlSerializer.Deserialize(stream); // Clear entity manager before load. this.EntityManager.RemoveEntities(); this.EntityManager.CleanUpEntities(); foreach (var savedEntity in savegame.SavedEntities) { this.EntityManager.CreateEntity(savedEntity.EntityId); } foreach (var savedEntity in savegame.SavedEntities) { this.EntityManager.InitEntity(savedEntity.EntityId, savedEntity.Blueprint, null, null); } }
/// <summary> /// Adds the matching Unity component to the game object that represents /// the entity with the passed id. /// </summary> /// <param name="e"> Game event that has occurred. </param> private void OnComponentAdded(GameEvent e) { Profiler.BeginSample("Component added"); EntityComponentData eventArgs = (EntityComponentData)e.EventData; int entityId = eventArgs.EntityId; IEntityComponent component = eventArgs.Component; GameObject entityObject = this.entities[entityId]; // Check if a behaviour has to be attached which visualizes the logic state. foreach (LogicToVisualMapping logicToVisualMapping in this.logicVisualMappings.Where(mapping => mapping.LogicType == component.GetType())) { // NOTE: The component may already exist because we recycle existing entity objects and the old components // just get removed after the current update loop (see http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html). entityObject.AddComponent(logicToVisualMapping.VisualType); } Profiler.EndSample(); }
private void DelegateVisualEvent(int entityId, GameEvent e) { // Get entity object. GameObject entityObject; this.TryGetEntityObject(entityId, out entityObject); if (entityObject == null) { Debug.LogError( string.Format( "Received event {0} for visual behaviour, but no entity object with id {1} was found.", e.EventType, entityId)); return; } // Check which method to call. LogicToVisualDelegate logicToVisualDelegate = this.logicToVisualDelegates.FirstOrDefault( existingDelegate => existingDelegate.Event.Equals(e.EventType)); if (logicToVisualDelegate != null) { foreach (string callbackName in logicToVisualDelegate.CallbackNames) { entityObject.SendMessage(callbackName, e, SendMessageOptions.DontRequireReceiver); } } }
private void OnComponentRemoved(GameEvent e) { EntityComponentData eventArgs = (EntityComponentData)e.EventData; int entityId = eventArgs.EntityId; IEntityComponent component = eventArgs.Component; GameObject entityObject = this.entities[entityId]; // Check if a behaviour has to be removed which visualizes the logic state. foreach (LogicToVisualMapping logicToVisualMapping in this.logicVisualMappings) { if (component.GetType() != logicToVisualMapping.LogicType) { continue; } Component visualComponent = entityObject.GetComponent(logicToVisualMapping.VisualType); if (visualComponent != null) { Destroy(visualComponent); } break; } }
private void OnWeaponReloaded(GameEvent e) { // Update data. this.UpdateBulletCounts(); }
/// <summary> /// Queues the passed event to be processed later. /// </summary> /// <param name="e"> Event to queue. </param> public void QueueEvent(GameEvent e) { this.newEvents.Add(e); }
/// <summary> /// Notifies all interested listeners of the specified event. /// </summary> /// <param name="e">Event to pass to listeners.</param> private void ProcessEvent(GameEvent e) { // Check for listeners to all events. EventDelegate eventListeners = this.allEventListeners; if (eventListeners != null) { eventListeners(e); } if (this.listeners.TryGetValue(e.EventType, out eventListeners)) { if (eventListeners != null) { eventListeners(e); } else { this.OnUnhandledEvent(e.EventType); } } else { this.OnUnhandledEvent(e.EventType); } }
/// <summary> /// Destroys the game object representing the entity with the specified id. /// </summary> /// <param name="e"> Game event that has occurred. </param> private void OnEntityRemoved(GameEvent e) { int entityId = (int)e.EventData; GameObject entityObject; if (!this.entities.TryGetValue(entityId, out entityObject)) { Debug.LogError(string.Format("Entity object for entity with id '{0}' wasn't found.", entityId)); return; } Profiler.BeginSample("Remove entity"); // Reset entity object. this.ResetEntityObject(entityObject); if (this.EntityObjectPool != null) { this.EntityObjectPool.Free(entityObject); } this.entities.Remove(entityId); Profiler.EndSample(); }
/// <summary> /// Fires the passed event immediately, notifying all listeners. /// </summary> /// <param name="e"> Event to fire. </param> public void FireImmediately(GameEvent e) { this.ProcessEvent(e); }
private void OnVisualEvent(GameEvent e) { // Check for which entity the event is for. EntityEventData entityEventData = e.EventData as EntityEventData; if (entityEventData != null) { this.DelegateVisualEvent(entityEventData.EntityId, e); return; } Entity2Data entity2EventData = e.EventData as Entity2Data; if (entity2EventData != null) { this.DelegateVisualEvent(entity2EventData.First, e); this.DelegateVisualEvent(entity2EventData.Second, e); return; } Debug.LogError( string.Format( "Received event {0} for visual behaviour, but event data {1} wasn't derived from EntityEventData or Entity2Data to get entity id(s).", e.EventType, e.EventData.GetType())); }
private void OnEntityRemoved(GameEvent e) { var entityId = (int)e.EventData; if (entityId == this.EntityId) { Destroy(this.gameObject); } }
/// <summary> /// Called when a new system has been added. /// </summary> /// <param name="e"> Event that has occurred within the framework. </param> private void OnSystemAdded(GameEvent e) { this.testPassed = this.system.Equals(e.EventData); }
/// <summary> /// Called when the game starts. /// </summary> /// <param name="e"> Event that has occurred within the framework. </param> private void OnGameStarted(GameEvent e) { this.testPassed = true; }
/// <summary> /// Called when the game has been resumed. /// </summary> /// <param name="e"> Event that has occurred within the framework. </param> private void OnGameResumed(GameEvent e) { this.testPassed = true; }
/// <summary> /// Called when an entity has been removed. /// </summary> /// <param name="e"> Event that has occurred within the framework. </param> private void OnEntityRemoved(GameEvent e) { int entityId = (int)e.EventData; this.testPassed = entityId == this.testEntityId; }
/// <summary> /// Called when a component has been removed. /// </summary> /// <param name="e"> Event that has occurred within the framework. </param> private void OnComponentRemoved(GameEvent e) { EntityComponentData data = (EntityComponentData)e.EventData; this.testPassed = data.EntityId == this.testEntityId && this.entityComponent.Equals(data.Component); }
/// <summary> /// Fires the passed event after a short delay. /// </summary> /// <param name="delay">Time to wait before firing the event, in seconds.</param> /// <param name="e">Event to fire.</param> public void FireDelayed(float delay, GameEvent e) { if (delay > 0) { this.delayedEvents.Add(new DelayedEvent { TimeRemaining = delay, Event = e }); } else { this.FireImmediately(e); } }
/// <summary> /// Creates a new game object whenever a new entity has been created. /// </summary> /// <param name="e"> Game event that has occurred. </param> private void OnEntityCreated(GameEvent e) { int entityId = (int)e.EventData; // Check if entity is still alive, may already be removed due to different update frames. if (!this.game.EntityManager.EntityIsAlive(entityId)) { return; } if (this.entities.ContainsKey(entityId)) { Debug.LogError(string.Format("Entity object for entity with id '{0}' already exists.", entityId)); return; } Profiler.BeginSample("Create entity"); GameObject entityObject; if (this.EntityObjectPool != null) { entityObject = this.EntityObjectPool.Alloc(); } else { Debug.LogWarning("No entity object pool available, creating dummy entity object."); entityObject = new GameObject(); } #if UNITY_EDITOR Profiler.BeginSample("Change parent transform"); entityObject.transform.parent = this.entitiesRoot.transform; Profiler.EndSample(); #endif // Check for entity behaviour to set entity id. EntityBehaviour entityBehaviour = entityObject.GetComponent<EntityBehaviour>(); if (entityBehaviour != null) { entityBehaviour.EntityId = entityId; entityBehaviour.Game = this.game; } this.entities.Add(entityId, entityObject); Profiler.EndSample(); }
private void OnEvent(GameEvent e) { if (this.Disabled) { return; } string eventTypeString = this.ConvertEventTypeToString(e.EventType); if (!this.disabledEventTypes.Contains(eventTypeString)) { this.Info( e.EventData != null ? this.FormatLog(string.Format("{0}: {1}", eventTypeString, e.EventData)) : this.FormatLog(eventTypeString)); } }
private void OnGameStarted(GameEvent e) { this.CreatePlayerCharacter(); }