public static BasicBeamController GenerateBeamPrefab(this Projectile projectile, string spritePath, Vector2 colliderDimensions, Vector2 colliderOffsets, List <string> beamAnimationPaths = null, int beamFPS = -1, List <string> impactVFXAnimationPaths = null, int beamImpactFPS = -1, Vector2?impactVFXColliderDimensions = null, Vector2?impactVFXColliderOffsets = null, List <string> endVFXAnimationPaths = null, int beamEndFPS = -1, Vector2?endVFXColliderDimensions = null, Vector2?endVFXColliderOffsets = null, List <string> muzzleVFXAnimationPaths = null, int beamMuzzleFPS = -1, Vector2?muzzleVFXColliderDimensions = null, Vector2?muzzleVFXColliderOffsets = null)
        {
            try
            {
                float convertedColliderX = colliderDimensions.x / 16f;
                float convertedColliderY = colliderDimensions.y / 16f;
                float convertedOffsetX   = colliderOffsets.x / 16f;
                float convertedOffsetY   = colliderOffsets.y / 16f;

                int             spriteID    = SpriteBuilder.AddSpriteToCollection(spritePath, ETGMod.Databases.Items.ProjectileCollection);
                tk2dTiledSprite tiledSprite = projectile.gameObject.GetOrAddComponent <tk2dTiledSprite>();



                tiledSprite.SetSprite(ETGMod.Databases.Items.ProjectileCollection, spriteID);
                tk2dSpriteDefinition def = tiledSprite.GetCurrentSpriteDef();
                def.colliderVertices = new Vector3[] {
                    new Vector3(convertedOffsetX, convertedOffsetY, 0f),
                    new Vector3(convertedColliderX, convertedColliderY, 0f)
                };

                def.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.MiddleLeft);

                //tiledSprite.anchor = tk2dBaseSprite.Anchor.MiddleCenter;
                tk2dSpriteAnimator  animator  = projectile.gameObject.GetOrAddComponent <tk2dSpriteAnimator>();
                tk2dSpriteAnimation animation = projectile.gameObject.GetOrAddComponent <tk2dSpriteAnimation>();
                animation.clips  = new tk2dSpriteAnimationClip[0];
                animator.Library = animation;
                UnityEngine.Object.Destroy(projectile.GetComponentInChildren <tk2dSprite>());
                BasicBeamController beamController = projectile.gameObject.GetOrAddComponent <BasicBeamController>();

                //---------------- Sets up the animation for the main part of the beam
                if (beamAnimationPaths != null)
                {
                    tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip()
                    {
                        name = "beam_idle", frames = new tk2dSpriteAnimationFrame[0], fps = beamFPS
                    };
                    List <string> spritePaths = beamAnimationPaths;

                    List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>();
                    foreach (string path in spritePaths)
                    {
                        tk2dSpriteCollectionData collection = ETGMod.Databases.Items.ProjectileCollection;
                        int frameSpriteId             = SpriteBuilder.AddSpriteToCollection(path, collection);
                        tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId];
                        frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.MiddleLeft);
                        frameDef.colliderVertices = def.colliderVertices;
                        frames.Add(new tk2dSpriteAnimationFrame {
                            spriteId = frameSpriteId, spriteCollection = collection
                        });
                    }
                    clip.frames     = frames.ToArray();
                    animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray();
                    beamController.beamAnimation = "beam_idle";
                }

                //------------- Sets up the animation for the part of the beam that touches the wall
                if (endVFXAnimationPaths != null && endVFXColliderDimensions != null && endVFXColliderOffsets != null)
                {
                    SetupBeamPart(animation, endVFXAnimationPaths, "beam_end", beamEndFPS, (Vector2)endVFXColliderDimensions, (Vector2)endVFXColliderOffsets);
                    beamController.beamEndAnimation = "beam_end";
                }
                else
                {
                    SetupBeamPart(animation, beamAnimationPaths, "beam_end", beamFPS, null, null, def.colliderVertices);
                    beamController.beamEndAnimation = "beam_end";
                }

                //---------------Sets up the animaton for the VFX that plays over top of the end of the beam where it hits stuff
                if (impactVFXAnimationPaths != null && impactVFXColliderDimensions != null && impactVFXColliderOffsets != null)
                {
                    SetupBeamPart(animation, impactVFXAnimationPaths, "beam_impact", beamImpactFPS, (Vector2)impactVFXColliderDimensions, (Vector2)impactVFXColliderOffsets);
                    beamController.impactAnimation = "beam_impact";
                }

                //--------------Sets up the animation for the very start of the beam
                if (muzzleVFXAnimationPaths != null && muzzleVFXColliderDimensions != null && muzzleVFXColliderOffsets != null)
                {
                    SetupBeamPart(animation, muzzleVFXAnimationPaths, "beam_start", beamMuzzleFPS, (Vector2)muzzleVFXColliderDimensions, (Vector2)muzzleVFXColliderOffsets);
                    beamController.beamStartAnimation = "beam_start";
                }
                else
                {
                    SetupBeamPart(animation, beamAnimationPaths, "beam_start", beamFPS, null, null, def.colliderVertices);
                    beamController.beamStartAnimation = "beam_start";
                }

                return(beamController);
            }
            catch (Exception e)
            {
                ETGModConsole.Log(e.ToString());
                return(null);
            }
        }
    public override void OnInspectorGUI()
    {
        tk2dTiledSprite sprite = (tk2dTiledSprite)target;

        base.OnInspectorGUI();

        if (sprite.Collection == null)
        {
            return;
        }


        EditorGUILayout.BeginVertical();

        var spriteData = sprite.GetCurrentSpriteDef();

        // need raw extents (excluding scale)
        Vector3 extents = spriteData.boundsData[1];

        bool newCreateBoxCollider = EditorGUILayout.Toggle("Create Box Collider", sprite.CreateBoxCollider);

        if (newCreateBoxCollider != sprite.CreateBoxCollider)
        {
            Undo.RegisterUndo(targetTiledSprites, "Create Box Collider");
            sprite.CreateBoxCollider = newCreateBoxCollider;
        }

        // if either of these are zero, the division to rescale to pixels will result in a
        // div0, so display the data in fractions to avoid this situation
        bool editBorderInFractions = true;

        if (spriteData.texelSize.x != 0.0f && spriteData.texelSize.y != 0.0f && extents.x != 0.0f && extents.y != 0.0f)
        {
            editBorderInFractions = false;
        }

        if (!editBorderInFractions)
        {
            Vector2 newDimensions = EditorGUILayout.Vector2Field("Dimensions (Pixel Units)", sprite.dimensions);
            if (newDimensions != sprite.dimensions)
            {
                Undo.RegisterUndo(targetTiledSprites, "Tiled Sprite Dimensions");
                foreach (tk2dTiledSprite spr in targetTiledSprites)
                {
                    spr.dimensions = newDimensions;
                }
            }

            tk2dTiledSprite.Anchor newAnchor = (tk2dTiledSprite.Anchor)EditorGUILayout.EnumPopup("Anchor", sprite.anchor);
            if (newAnchor != sprite.anchor)
            {
                Undo.RegisterUndo(targetTiledSprites, "Tiled Sprite Anchor");
                foreach (tk2dTiledSprite spr in targetTiledSprites)
                {
                    spr.anchor = newAnchor;
                }
            }
        }
        else
        {
            GUILayout.Label("Border (Displayed as Fraction).\nSprite Collection needs to be rebuilt.", "textarea");
        }

        Mesh mesh = sprite.GetComponent <MeshFilter>().sharedMesh;

        if (mesh != null)
        {
            GUILayout.Label(string.Format("Triangles: {0}", mesh.triangles.Length / 3));
        }

        // One of the border valus has changed, so simply rebuild mesh data here
        if (GUI.changed)
        {
            foreach (tk2dTiledSprite spr in targetTiledSprites)
            {
                spr.Build();
                EditorUtility.SetDirty(spr);
            }
        }

        EditorGUILayout.EndVertical();
    }
Exemple #3
0
        /// <summary>
        /// Adds a tiled trail to the Projectile
        /// </summary>
        /// <param name="timeTillAnimStart">How long after spawning until the trail will begin to play it's animation, if it has one.</param>
        public static void AddTrailToProjectile(this Projectile target, string spritePath, Vector2 colliderDimensions, Vector2 colliderOffsets, List <string> animPaths = null, int animFPS = -1, List <string> startAnimPaths = null, int startAnimFPS = -1, float timeTillAnimStart = -1, float cascadeTimer = -1, float softMaxLength = -1, bool destroyOnEmpty = false)
        {
            try
            {
                GameObject newTrailObject = new GameObject();
                newTrailObject.InstantiateAndFakeprefab();
                newTrailObject.transform.parent = target.transform;

                float convertedColliderX = colliderDimensions.x / 16f;
                float convertedColliderY = colliderDimensions.y / 16f;
                float convertedOffsetX   = colliderOffsets.x / 16f;
                float convertedOffsetY   = colliderOffsets.y / 16f;

                int             spriteID    = SpriteBuilder.AddSpriteToCollection(spritePath, ETGMod.Databases.Items.ProjectileCollection);
                tk2dTiledSprite tiledSprite = newTrailObject.GetOrAddComponent <tk2dTiledSprite>();

                tiledSprite.SetSprite(ETGMod.Databases.Items.ProjectileCollection, spriteID);
                tk2dSpriteDefinition def = tiledSprite.GetCurrentSpriteDef();
                def.colliderVertices = new Vector3[] {
                    new Vector3(convertedOffsetX, convertedOffsetY, 0f),
                    new Vector3(convertedColliderX, convertedColliderY, 0f)
                };

                def.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.MiddleLeft);

                tk2dSpriteAnimator  animator  = newTrailObject.GetOrAddComponent <tk2dSpriteAnimator>();
                tk2dSpriteAnimation animation = newTrailObject.GetOrAddComponent <tk2dSpriteAnimation>();
                animation.clips  = new tk2dSpriteAnimationClip[0];
                animator.Library = animation;

                TrailController trail = newTrailObject.AddComponent <TrailController>();

                //---------------- Sets up the animation for the main part of the trail
                if (animPaths != null)
                {
                    BeamToolbox.SetupBeamPart(animation, animPaths, "trail_mid", animFPS, null, null, def.colliderVertices);
                    trail.animation     = "trail_mid";
                    trail.usesAnimation = true;
                }
                else
                {
                    trail.usesAnimation = false;
                }

                if (startAnimPaths != null)
                {
                    BeamToolbox.SetupBeamPart(animation, startAnimPaths, "trail_start", startAnimFPS, null, null, def.colliderVertices);
                    trail.startAnimation     = "trail_start";
                    trail.usesStartAnimation = true;
                }
                else
                {
                    trail.usesStartAnimation = false;
                }

                //Trail Variables
                if (softMaxLength > 0)
                {
                    trail.usesSoftMaxLength = true; trail.softMaxLength = softMaxLength;
                }
                if (cascadeTimer > 0)
                {
                    trail.usesCascadeTimer = true; trail.cascadeTimer = cascadeTimer;
                }
                if (timeTillAnimStart > 0)
                {
                    trail.usesGlobalTimer = true; trail.globalTimer = timeTillAnimStart;
                }
                trail.destroyOnEmpty = destroyOnEmpty;
            }
            catch (Exception e)
            {
                ETGModConsole.Log(e.ToString());
            }
        }