public bool CanSeeOrHear(Entity source, Entity target, out bool canHear) { var sourcePos = source.GetPosition(); var dir = target.GetPosition() - sourcePos; var ray = new Ray(sourcePos, dir.normalized); var rayLimit = Physics.RaycastNonAlloc(ray, _rayHits, dir.magnitude, UnitCheckMask); _rayHits.SortByDistanceAsc(rayLimit); canHear = true; for (int i = 0; i < rayLimit; i++) { if (_rayHits[i].transform.CompareTag(StringConst.TagEnvironment)) { canHear = false; continue; } var entity = UnityToEntityBridge.GetEntity(_rayHits[i].collider); if (entity == null) { continue; } if (entity == source) { continue; } if (entity == target) { return(true); } } return(true); }
void OnTriggerEnter(Collider other) { if (!enabled || !_canGenerateCollisions || _checkedColliders.Contains(other)) { return; } _checkedColliders.Add(other); var hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(other)); var entity = EntityController.GetEntity(EntityID); if (hitEntity == null || hitEntity.Id == EntityID) { return; } if (!CollisionCheckSystem.IsValidCollision(entity, _limitToEnemy, hitEntity, other, out var sourceNode, out var targetNode)) { return; } var position = transform.position; var hitPnt = other.ClosestPointOnBounds(position); var hitNormal = (hitPnt - position).normalized; #if DEBUG DebugExtension.DrawPoint(hitPnt, Color.yellow, 1.5f, 4f); #endif hitEntity.Post(new CollisionEvent(entity, sourceNode, targetNode, hitPnt, hitNormal)); entity.Post(new PerformedCollisionEvent(sourceNode, targetNode, hitPnt, hitNormal)); }
private void UpdateNode(ref NpcFlyingNode npc) { if (npc.SensorTargets.WatchTargets.Count == 0) { npc.TryWander(); return; } var target = npc.SensorTargets.WatchTargets[0].Target; if (!npc.Chasing) { npc.Chase(target); } if (npc.Projectile.ShootTimer.IsActive) { return; } if (Physics.Raycast( npc.Projectile.ShootPivot.Tr.position, npc.Projectile.ShootPivot.Tr.forward, out var hit, 50, LayerMasks.DefaultCollision)) { if (UnityToEntityBridge.GetEntity(hit.collider) == target) { npc.Stop(); npc.Projectile.Fire(); } } }
//private Entity FindArenaActorInRange(Entity source, float range, TargetType targeting) { // List<Entity> actorList = new List<Entity>(); // switch (targeting) { // case TargetType.Self: // return source; // case TargetType.Friendly: // World.Get<FactionSystem>().FillFactionFriendsList(actorList, source.Get<FactionComponent>().Faction); // break; // default: // World.Get<FactionSystem>().FillFactionEnemiesList(actorList, source.Get<FactionComponent>().Faction); // break; // } // actorList.Shuffle(); // var ranksCanBust = (int) range; // for (int i = 0; i < actorList.Count; i++) { // if (actorList[i].PositionRank <= ranksCanBust) { // return actorList[i]; // } // } // return null; //} private bool CanSeeTarget(int rayLimit, Entity source, Entity target) { _rayHits.SortByDistanceAsc(rayLimit); for (int i = 0; i < rayLimit; i++) { if (_rayHits[i].transform.CompareTag(StringConst.TagEnvironment)) { return(false); } var colliderEntity = UnityToEntityBridge.GetEntity(_rayHits[i].collider); if (colliderEntity == null || colliderEntity == source) { continue; } if (colliderEntity == target) { return(true); } if (World.Get <FactionSystem>().AreEnemies(colliderEntity, source)) { return(false); } } return(true); }
void OnCollisionEnter(Collision collision) { if (!enabled || !_canGenerateCollisions || _checkedColliders.Contains(collision.collider)) { return; } var other = collision.collider; _checkedColliders.Add(other); var hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(other)); var entity = EntityController.GetEntity(EntityID); if (hitEntity == null || hitEntity.Id == EntityID) { return; } if (!CollisionCheckSystem.IsValidCollision(entity, _limitToEnemy, hitEntity, other, out var sourceNode, out var targetNode)) { return; } var collisionPnt = collision.contacts[0]; var hitPnt = collisionPnt.point; var hitNormal = collisionPnt.normal; #if DEBUG DebugExtension.DrawPoint(hitPnt, Color.magenta, 1.5f, 4f); #endif hitEntity.Post(new CollisionEvent(entity, sourceNode, targetNode, hitPnt, hitNormal)); entity.Post(new PerformedCollisionEvent(sourceNode, targetNode, hitPnt, hitNormal)); }
private void RunUpdate(ref UnitySensorNode node) { node.Sensor.Sensor.Pulse(); node.Targets.UpdateWatchTargets(); for (int i = 0; i < node.Sensor.Sensor.DetectedColliders.Count; i++) { var enemy = UnityToEntityBridge.GetEntity(node.Sensor.Sensor.DetectedColliders[i]); if (enemy == null || enemy == node.Entity) { continue; } var faction = enemy.Get <FactionComponent>(); if (!World.Get <FactionSystem>().AreEnemies(faction, node.Faction)) { continue; } node.Targets.AddWatch(enemy, true); Console.Log(node.Entity.DebugId + " saw " + enemy.DebugId); } if (node.Targets.WatchTargets.Count != 0) { return; } _tempEnemyList.Clear(); World.Get <FactionSystem>().FillFactionEnemiesList(_tempEnemyList, node.Faction.Faction); var nodePos = node.Tr.position.WorldToGenericGrid(HearingSectorSize); for (int f = 0; f < _tempEnemyList.Count; f++) { var enemy = _tempEnemyList[f]; var tr = enemy.Get <TransformComponent>(); if (tr == null) { Debug.Log("Enemy has no TR " + enemy.DebugId); continue; } if (tr.position.WorldToGenericGrid(HearingSectorSize) != nodePos) { continue; } var hearingChance = HearingChance; if (enemy.Tags.Contain(EntityTags.PerformingCommand)) { hearingChance *= 4; } else if (enemy.Tags.Contain(EntityTags.Moving)) { hearingChance *= 2; } if (Game.Random.DiceRollSucess(hearingChance)) { if (!Physics.Linecast(tr.position, node.Tr.position, LayerMasks.Walls)) { node.Targets.AddWatch(enemy, false); Console.Log(node.Entity.DebugId + " heard " + enemy.DebugId); } } } }
void OnCollisionEnter(Collision collision) { if (collision.transform.CompareTag(StringConst.TagPlayer)) { var entity = UnityToEntityBridge.GetEntity(collision.collider).GetTemplate <CharacterTemplate>(); if (entity != null) { World.Get <RulesSystem>().Post(new HealingEvent(null, _amount, entity, entity, _vital)); ItemPool.Despawn(gameObject); } } }
void OnCollisionEnter(Collision collision) { if (collision.transform.CompareTag(StringConst.TagPlayer)) { var entity = UnityToEntityBridge.GetEntity(collision.collider); if (entity != null) { entity.Post(new HealingEvent(_amount, entity, entity, _vital)); ItemPool.Despawn(gameObject); } } }
private static CollisionEvent?CheckRayList(Entity originEntity, int limit, bool limitEnemy) { for (int i = 0; i < limit; i++) { if (originEntity.IsDestroyed()) { return(null); } var hit = _rayHits[i]; Entity hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(hit.collider)); bool isEnvironment = hitEntity == null && hit.transform.IsEnvironment(); #if DEBUG_RAYCAST Color pointColor = Color.white; if (isEnvironment) { pointColor = Color.green; } else if (hit.transform.CompareTag(StringConst.TagInvalidCollider)) { pointColor = Color.magenta; } else if (hitEntity != null) { pointColor = Color.red; } DebugExtension.DebugPoint(_rayHits[i].point + (Vector3.up * (i * 0.1f)), pointColor, 0.25f, 2.5f); #endif if (isEnvironment) { #if DEBUG DebugLog.Add(originEntity.DebugId + " hit environment " + _rayHits[i].transform.name); #endif originEntity.Post(new EnvironmentCollisionEvent(originEntity, _rayHits[i].point, _rayHits[i].normal)); return(null); } if (hitEntity == null) { continue; } if (IsValidCollision(originEntity, limitEnemy, hitEntity, _rayHits[i].collider, out var sourceNode, out var targetNode)) { var ce = new CollisionEvent(originEntity, sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal); hitEntity.Post(ce); originEntity.Post(new PerformedCollisionEvent(sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal)); return(ce); } } return(null); }
private void Clear() { this.Post(new EntityDisposed(this)); UnityToEntityBridge.Unregister(this); EntityController.FinishDeleteEntity(this); _components.Clear(); Id = -1; Name = "Destroyed"; ClearParent(); if (_tags != null) { _tagPool.Store(_tags); _tags = null; } }
private void HandleLoader(EquipmentChanged arg, ModelLoaderComponent loader) { if (!loader.OnlyActiveWhileEquipped) { return; } var entity = arg.Owner; if (arg.Owner == null && loader.LoadedComponents.Count > 0) { UnityToEntityBridge.Unregister(entity); for (int i = 0; i < loader.LoadedComponents.Count; i++) { entity.Remove(loader.LoadedComponents[i]); } loader.LoadedComponents.Clear(); loader.LoadedModel = null; } else if (arg.Owner != null && loader.LoadedModel == null) { var parent = arg.Slot.EquipTr; var model = ItemPool.Spawn(loader.ModelName); if (parent != null) { model.transform.SetParentResetPos(parent); } loader.LoadedModel = new CachedGenericComponent <IRenderingComponent>(model.GetComponent <IRenderingComponent>()); if (loader.LoadedModel == null) { ItemPool.Despawn(model.gameObject); return; } UnityToEntityBridge.RegisterToEntity(model.gameObject, entity); loader.LoadedComponents.Add(entity.Add(new RenderingComponent(loader.LoadedModel.Value)).GetType()); loader.LoadedComponents.Add(entity.Add(new TransformComponent(model.Transform)).GetType()); loader.LoadedComponents.Add( entity.Add(new FloatingTextStatusComponent(model.Transform, new Vector3(0, 1.5f, 0))).GetType()); var rb = model.GetComponent <Rigidbody>(); if (rb != null) { loader.LoadedComponents.Add(entity.Add(new RigidbodyComponent(rb)).GetType()); } } }
public bool TryStore(Entity entity) { if (_poolDict.TryGetValue(entity.Get <TypeId>().Id, out var stack)) { if (!stack.IsFull) { UnityToEntityBridge.Unregister(entity); entity.Get <RenderingComponent>().Clear(); entity.Remove(typeof(SpriteAnimationComponent)); entity.Tags.Clear(); entity.Post(new ProjectileDespawned(entity)); entity.Pooled = true; entity.ClearParent(); stack.Add(entity); return(true); } } //entity.Destroy(); return(false); }
public void Handle(ReceivedDamageEvent arg) { var sensorTargets = arg.Target.Entity.Find <SensorTargetsComponent>(); //#if DEBUG // DebugLog.Add( // sensorTargets.GetEntity().DebugId + " was attacked by " + arg.Origin?.Entity.DebugId + " parent " + // arg.Origin?.Entity.ParentId + " is pooled " + arg.Origin?.Entity.Pooled); //#endif sensorTargets.AddWatch(arg.Origin, true); if (sensorTargets.Shouted) { return; } var sourceFaction = arg.Target.Faction.Value; var factionSystem = World.Get <FactionSystem>(); sensorTargets.Shouted = true; var sourcePos = arg.Target.Tr.position; var limit = Physics.OverlapSphereNonAlloc(sourcePos, ShoutRadius, _shoutColliders, LayerMasks.DefaultCollision); for (int i = 0; i < limit; i++) { var sensorTemplate = UnityToEntityBridge.GetEntity(_shoutColliders[i]).GetTemplate <UnitySensorTemplate>(); if (sensorTemplate == null) { continue; } if (!factionSystem.AreFriends(sourceFaction, sensorTemplate.Faction.Value)) { continue; } if (Physics.Linecast(sourcePos, sensorTemplate.Tr.position, LayerMasks.Walls)) { continue; } sensorTemplate.Targets.AddWatch(arg.Origin, false); } }
public Entity GetTarget(Entity source, Vector3 dir, float range, TargetType targeting) { //if (GameOptions.ArenaCombat) { // return FindArenaActorInRange(source, range, targeting); //} var sourcePos = source.GetPosition(); var ray = new Ray(sourcePos, dir); var rayLimit = Physics.RaycastNonAlloc(ray, _rayHits, range, UnitCheckMask); _rayHits.SortByDistanceAsc(rayLimit); for (int i = 0; i < rayLimit; i++) { if (_rayHits[i].transform.CompareTag(StringConst.TagEnvironment)) { return(null); } var entity = UnityToEntityBridge.GetEntity(_rayHits[i].collider); if (entity == null) { continue; } if (entity == source) { continue; } var target = EntityController.GetEntity(entity); if (targeting == TargetType.Enemy && !World.Get <FactionSystem>().AreEnemies(source, target)) { continue; } if (targeting == TargetType.Friendly && !World.Get <FactionSystem>().AreFriends(source, target)) { continue; } return(target); } return(null); }
private static void CheckColliderList(Entity originEntity, Entity ignoreEntity, Vector3 position, int limit, bool limitEnemy) { for (int i = 0; i < limit; i++) { if (originEntity.IsDestroyed()) { return; } var collider = _colliders[i]; var hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(collider)); if (hitEntity == ignoreEntity || hitEntity == null) { continue; } if (IsValidCollision(originEntity, limitEnemy, hitEntity, collider, out var sourceNode, out var targetNode)) { CollisionExtensions.GenerateHitLocDir(position, hitEntity, collider, out var hitPnt, out var hitNormal); var ce = new CollisionEvent(originEntity, sourceNode, targetNode, hitPnt, hitNormal); hitEntity.Post(ce); originEntity.Post(new PerformedCollisionEvent(sourceNode, targetNode, hitPnt, hitNormal)); } } }
private static CollisionEvent?CheckRayList(Entity originEntity, Ray ray, int limit, bool limitEnemy) { for (int i = 0; i < limit; i++) { if (originEntity.IsDestroyed()) { return(null); } var hit = _rayHits[i]; Entity hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(hit.collider)); bool isEnvironment = hitEntity == null && hit.transform.IsEnvironment(); #if DEBUG_RAYCAST Color pointColor = Color.white; if (isEnvironment) { pointColor = Color.green; } else if (hit.transform.CompareTag(StringConst.TagInvalidCollider)) { pointColor = Color.magenta; } else if (hitEntity != null) { pointColor = Color.red; } DebugExtension.DrawPoint(_rayHits[i].point + (Vector3.up * (i * 0.1f)), pointColor, 0.25f, 2.5f); #endif if (isEnvironment) { #if DEBUG DebugLog.Add(originEntity.DebugId + " hit environment " + _rayHits[i].transform.name); #endif originEntity.Post(new EnvironmentCollisionEvent(originEntity, _rayHits[i].point, _rayHits[i].normal)); return(null); } if (hitEntity == null) { continue; } if (IsValidCollision(originEntity, limitEnemy, hitEntity, _rayHits[i].collider, out var sourceNode, out var targetNode)) { if (targetNode.DetailCollider != null) { var detailTr = targetNode.DetailCollider.Collider.transform; var localPoint = hit.transform.InverseTransformPoint(ray.origin); var localDir = hit.transform.InverseTransformDirection(ray.direction); var rayCast = new Ray(detailTr.TransformPoint(localPoint), detailTr.TransformDirection(localDir)); if (!targetNode.DetailCollider.Collider.Raycast(rayCast, out var childHit, 500)) { DebugExtension.DrawPoint(childHit.point, Color.yellow, 0.25f, 2.5f); continue; } } var ce = new CollisionEvent(originEntity, sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal); hitEntity.Post(ce); originEntity.Post(new PerformedCollisionEvent(sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal)); return(ce); } } return(null); }
void Update() { if (!Game.GameActive) { return; } //if (_debug) { // DebugText.UpdatePermText("World Control", "Game Not Active"); //} //if (GenericDraggableController.Dragging) { // if (_debug) { // DebugText.UpdatePermText("World Control", "Dragging"); // } // return; //} _foundControl = false; //_eventData.position = Input.mousePosition; //EventSystem.current.RaycastAll(_eventData, _result); if (PlayerInputSystem.IsCursorOverUI) { SetCurrentNull(); //if (_debug) { // var go = PlayerInput.CurrentInput != null ? PlayerInput.CurrentInput.GameObjectUnderPointer() : null; // DebugText.UpdatePermText("World Control", string.Format("Over UI: {0}", go != null ? go.name : // EventSystem.current.currentSelectedGameObject != null ? EventSystem.current.currentSelectedGameObject.name : " null ?")); //} return; } _mouseRay = Cam.ScreenPointToRay(Input.mousePosition); Entity currentActor = null; var cnt = Physics.RaycastNonAlloc(_mouseRay, _hits, _currentEnemyRayDistance, _worldControlsMask); _hits.SortByDistanceAsc(cnt); for (int i = 0; i < cnt; i++) { var hit = _hits[i]; if (!_foundControl) { if (hit.transform.gameObject == _currentGameObject) { _foundControl = true; continue; } if (hit.distance <= _currentRayDistance) { hit.transform.GetComponentsInChildren(_tempControls); for (int j = 0; j < _tempControls.Count; j++) { if (_tempControls[j] == null) { continue; } if (_tempControls[j].WorldControlActive) { SetNewCurrent(_tempControls[j], hit.transform.gameObject); _foundControl = true; break; } } } } if (currentActor == null) { currentActor = UnityToEntityBridge.GetEntity(hit.collider); } if (LayerMasks.Environment.ContainsLayer(hit.transform.gameObject.layer)) { if (_debug) { DebugText.UpdatePermText("World Control", "Environment " + cnt); } break; } } if (currentActor != null) { if (_debug) { DebugText.UpdatePermText("World Control", currentActor.Id.ToString()); } } //RaycastHit hit; //if (Physics.Raycast(_mouseRay, out hit, _enemyDistance, _worldControlsMask)) { // if (hit.transform.gameObject == _currentGameObject) { // _foundControl = true; // } // else { // if (hit.distance <= _rayDistance) { // var newCurrent = hit.transform.GetComponent<IWorldControl>(); // if (newCurrent != null) { // SetNewCurrent(newCurrent, hit.transform.gameObject); // _foundControl = true; // } // } // else { // if (currentActor == null) { // Actor.Actors.TryGetValue(hit.collider, out currentActor); // } // } // } //} UICenterTarget.SetTargetActor(currentActor.GetTemplate <CharacterTemplate>()); if (!_foundControl || currentActor != null) { SetCurrentNull(); } if (_debug) { DebugText.UpdatePermText("World Control", string.Format("Current: {0}", _currentGameObject != null ? _currentGameObject.name : "None")); } //if (_current == null && AlternativeUse == null) { // var wasActive = GenericDraggableController.HasTarget; // if (GenericDraggableController.main.CanDrag()) { // UICenterTarget.SetText("Drag"); // } // else if (wasActive){ // UICenterTarget.Clear(); // } //} //else { // GenericDraggableController.ClearTarget(); //} }
public void OnSystemUpdate(float dt) { switch (_state) { case State.FinishingFall: if (!FoundFloor()) { _state = State.Falling; } else { transform.position = _floorLerp.Get(); if (!_floorLerp.Active) { _state = State.Disabled; } } break; case State.Pulling: if (_pullTarget == null) { CheckState(); break; } var dir = (_pullTarget.position - transform.position); var distance = dir.magnitude; if (distance > _pullRange.Max * 1.5f) { CheckState(); } else { transform.position = Vector3.MoveTowards(transform.position, _pullTarget.position, Mathf.Lerp(_pullSpeed * 0.25f, _pullSpeed, Mathf.InverseLerp(_pullRange.Max, _pullRange.Min, distance)) * dt); } break; case State.Falling: transform.Translate(-transform.up * _gravitySpeed * dt); CheckState(); break; case State.Throwing: if (!_arcMover.Active) { CheckState(); break; } _arcMover.Get(transform); break; } if (_collisionRadius > 0 && _collisions.Length > 0) { var cnt = Physics.OverlapSphereNonAlloc(transform.position, _collisionRadius, _tempColliders, _collisionMask); for (int c = 0; c < cnt; c++) { var collEntity = UnityToEntityBridge.GetEntity(_tempColliders[c]); if (collEntity == null) { continue; } for (int i = 0; i < _collisions.Length; i++) { _collisions[i].Collision(collEntity); } } } if (_pullRange.Max <= 0) { return; } switch (_state) { case State.Pulling: case State.Throwing: return; } var pullCnt = Physics.OverlapSphereNonAlloc(transform.position, _pullRange.Max, _tempColliders, _pullMask); for (int c = 0; c < pullCnt; c++) { var coll = _tempColliders[c]; if (_playerOnly && !coll.transform.CompareTag(StringConst.TagPlayer)) { continue; } _pullTarget = _tempColliders[0].transform; _state = State.Pulling; break; } }
public Entity SpawnProjectile(Entity owner, string id, Vector3 target, Vector3 spawnPos, Quaternion spawnRot) { if (_templates.Count == 0) { Init(); } if (!_templates.TryGetValue(id, out var template)) { #if DEBUG DebugLog.Add("Couldn't find project template " + id, false); #endif return(null); } var entity = GetProjectile(template); var node = entity.GetNode <ProjectileNode>(); node.MoveTarget.SetMoveTarget(target); if (template.ActionFx != null) { node.ActionFx.ChangeFx(template.ActionFx); } var prefab = ItemPool.Spawn(UnityDirs.ActionSpawn, template.Type, spawnPos, spawnRot); if (prefab == null) { return(entity); } var spawn = prefab.GetComponent <IProjectile>(); entity.Add(new TransformComponent(prefab.Transform)); switch (template.Type) { default: case "Simple": break; case "SpriteAnimation": spawn.SetColor(template.MainColor, Color.white * template.GlowPower); if (template.Animation != null) { var spriteRenderer = prefab.Renderers[0] as SpriteRenderer; entity.Add(new SpriteAnimationComponent(spriteRenderer, template.Animation, false, template.Billboard)); } break; case "VolumeLaser": spawn.SetColor(template.MainColor, template.OffsetColor); break; } spawn.SetSize(template.Size, template.Length); switch (template.Movement) { case "Forward": case "Arc": prefab.Transform.LookAt(target, prefab.Transform.up); break; case "Force": //var force = transform.forward * ForceRange.Lerp(Mathf.Clamp01(charging.ElapsedTime / MaxChargeTime)); break; } if (spawn.Rigidbody != null) { node.Rb.SetRb(spawn.Rigidbody); } entity.Tags.Add(EntityTags.Moving); node.Rendering.Set(spawn); UnityToEntityBridge.RegisterToEntity(prefab.Transform.gameObject, entity); entity.ParentId = owner.Id; entity.Post(new ProjectileSpawned(template, entity)); return(entity); }