1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 17:02:55 +08:00
osu-lazer/osu.Game.Rulesets.Catch/UI/CatcherArea.cs

179 lines
5.8 KiB
C#
Raw Normal View History

2017-08-08 06:32:23 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2017-08-08 11:46:37 +08:00
using System.Linq;
2017-08-07 14:09:31 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
2017-08-11 15:11:46 +08:00
using osu.Framework.Input.Bindings;
2017-08-08 11:46:37 +08:00
using osu.Framework.MathUtils;
using osu.Game.Rulesets.Catch.Judgements;
using osu.Game.Rulesets.Catch.Objects;
using osu.Game.Rulesets.Catch.Objects.Drawable;
using osu.Game.Rulesets.Objects.Drawables;
2017-08-07 14:09:31 +08:00
using OpenTK;
namespace osu.Game.Rulesets.Catch.UI
{
public class CatcherArea : Container
{
2017-08-08 11:46:37 +08:00
private Catcher catcher;
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
public void Add(DrawableHitObject<CatchBaseHit, CatchJudgement> fruit, Vector2 screenPosition) => catcher.AddToStack(fruit, screenPosition);
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
public bool CheckIfWeCanCatch(CatchBaseHit obj) => Math.Abs(catcher.Position.X - obj.Position) < catcher.DrawSize.X / DrawSize.X / 2;
2017-08-07 14:09:31 +08:00
[BackgroundDependencyLoader]
2017-08-08 11:46:37 +08:00
private void load()
2017-08-07 14:09:31 +08:00
{
Children = new Drawable[]
{
2017-08-08 11:46:37 +08:00
catcher = new Catcher
2017-08-07 14:09:31 +08:00
{
RelativePositionAxes = Axes.Both,
Anchor = Anchor.TopLeft,
Origin = Anchor.TopCentre,
X = 0.5f,
2017-08-08 11:46:37 +08:00
}
2017-08-07 14:09:31 +08:00
};
}
2017-08-08 11:46:37 +08:00
protected override void Update()
{
base.Update();
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
catcher.Size = new Vector2(DrawSize.Y);
}
2017-08-07 14:09:31 +08:00
2017-08-12 18:54:07 +08:00
private class Catcher : Container, IKeyBindingHandler<CatchAction>
2017-08-07 14:09:31 +08:00
{
2017-08-08 11:46:37 +08:00
private Texture texture;
[BackgroundDependencyLoader]
private void load(TextureStore textures)
2017-08-07 14:09:31 +08:00
{
2017-08-08 11:46:37 +08:00
texture = textures.Get(@"Play/Catch/fruit-catcher-idle");
Child = createCatcherSprite();
2017-08-07 14:09:31 +08:00
}
2017-08-08 11:46:37 +08:00
private int currentDirection;
private bool dashing;
protected bool Dashing
2017-08-07 14:09:31 +08:00
{
2017-08-08 11:46:37 +08:00
get { return dashing; }
set
{
if (value == dashing) return;
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
dashing = value;
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
if (dashing)
Schedule(addAdditiveSprite);
}
2017-08-07 14:09:31 +08:00
}
2017-08-08 11:46:37 +08:00
private void addAdditiveSprite()
{
if (!dashing) return;
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
var additive = createCatcherSprite();
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
additive.RelativePositionAxes = Axes.Both;
additive.BlendingMode = BlendingMode.Additive;
additive.Position = Position;
additive.Scale = Scale;
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
((CatcherArea)Parent).Add(additive);
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
additive.FadeTo(0.4f).FadeOut(800, Easing.OutQuint).Expire();
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
Scheduler.AddDelayed(addAdditiveSprite, 50);
}
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
private Sprite createCatcherSprite() => new Sprite
2017-08-07 14:09:31 +08:00
{
2017-08-08 11:46:37 +08:00
RelativeSizeAxes = Axes.Both,
FillMode = FillMode.Fit,
Texture = texture,
OriginPosition = new Vector2(DrawWidth / 2, 10) //temporary until the sprite is aligned correctly.
};
public bool OnPressed(CatchAction action)
2017-08-08 11:46:37 +08:00
{
switch (action)
2017-08-08 11:46:37 +08:00
{
case CatchAction.MoveLeft:
currentDirection--;
return true;
case CatchAction.MoveRight:
currentDirection++;
return true;
case CatchAction.Dash:
Dashing = true;
return true;
2017-08-08 11:46:37 +08:00
}
return false;
2017-08-07 14:09:31 +08:00
}
public bool OnReleased(CatchAction action)
2017-08-08 11:46:37 +08:00
{
switch (action)
2017-08-08 11:46:37 +08:00
{
case CatchAction.MoveLeft:
currentDirection++;
return true;
case CatchAction.MoveRight:
currentDirection--;
return true;
case CatchAction.Dash:
Dashing = false;
return true;
2017-08-08 11:46:37 +08:00
}
return false;
2017-08-08 11:46:37 +08:00
}
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
protected override void Update()
2017-08-07 14:09:31 +08:00
{
2017-08-08 11:46:37 +08:00
base.Update();
if (currentDirection == 0) return;
float speed = Dashing ? 1.5f : 1;
Scale = new Vector2(Math.Sign(currentDirection), 1);
X = (float)MathHelper.Clamp(X + Math.Sign(currentDirection) * Clock.ElapsedFrameTime / 1800 * speed, 0, 1);
2017-08-07 14:09:31 +08:00
}
2017-08-08 11:46:37 +08:00
public void AddToStack(DrawableHitObject<CatchBaseHit, CatchJudgement> fruit, Vector2 absolutePosition)
{
fruit.RelativePositionAxes = Axes.None;
fruit.Position = new Vector2(ToLocalSpace(absolutePosition).X - DrawSize.X / 2, 0);
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
fruit.Anchor = Anchor.TopCentre;
fruit.Origin = Anchor.BottomCentre;
fruit.Scale *= 0.7f;
fruit.LifetimeEnd = double.MaxValue;
fruit.Depth = (float)Time.Current;
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
float distance = fruit.DrawSize.X / 2 * fruit.Scale.X;
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
while (Children.OfType<DrawableFruit>().Any(f => Vector2.DistanceSquared(f.Position, fruit.Position) < distance * distance))
{
fruit.X += RNG.Next(-5, 5);
fruit.Y -= RNG.Next(0, 5);
}
2017-08-07 14:09:31 +08:00
2017-08-08 11:46:37 +08:00
Add(fruit);
}
2017-08-07 14:09:31 +08:00
}
}
}