示例#1
0
        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);
        }
示例#2
0
        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);
        }