Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef XENOLITH_RENDERER_BASIC2D_XL2DACTIONACCELERATEDMOVE_H_
24 : #define XENOLITH_RENDERER_BASIC2D_XL2DACTIONACCELERATEDMOVE_H_
25 :
26 : #include "XLAction.h"
27 :
28 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d {
29 :
30 : class ActionAcceleratedMove : public ActionInterval {
31 : public:
32 : /** Движение от начальной точки до указанной, действие завершается по достижении конечной точки */
33 : static Rc<ActionInterval> createBounce(float acceleration, Vec2 from, Vec2 to, Vec2 velocity = Vec2::ZERO,
34 : float bounceAcceleration = 0, Function<void(Node *)> &&callback = nullptr);
35 :
36 : /** Движение от начальной точки до указанной, действие завершается по достижении конечной точки */
37 : static Rc<ActionInterval> createBounce(float acceleration, Vec2 from, Vec2 to, float velocity,
38 : float bounceAcceleration = 0, Function<void(Node *)> &&callback = nullptr);
39 :
40 : /** Движение от начальной точки до указанной, действие завершается при сбросе скорости до 0 */
41 : static Rc<ActionInterval> createFreeBounce(float acceleration, Vec2 from, Vec2 to, Vec2 velocity = Vec2::ZERO,
42 : float bounceAcceleration = 0, Function<void(Node *)> &&callback = nullptr);
43 :
44 : /** Движение от начальной точки в направлении вектора скорости, действие завершается при границ */
45 : static Rc<ActionInterval> createWithBounds(float acceleration, Vec2 from, Vec2 velocity,
46 : const Rect &bounds, Function<void(Node *)> &&callback = nullptr);
47 :
48 : /** отрезок пути до полной остановки (скорость и ускорение направлены в разные стороны) */
49 : static Rc<ActionAcceleratedMove> createDecceleration(Vec2 normal, Vec2 startPoint, float startVelocity,
50 : float acceleration, Function<void(Node *)> &&callback = nullptr);
51 :
52 : /** отрезок пути до полной остановки (скорость и ускорение направлены в разные стороны) */
53 : static Rc<ActionAcceleratedMove> createDecceleration(Vec2 startPoint, Vec2 endPoint,
54 : float acceleration, Function<void(Node *)> &&callback = nullptr);
55 :
56 : /** ускорение до достижения конечной скорости */
57 : static Rc<ActionAcceleratedMove> createAccelerationTo(Vec2 normal, Vec2 startPoint, float startVelocity, float endVelocity,
58 : float acceleration, Function<void(Node *)> &&callback = nullptr);
59 :
60 : /** ускорение до достижения конечной точки */
61 : static Rc<ActionAcceleratedMove> createAccelerationTo(Vec2 startPoint, Vec2 endPoint, float startVelocity,
62 : float acceleration, Function<void(Node *)> &&callback = nullptr);
63 :
64 : /** ускоренное движение по времени */
65 : static Rc<ActionAcceleratedMove> createWithDuration(float duration, Vec2 normal, Vec2 startPoint, float startVelocity,
66 : float acceleration, Function<void(Node *)> &&callback = nullptr);
67 :
68 28 : virtual ~ActionAcceleratedMove() { }
69 :
70 : virtual bool initDecceleration(Vec2 normal, Vec2 startPoint, float startVelocity,
71 : float acceleration, Function<void(Node *)> &&callback = nullptr);
72 : virtual bool initDecceleration(Vec2 startPoint, Vec2 endPoint,
73 : float acceleration, Function<void(Node *)> &&callback = nullptr);
74 : virtual bool initAccelerationTo(Vec2 normal, Vec2 startPoint, float startVelocity, float endVelocity,
75 : float acceleration, Function<void(Node *)> &&callback = nullptr);
76 : virtual bool initAccelerationTo(Vec2 startPoint, Vec2 endPoint, float startVelocity,
77 : float acceleration, Function<void(Node *)> &&callback = nullptr);
78 : virtual bool initWithDuration(float duration, Vec2 normal, Vec2 startPoint, float startVelocity,
79 : float acceleration, Function<void(Node *)> &&callback = nullptr);
80 :
81 : float getDuration() const;
82 :
83 : Vec2 getPosition(float timePercent) const;
84 :
85 : const Vec2 &getStartPosition() const { return _startPoint; }
86 0 : const Vec2 &getEndPosition() const { return _endPoint; }
87 0 : const Vec2 &getNormal() const { return _normalPoint; }
88 :
89 : float getStartVelocity() const { return _startVelocity; }
90 0 : float getEndVelocity() const { return _endVelocity; }
91 : float getCurrentVelocity() const;
92 :
93 : virtual void startWithTarget(Node *target) override;
94 : virtual void update(float time) override;
95 :
96 : virtual void setCallback(Function<void(Node *)> &&);
97 :
98 : protected:
99 : float _accDuration;
100 : float _acceleration;
101 :
102 : float _startVelocity;
103 : float _endVelocity;
104 :
105 : Vec2 _normalPoint;
106 : Vec2 _startPoint;
107 : Vec2 _endPoint;
108 :
109 : Vec2 computeEndPoint();
110 : Vec2 computeNormalPoint();
111 : float computeEndVelocity();
112 :
113 : Function<void(Node *)> _callback;
114 : };
115 :
116 : }
117 :
118 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2DACTIONACCELERATEDMOVE_H_ */
|