Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <Siv3D.hpp> // OpenSiv3D v0.3.1
  2.  
  3.  
  4. class Fish
  5. {
  6. private:
  7. Vec2 m_start;
  8. Vec2 m_dest;
  9. double m_duration;
  10. Stopwatch m_sw;
  11.  
  12. public:
  13. Fish() : Fish(RandomVec2(Window::ClientRect())) {}
  14.  
  15. Fish(Vec2 pos)
  16. : m_start(pos)
  17. , m_dest(m_start)
  18. , m_duration(Random(0.0, 5.0))
  19. , m_sw(true) {}
  20.  
  21. Vec2 getPos() const
  22. {
  23. return m_start.lerp(m_dest, EaseOutQuad(Min(m_sw.sF(), 3.0) / 3.0));
  24. }
  25.  
  26. bool update()
  27. {
  28. if (m_sw.sF() > m_duration)
  29. {
  30. m_start = getPos();
  31. m_dest = RandomVec2(Window::ClientRect());
  32. m_duration = Random(1.0, 5.0);
  33. m_sw.restart();
  34. }
  35.  
  36. return RandomBool((1.0 - Min(m_sw.sF(), 3.0)) * 0.05);
  37. }
  38.  
  39. void draw() const
  40. {
  41. TextureAsset(U"fish").mirrored((m_dest - m_start).x > 0).scaled(0.5).drawAt(getPos());
  42. }
  43. };
  44.  
  45.  
  46. struct RippleEffect : IEffect
  47. {
  48. double m_size;
  49. double m_duration;
  50. Circle m_circle;
  51.  
  52. RippleEffect(Vec2 pos)
  53. {
  54. m_size = Random(80.0, 100.0);
  55. m_duration = 3.0;
  56. m_circle.setPos(pos + RandomVec2(Circle(0, 0, 10)));
  57. }
  58.  
  59. bool update(double timeSec)
  60. {
  61. double a = (1.0 - EaseOutSine(timeSec / m_duration)) * 0.4;
  62. double r = EaseOutSine(timeSec / m_duration) * m_size;
  63. m_circle.setR(r).drawFrame(2.0, ColorF(0.3, 0.4, 0.5, a));
  64. return timeSec < m_duration;
  65. }
  66. };
  67.  
  68.  
  69. class Tamo
  70. {
  71. private:
  72. const double m_size;
  73. Circle m_paper;
  74. RectF m_handle;
  75. RectF m_water;
  76. Transition m_transition;
  77. Stopwatch m_effect_sw;
  78.  
  79. public:
  80. Tamo(double size)
  81. : m_size(size)
  82. , m_paper(size)
  83. , m_handle(Arg::leftCenter = Vec2{ 0, 0 }, Vec2{ size * 1.6, size * 0.4 })
  84. , m_transition(0.6s, 0.2s)
  85. , m_effect_sw(true)
  86. {}
  87.  
  88. double getR() const { return m_size; }
  89.  
  90. bool update()
  91. {
  92. m_transition.update(MouseL.pressed());
  93.  
  94. m_water.set(Arg::bottomCenter = Vec2{ -m_size, m_size * 1.5 }, Vec2{ 2.5 * m_size, m_size * 1.1 * m_transition.easeInOut() });
  95.  
  96. if (MouseL.pressed() && m_effect_sw > 0.3s)
  97. {
  98. m_effect_sw.restart();
  99. return true;
  100. }
  101. return false;
  102. }
  103.  
  104. void draw()
  105. {
  106. const Transformer2D t1(Mat3x2::Translate(Cursor::PosF()));
  107. const Transformer2D t3(Mat3x2::Rotate(40_deg));
  108. const Transformer2D t2(Mat3x2::Scale(Vec2{ 1.0, 1.0 - 0.3 * m_transition.easeInOut() }));
  109. m_paper.draw(ColorF(0.8, 0.9, 1.0)).drawFrame(3.0, ColorF(0.3, 0.4, 0.5));
  110. const Transformer2D t4(Mat3x2::Translate(Vec2{ m_size , 0 }));
  111. m_handle.draw(ColorF(1.0, 0.5, 0.0));
  112. const Transformer2D t5(Mat3x2::Rotate(20_deg));
  113. m_water.draw(ColorF(0.6, 0.75, 1.0, 0.5));
  114. }
  115. };
  116.  
  117.  
  118. void Main()
  119. {
  120. Graphics::SetBackground(ColorF(0.6, 0.75, 1.0, 0.5));
  121.  
  122. Tamo tamo(50);
  123.  
  124. Effect effect;
  125.  
  126. int score = 0;
  127. Font font(30);
  128.  
  129. Array<Fish> fish(10, Arg::generator = []() { return Fish(); });
  130. TextureAsset::Register(U"fish", Emoji(U"\U0001F41F"));
  131. Stopwatch fish_arrival_sw(true);
  132.  
  133. Texture background(U"example/windmill.png");
  134.  
  135. const Audio se(U"example/shot.mp3");
  136. const Audio audio(U"example/test.mp3");
  137. audio.play();
  138.  
  139. while (System::Update())
  140. {
  141. if (fish_arrival_sw > 1.0s)
  142. {
  143. fish << Fish(RandomVec2(Window::Width() + Window::Height()));
  144. fish_arrival_sw.restart();
  145. }
  146. if (tamo.update()) effect.add<RippleEffect>(Cursor::PosF());
  147. for (auto it = fish.begin(); it != fish.end();)
  148. {
  149. if (it->update())
  150. effect.add<RippleEffect>(it->getPos());
  151. if (MouseL.up() && Circle(Cursor::PosF(), tamo.getR()).contains(it->getPos()))
  152. {
  153. score++;
  154. se.play();
  155. it = fish.erase(it);
  156. }
  157. else
  158. it++;
  159. }
  160.  
  161. Window::ClientRect()(background).draw(ColorF(1.0, 0.3));
  162.  
  163. effect.update(); // draw
  164. fish.each([](const Fish& f) { f.draw(); });
  165. tamo.draw();
  166.  
  167. const Transformer2D t1(Mat3x2::Translate(Vec2{ 10, 10 }));
  168. RoundRect(Vec2{ 0, 0 }, Vec2{ 150, 50 }, 10).draw(ColorF(1.0, 0.5));
  169. const Transformer2D t2(Mat3x2::Translate(Vec2{ 5, 5 }));
  170. TextureAsset(U"fish").resized(40, 40).draw(0, 0);
  171. font(U"x {}"_fmt(score)).draw(50, 0);
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement