-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.hx
134 lines (119 loc) · 3.3 KB
/
Example.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import hxd.Key;
import h2d.Text;
import h2d.Graphics;
import harmonica.Harmonica;
class Example extends hxd.App {
var spring:Spring = null;
var circle:Graphics = null;
inline static final radius = 80;
inline static final multi = 1.0;
function drawCircle() {
circle.beginFill(0xFFFFFF);
circle.drawCircle(0,0,radius, 7);
circle.endFill();
}
override function init() {
circle = new Graphics(s2d);
onResize();
normalSpring();
}
var xVelocity = 0.0;
var yVelocity = 0.0;
var held = false;
var forcePull = false;
var flying = false;
var maxSpeed = 0.0;
function normalSpring() {
spring = Harmonica.newSpring(Harmonica.fPS(60), 6.0 * multi, 0.5 * multi);
}
override function update(dt:Float) {
var window = hxd.Window.getInstance();
if (!window.isFocused)
return;
final dis = hxd.Math.distance(Math.abs(circle.x - window.mouseX), Math.abs(circle.y - window.mouseY));
if (Math.abs(dis) < radius * 4) {
if (flying && Math.abs(xVelocity) < 600 * multi && Math.abs(yVelocity) < 600 * multi) {
normalSpring();
flying = false;
}
}
if (Key.isDown(Key.MOUSE_LEFT)) {
if (dis < 100) {
if (!held && !flying) {
// setup new sprint for being held
spring = Harmonica.newSpring(Harmonica.fPS(60), 20 * multi, 0.5 * multi);
forcePull = false;
held = true;
}
}else{
// force pull
if (!forcePull && !held) {
spring = Harmonica.newSpring(Harmonica.fPS(60), 10 * multi, 0.5 * multi);
forcePull = true;
held = false;
flying = false;
}
}
}else{
if (held && !forcePull) {
// throw!
spring = Harmonica.newSpring(Harmonica.fPS(60), 2.8 * multi, 0.2 * multi);
held = false;
flying = true;
xVelocity *= 2;
yVelocity *= 2;
}
if (forcePull) {
spring = Harmonica.newSpring(Harmonica.fPS(60), 2.8 * multi, 0.2 * multi);
}
forcePull = false;
}
if (maxSpeed < Math.abs(xVelocity))
maxSpeed = Math.abs(xVelocity);
if (maxSpeed < Math.abs(yVelocity))
maxSpeed = Math.abs(yVelocity);
final data = spring.update(circle.x, xVelocity, window.mouseX);
// tuple (x,velocityX)
circle.x = data.left;
xVelocity = data.right;
final data = spring.update(circle.y, yVelocity, window.mouseY);
// tuple (y,velocityY)
circle.y = data.left;
yVelocity = data.right;
// rotate when xVelocity changes
circle.rotation += Math.min(xVelocity, 2_000) / 1000 / 8;
// skewing
circle.scaleY = (1 - 0.5 * (Math.min(yVelocity, 2_000) / 4000)) * multi;
circle.scaleX = (1 + 0.25 * (Math.min(xVelocity, 2_000) / 4000)) * multi;
// make it so that the circle is always on screen
if (circle.x > window.width + radius * 2) {
circle.x = -radius * 2;
//xVelocity *= 1.2;
}
if (circle.x < -radius * 2) {
circle.x = window.width + radius * 2;
//xVelocity *= 1.2;
}
if (circle.y > window.height + radius * 2) {
circle.y = -radius * 2;
//yVelocity *= 1.2;
}
if (circle.y < -radius * 2) {
circle.y = window.height + radius * 2;
//yVelocity *= 1.2;
}
}
// if we the window has been resized
override function onResize() {
if(circle == null)
return;
// center our object
circle.x = Std.int(s2d.width / 2);
circle.y = Std.int(s2d.height / 2);
circle.clear();
drawCircle();
}
}
function main() {
new Example();
}