private void OnGUI() { float labelWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 64f; EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); currentAnimationId = EditorGUILayout.Popup("Animation", currentAnimationId, animationNames, GUILayout.Width(192f)); if (EditorGUI.EndChangeCheck()) { if (currentFrame > ac[currentAnimationId].frames.Count) { currentFrame = ac[currentAnimationId].frames.Count - 1; } } if (GUILayout.Button("-", GUILayout.Width(20f))) { string n = ac[currentAnimationId] == null ? "null" : ac[currentAnimationId].name; if (EditorUtility.DisplayDialog("Delete Animation", "Are you sure you want to delete " + n + "? This will also delete the asset.", "Yes", "No")) { List <SpriteAnimation> anims = new List <SpriteAnimation>(ac.animations); anims.RemoveAt(currentAnimationId); ac.animations = anims.ToArray(); UpdateAnimations(); } } if (GUILayout.Button("+", GUILayout.Width(20f))) { StringInputPopup.Display((s) => { Array.Resize(ref ac.animations, ac.Count + 1); var a = new SpriteAnimation(); a.name = s; a.frames = new List <SpriteFrame>(); ac[ac.Count - 1] = a; UpdateAnimations(); }, defaultAnimationNames[System.Math.Min(ac.Count, defaultAnimationNames.Length - 1)], "Enter new animation name."); } if (GUILayout.Button("copy", GUILayout.Width(40f))) { clipboardAnimation = ac.animations[currentAnimationId]; } if (GUILayout.Button("paste", GUILayout.Width(40f))) { ac.animations[currentAnimationId] = clipboardAnimation.Copy(); } EditorGUI.BeginChangeCheck(); int c = ac.Count > 0 ? (ac[currentAnimationId] == null ? 0 : ac[currentAnimationId].frames.Count) : 1; currentFrame = EditorGUILayout.IntSlider("Frame", currentFrame + 1, System.Math.Min(1, c), c) - 1; if (EditorGUI.EndChangeCheck()) { } if (GUILayout.Button("-", GUILayout.Width(20f))) { if (EditorUtility.DisplayDialog("Delete Frame", "Are you sure you want to delete frame #" + (currentFrame + 1) + "?", "Yes", "No")) { ac[currentAnimationId].frames.RemoveAt(currentFrame); } } if (GUILayout.Button("+", GUILayout.Width(20f))) { SpriteFrame f = new SpriteFrame(); ac[currentAnimationId].frames.Add(f); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button(">", GUILayout.Width(20f))) { isPlaying = true; } if (GUILayout.Button("||", GUILayout.Width(20f))) { isPlaying = false; } if (GUILayout.Button("☐", GUILayout.Width(20f))) { isPlaying = false; t = 0f; } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(16f); pixelSize = EditorGUILayout.IntSlider("Pixel Scale", pixelSize, 1, 8, GUILayout.Width(256f)); var anim = ac[currentAnimationId]; if (anim != null) { if (c > currentFrame && currentFrame >= 0) { var f = anim.frames[currentFrame]; if (f.durationMultiplier <= 0f) { f.durationMultiplier = 1f; } f.durationMultiplier = Mathf.Max(.5f, EditorGUILayout.FloatField("Duration", f.durationMultiplier, GUILayout.Width(128f))); if (f != null) { switch (facing) { case BaseActor.Facing.Left: ac[currentAnimationId].frames[currentFrame].left.sprite = (Sprite)EditorGUILayout.ObjectField("Sprite", f.left.sprite, typeof(Sprite), false); break; case BaseActor.Facing.Right: ac[currentAnimationId].frames[currentFrame].right.sprite = (Sprite)EditorGUILayout.ObjectField("Sprite", f.right.sprite, typeof(Sprite), false); break; case BaseActor.Facing.Down: ac[currentAnimationId].frames[currentFrame].down.sprite = (Sprite)EditorGUILayout.ObjectField("Sprite", f.down.sprite, typeof(Sprite), false); break; case BaseActor.Facing.Up: ac[currentAnimationId].frames[currentFrame].up.sprite = (Sprite)EditorGUILayout.ObjectField("Sprite", f.up.sprite, typeof(Sprite), false); break; } Vector2 center = new Vector2(position.width / 2f, position.height / 2f); var e = Event.current; if (e.type == EventType.KeyDown) { switch (e.keyCode) { case KeyCode.LeftControl: controlKey = true; break; case KeyCode.A: allKey = true; break; case KeyCode.LeftArrow: currentFrame = (currentFrame - 1) % c; break; case KeyCode.RightArrow: currentFrame = (currentFrame + 1) % c; break; case KeyCode.DownArrow: currentAnimationId = (currentAnimationId - 1) % ac.Count; break; case KeyCode.UpArrow: currentAnimationId = (currentAnimationId + 1) % ac.Count; break; } } else if (e.type == EventType.KeyUp) { switch (e.keyCode) { case KeyCode.LeftControl: controlKey = false; break; case KeyCode.A: allKey = false; break; } } if (Event.current.commandName == "ObjectSelectorUpdated") { var pickedSprite = (Sprite)EditorGUIUtility.GetObjectPickerObject(); switch (facing) { case BaseActor.Facing.Left: f.left.sprite = pickedSprite; break; case BaseActor.Facing.Right: f.right.sprite = pickedSprite; break; case BaseActor.Facing.Down: f.down.sprite = pickedSprite; break; case BaseActor.Facing.Up: f.up.sprite = pickedSprite; break; } } if (f[facing] != null) { var spriteData = f[facing]; var sprite = spriteData.sprite; if (sprite != null) { float w = (float)sprite.texture.width; float h = (float)sprite.texture.height; float x = center.x - sprite.textureRect.width / 2f * (float)pixelSize; float y = center.y - sprite.textureRect.height / 2f * (float)pixelSize; float spriteW = sprite.textureRect.width * (float)pixelSize; float spriteH = sprite.textureRect.height * (float)pixelSize; var mirr = anim.mirroring[(int)facing]; Vector2 mirrV = new Vector2(mirr.X ? -1f : 1f, mirr.Y ? -1f : 1f); x += animationMotionPosition.x; y += animationMotionPosition.y; if (mirr.X) { x += spriteW; } if (mirr.Y) { y += spriteH; } Rect r = new Rect(x, y, spriteW * mirrV.x, spriteH * mirrV.y); GUI.DrawTextureWithTexCoords(r, sprite.texture, new Rect(sprite.textureRect.x / w, sprite.textureRect.y / h, sprite.textureRect.width / w, sprite.textureRect.height / h)); } else if (GUI.Button(new Rect(center.x - 128f, center.y - 128f, 256f, 256f), "Select a sprite")) { EditorGUIUtility.ShowObjectPicker <Sprite>(f[facing], false, string.Empty, 0); } } GUIStyle passiveStyle = new GUIStyle("Button"); GUIStyle activeStyle = new GUIStyle(passiveStyle); activeStyle.normal.textColor = activeStyle.hover.textColor = Color.green; activeStyle.fontStyle = FontStyle.Bold; Vector2 spriteAreaSize = new Vector2(256f + 0f, 256f + 0f); if (GUI.Button(new Rect(center.x - spriteAreaSize.x / 2f - 20f, center.y - spriteAreaSize.y / 2f, 20f, spriteAreaSize.y), "<-", facing == BaseActor.Facing.Left ? activeStyle : passiveStyle)) { if (controlKey) { if (allKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].left.sprite = ac[currentAnimationId].frames[i][facing].sprite; } Debug.Log("Copied active all animation frame sprites to target facing!"); } else { ac[currentAnimationId].frames[currentFrame].left.sprite = f[facing]; Debug.Log("Copied active Sprite to target facing!"); } } else { facing = BaseActor.Facing.Left; } } if (GUI.Button(new Rect(center.x + spriteAreaSize.x / 2f, center.y - spriteAreaSize.y / 2f, 20f, spriteAreaSize.y), "->", facing == BaseActor.Facing.Right ? activeStyle : passiveStyle)) { if (controlKey) { if (allKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].right.sprite = ac[currentAnimationId].frames[i][facing].sprite; } Debug.Log("Copied active all animation frame sprites to target facing!"); } else { ac[currentAnimationId].frames[currentFrame].right.sprite = f[facing]; Debug.Log("Copied active Sprite to target facing!"); } } facing = BaseActor.Facing.Right; } if (GUI.Button(new Rect(center.x - spriteAreaSize.x / 2f, center.y - spriteAreaSize.y / 2f - 20f, spriteAreaSize.x, 20f), "^", facing == BaseActor.Facing.Up ? activeStyle : passiveStyle)) { if (controlKey) { if (allKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].up.sprite = ac[currentAnimationId].frames[i][facing].sprite; } Debug.Log("Copied active all animation frame sprites to target facing!"); } else { ac[currentAnimationId].frames[currentFrame].up.sprite = f[facing]; Debug.Log("Copied active Sprite to target facing!"); } } facing = BaseActor.Facing.Up; } if (GUI.Button(new Rect(center.x - spriteAreaSize.x / 2f, center.y + spriteAreaSize.y / 2f, spriteAreaSize.x, 20f), "v", facing == BaseActor.Facing.Down ? activeStyle : passiveStyle)) { if (controlKey) { if (allKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].down.sprite = ac[currentAnimationId].frames[i][facing].sprite; } Debug.Log("Copied active all animation frame sprites to target facing!"); } else { ac[currentAnimationId].frames[currentFrame].down.sprite = f[facing]; Debug.Log("Copied active Sprite to target facing!"); } } facing = BaseActor.Facing.Down; } } else { Debug.LogError("NULL"); } EditorGUILayout.BeginVertical(GUILayout.Width(128f)); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Mirror (X/Y)", GUILayout.Width(64f)); bool mirrorX = EditorGUILayout.Toggle(anim.mirroring[(int)facing].X); bool mirrorY = EditorGUILayout.Toggle(anim.mirroring[(int)facing].Y); anim.mirroring[(int)facing] = new Mirroring(mirrorX, mirrorY); EditorGUILayout.EndHorizontal(); EditorGUI.BeginChangeCheck(); var duration = EditorGUILayout.FloatField("Duration", ac[currentAnimationId].frames[currentFrame].durationMultiplier); if (EditorGUI.EndChangeCheck()) { if (controlKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].durationMultiplier = duration; } } else { ac[currentAnimationId].frames[currentFrame].durationMultiplier = duration; } } EditorGUI.BeginChangeCheck(); var flags = (FrameFlags)EditorGUILayout.EnumFlagsField("Flags", ac[currentAnimationId].frames[currentFrame].flags); if (EditorGUI.EndChangeCheck()) { if (controlKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].flags = flags; } } else { ac[currentAnimationId].frames[currentFrame].flags = flags; } } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Motion", GUILayout.Width(48f)); showMotion = EditorGUILayout.Toggle("Show", showMotion); EditorGUILayout.EndHorizontal(); EditorGUI.BeginChangeCheck(); var motionX = EditorGUILayout.CurveField("Forward", ac[currentAnimationId].frames[currentFrame].motion.motionX); var motionY = EditorGUILayout.CurveField("Up", ac[currentAnimationId].frames[currentFrame].motion.motionY); if (EditorGUI.EndChangeCheck()) { if (controlKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].motion.motionX = motionX; } } else { ac[currentAnimationId].frames[currentFrame].motion.motionX = motionX; } if (controlKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].motion.motionY = motionY; } } else { ac[currentAnimationId].frames[currentFrame].motion.motionY = motionY; } } EditorGUI.BeginChangeCheck(); float motionMultiplier = EditorGUILayout.FloatField("Multiplier", ac[currentAnimationId].frames[currentFrame].motion.motionMultiplier); if (EditorGUI.EndChangeCheck()) { if (controlKey) { for (int i = 0; i < c; i++) { ac[currentAnimationId].frames[i].motion.motionMultiplier = motionMultiplier; } } else { ac[currentAnimationId].frames[currentFrame].motion.motionMultiplier = motionMultiplier; } } EditorGUILayout.EndVertical(); } else { Debug.LogError("NO FRAMES"); } } EditorGUIUtility.labelWidth = labelWidth; }
public virtual void OnFrameExit(SpriteAnimationBehaviour animated, SpriteFrame frame) { }
public virtual void OnDamageTarget(SpriteAnimationBehaviour animated, SpriteFrame frame, IDamagable damaged) { }
public void Advance(float seconds) { animationTime += seconds; if (currentAnimationId < 0 || currentAnimationFrameCount == 0) { return; } int frame = 0; var anim = currentCollection[currentAnimationId]; float d = anim.Duration; if (d <= 0f) { anim.CalculateDuration(); d = anim.Duration; } float t = animationTime % d; float frameDuration = 0f; while ((frameDuration = anim.frames[frame].durationMultiplier / anim.fps) < t) { t -= frameDuration; frame++; if (frame >= currentAnimationFrameCount) { frame %= currentAnimationFrameCount; } } if (anim.frames[frame] != currentFrame) { if (currentFrame != null) { if (currentFrame.frameEvent != null) { currentFrame.frameEvent.OnFrameExit(this, currentFrame); } } currentFrame = anim.frames[frame]; if (currentFrame.frameEvent != null) { currentFrame.frameEvent.OnFrameEnter(this, currentFrame); } } var s = currentFrame[actor.facing]; if (spriteRenderer.sprite != s) { spriteRenderer.sprite = s; var collider = spriteRenderer.GetComponent <PolygonCollider2D>(); if (collider != null) { Destroy(collider); spriteRenderer.gameObject.AddComponent <PolygonCollider2D>().isTrigger = true; } frameChanged = true; if (currentFrame.flags.HasFlag(FrameFlags.SWAP_FACING)) { actor.SetFacing(BaseActor.InvertFacing(actor.facing)); actor.Move(-actor.movementDirection); } } if (currentFrame.flags.HasFlag(FrameFlags.IGNORE_ACTOR_COLLISIONS)) { if (actor.gameObject.layer == Engine.actorLayer) { actor.gameObject.layer = Engine.airborneActorLayer; } } else if (actor.gameObject.layer == Engine.airborneActorLayer) { actor.gameObject.layer = Engine.actorLayer; } float frameTime = currentFrame.durationMultiplier / anim.fps; float prog = t / frameTime; currentAnimationFrameProgress = prog; }