-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
400 lines (350 loc) · 17.7 KB
/
renderer.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
const electron = require('electron')
const { Point, Vector, Circle, Line, Ray, Segment, Arc, Box, Polygon, Matrix, PlanarSet, Multiline } = require('@flatten-js/core')
const BrowserWindow = electron.remote.BrowserWindow
const win = BrowserWindow.getAllWindows()[0]
const TransparencyMouseFix = require('electron-transparency-mouse-fix')
const fix = new TransparencyMouseFix({
log: true,
fixPointerEvents: 'auto'
})
var [width, height] = win.getSize()
width -= 10
height -= 20
let canvas = document.getElementById('canvas')
this.canvas.width = width
this.canvas.height = height
let ctx = canvas.getContext('2d')
topleft = new Point(0, 0)
topright = new Point(width, 0)
bottomleft = new Point(0, height)
bottomright = new Point(width, height)
wall = new Multiline([new Segment(topleft, topright), new Segment(topright, bottomright), new Segment(bottomright, bottomleft), new Segment(bottomleft, topleft)])
mouse = new Point(-100, -100)
electron.ipcRenderer.on('mouse', function (event, data) {
mouse.x = data.x
mouse.y = data.y
})
var robot = require("robotjs");
// // Speed up the mouse.
robot.setMouseDelay(1);
// var twoPI = Math.PI * 2.0;
// var screenSize = robot.getScreenSize();
// var height = (screenSize.height / 2) - 10;
// var width = screenSize.width;
// for (var x = 0; x < width; x++)
// {
// y = height * Math.sin((twoPI * x) / width) + height;
// robot.moveMouse(x, y);
// }
var objects = []
function update() {
delta = performance.now() - time
time += delta
ctx.clearRect(0, 0, width, height)
for (obj of objects)
obj.update(delta, ctx)
window.requestAnimationFrame(update)
}
time = performance.now()
window.requestAnimationFrame(update)
class Narwal {
constructor(x, y) {
this.pos = new Point(x, y)
this.dir = new Vector(1, 1).normalize()
this.tailMid = this.dir.multiply(-43)
this.tailEnd = this.dir.multiply(-50)
this.speed = 1
this.angleVel = 0
this.leftShift = 0
this.reentry = 0
this.animation = 0
this.anger = 0
this.gotMouse = false
this.hitbox = document.getElementById('hitbox')
this.hitbox.onclick = () => {
this.speed = 15
this.anger += 700
}
// this.hitbox.onmouseover = () => fishes[0].moving = false
// this.hitbox.onmouseleave = () => fishes[0].moving = true
}
update(delta) {
delta *= Math.max(1, Math.min(this.speed, 2))
this.anger = Math.min(2500, Math.max(this.anger - delta / 7, 0))
if (this.anger < 1000)
this.gotMouse = false
if (this.anger > 2400)
this.gotMouse = true
if (this.gotMouse)
robot.moveMouse(this.pos.translate(this.dir.multiply(35)).x + (process.platform == "darwin" ? 8 : 0), this.pos.translate(this.dir.multiply(35)).y + (process.platform == "darwin" ? 36 : 0));
if (!this.hunting)
this.speed -= Math.max(0, this.speed - .95) * delta / 1000
this.animation += delta
this.drawHorn(true)
this.drawHorn()
this.draw(true)
this.draw()
var angleChange = 0
if (this.reentry == 0) {
angleChange = this.angleVel
var wallIntersectPoint = wallRaycast(new Ray(this.pos, this.dir.rotate90CCW()))
if (wallIntersectPoint == undefined)
this.reentry = 1000
else if (this.hunting) {
if (!ball.stuck)
this.angleVel += angle(this.dir, new Vector(this.pos, ball.pos)) * delta / 450
} else {
if (!this.gotMouse) {
var mouseAngle = angle(this.dir, new Vector(this.pos, mouse))
var mouseWariness = Math.min(500, this.pos.distanceTo(mouse)[0]).map(0, 500, 10, 0) / Math.max(1, Math.abs(mouseAngle) * 4)
this.angleVel -= Math.sign(mouseAngle) * mouseWariness * mouseWariness / 1000
}
var wallDist = new Vector(this.pos, wallIntersectPoint).length
if (wallDist < 150) {
var leftRightCastPoint = new Point(this.pos.x * .1 + wallIntersectPoint.x * .9, this.pos.y * .1 + wallIntersectPoint.y * .9)
var wallDistLeft = wallRaycast(new Ray(leftRightCastPoint, this.dir.invert()))
var wallDistRight = wallRaycast(new Ray(leftRightCastPoint, this.dir))
wallDistLeft = new Vector(leftRightCastPoint, wallDistLeft).length + this.leftShift
wallDistRight = new Vector(leftRightCastPoint, wallDistRight).length
if (Math.abs(wallDistLeft - wallDistRight) < 10)
this.leftShift = Math.sign(Math.random() - .5) * 1000
this.angleVel += Math.sign(wallDistLeft - wallDistRight) * (150 - wallDist) / 1000
}
}
this.angleVel *= (1 - delta / 1000 * 1.5)
this.leftShift -= Math.sign(this.leftShift) * delta
if (ball.active) {
var ballAngle = Math.abs(angle(this.dir, new Vector(this.pos, ball.pos)))
if (ballAngle < .5) {
if (Math.random() * this.pos.distanceTo(ball.pos)[0] < delta + 20) {
this.hunting = true
this.speed = 1.5
}
}
}
} else {
if (this.reentry > 0) {
//still leaving
this.reentry -= delta
if (this.reentry <= 0) {
this.dir = new Vector(0, 1).rotate(Math.random() * 2 * Math.PI)
this.newSpawn = new Point(width / 2, height / 2).translate(this.dir.multiply(-Math.max(width, height) - 100))
this.pos = this.newSpawn
this.reentryDist = new Vector(this.newSpawn, wallRaycast(new Ray(this.pos, this.dir.rotate90CCW()))).length
if (ball.stuck)
ball.pos = new Point(1000000, 0);
ball.stuck = false
this.hunting = false
this.gotMouse = false
}
} else {
//re entering
if (this.reentryDist < new Vector(this.pos, this.newSpawn).length) {
this.reentry = 0
this.angleVel = 0
this.leftShift = 0
this.speed = 1
this.anger = 0
}
}
}
//movement and animation
this.pos = this.pos.translate(this.dir.multiply(delta / 10))
this.dir = this.dir.rotate(angleChange * delta / 1000)
var midDelta = angle(this.tailMid, this.dir.invert())
var endDelta = angle(this.tailEnd, this.dir.invert())
this.tailMid = this.tailMid.rotate(midDelta * delta / 125 * midDelta * Math.sign(midDelta))
this.tailEnd = this.tailEnd.rotate(endDelta * delta / 150 * endDelta * Math.sign(endDelta))
this.hitbox.style.top = (this.pos.y - 50) + "px"
this.hitbox.style.left = (this.pos.x - 50) + "px"
}
draw(outline = false) {
ctx.lineWidth = 4
ctx.strokeStyle = "#030303"
ctx.fillStyle = "#6FB2C3"
ctx.fillStyle = "#7EBAC9"
var finWiggle = Math.sin(this.animation / 800) / 6
var finWiggle2 = Math.sin(this.animation / 700 + 2) / 6
var finLeftBase = this.pos.translate(this.dir.multiply(26).rotate(-1.8))
var finRightBase = this.pos.translate(this.dir.multiply(26).rotate(1.8))
var finLeftOutCPoint = finLeftBase.translate(this.dir.multiply(12).rotate(-1.8 + finWiggle))
var finRightOutCPoint = finRightBase.translate(this.dir.multiply(12).rotate(1.8 + finWiggle2))
var finLeftPoint = finLeftBase.translate(this.dir.multiply(25).rotate(-2.8 + finWiggle))
var finRightPoint = finRightBase.translate(this.dir.multiply(25).rotate(2.8 + finWiggle2))
var finLeftInCPoint = finLeftBase.translate(this.dir.multiply(16).rotate(-3.8 + finWiggle))
var finRightInCPoint = finRightBase.translate(this.dir.multiply(16).rotate(3.8 + finWiggle2))
ctx.beginPath()
ctx.moveTo(finLeftBase.x, finLeftBase.y)
ctx.quadraticCurveTo(finLeftOutCPoint.x, finLeftOutCPoint.y, finLeftPoint.x, finLeftPoint.y)
ctx.quadraticCurveTo(finLeftInCPoint.x, finLeftInCPoint.y, finLeftBase.x, finLeftBase.y)
if (outline)
ctx.stroke()
else
ctx.fill()
ctx.beginPath()
ctx.moveTo(finRightBase.x, finRightBase.y)
ctx.quadraticCurveTo(finRightOutCPoint.x, finRightOutCPoint.y, finRightPoint.x, finRightPoint.y)
ctx.quadraticCurveTo(finRightInCPoint.x, finRightInCPoint.y, finRightBase.x, finRightBase.y)
if (outline)
ctx.stroke()
else
ctx.fill()
ctx.save()
ctx.fillStyle = "#9ecbd6"
ctx.beginPath()
var endAngle = angle(this.tailEnd, this.dir.invert())
var wiggle = Math.sin(this.animation / 300) / 6
wiggle /= Math.max(1, endAngle * 4)
var animatedMid = this.tailMid.rotate(wiggle / 2)
var animatedEnd = this.tailEnd.rotate(wiggle)
var leftTailBase = this.pos.translate(this.dir.multiply(-30).rotate(-.85 - Math.max(endAngle, 0)))
var leftTailBaseNormal = leftTailBase.translate(this.dir.multiply(15).rotate(-.85 - Math.max(endAngle, 0)).rotate90CW())
var leftTailEnd = this.pos.translate(animatedEnd.add(animatedEnd.normalize().rotate90CW().multiply(8)))
var leftTailMid = this.pos.translate(animatedMid.add(animatedMid.normalize().rotate90CW().multiply(4)))
var rightTailBase = this.pos.translate(this.dir.multiply(-30).rotate(.85 + Math.max(-endAngle, 0)))
var rightTailBaseNormal = rightTailBase.translate(this.dir.multiply(15).rotate(.85 + Math.max(-endAngle, 0)).rotate90CCW())
var rightTailEnd = this.pos.translate(animatedEnd.add(animatedEnd.normalize().rotate90CCW().multiply(8)))
var rightTailMid = this.pos.translate(animatedMid.add(animatedMid.normalize().rotate90CCW().multiply(4)))
var tailAnimatedLeft = animatedEnd.rotate90CW().normalize().rotate(wiggle / 2)
var tailAnimatedRight = tailAnimatedLeft.invert()
var tailCenter = this.pos.translate(animatedEnd.multiply(1.17))
var tailTipLeft = this.pos.translate(animatedEnd.multiply(1.27).add(tailAnimatedLeft.multiply(25)))
var tailTipRight = this.pos.translate(animatedEnd.multiply(1.27).add(tailAnimatedRight.multiply(25)))
var tailTipLeftLowerC = this.pos.translate(animatedEnd.multiply(1).add(tailAnimatedLeft.multiply(20)))
var tailTipLeftUpperC = this.pos.translate(animatedEnd.multiply(1.33).add(tailAnimatedLeft.multiply(15)))
var tailTipRightLowerC = this.pos.translate(animatedEnd.multiply(1).add(tailAnimatedRight.multiply(20)))
var tailTipRightUpperC = this.pos.translate(animatedEnd.multiply(1.33).add(tailAnimatedRight.multiply(15)))
ctx.moveTo(leftTailBase.x, leftTailBase.y)
ctx.bezierCurveTo(leftTailBaseNormal.x, leftTailBaseNormal.y, leftTailMid.x, leftTailMid.y, leftTailEnd.x, leftTailEnd.y)
ctx.quadraticCurveTo(tailTipLeftLowerC.x, tailTipLeftLowerC.y, tailTipLeft.x, tailTipLeft.y)
ctx.quadraticCurveTo(tailTipLeftUpperC.x, tailTipLeftUpperC.y, tailCenter.x, tailCenter.y)
ctx.quadraticCurveTo(tailTipRightUpperC.x, tailTipRightUpperC.y, tailTipRight.x, tailTipRight.y)
ctx.quadraticCurveTo(tailTipRightLowerC.x, tailTipRightLowerC.y, rightTailEnd.x, rightTailEnd.y)
ctx.bezierCurveTo(rightTailMid.x, rightTailMid.y, rightTailBaseNormal.x, rightTailBaseNormal.y, rightTailBase.x, rightTailBase.y)
var rightTailBaseNormalInv = rightTailBase.translate(this.dir.multiply(-30).rotate(.85 + Math.max(-endAngle, 0)).rotate90CCW())
var leftTailBaseNormalInv = leftTailBase.translate(this.dir.multiply(-30).rotate(-.85 - Math.max(endAngle, 0)).rotate90CW())
var nose = this.pos.translate(this.dir.multiply(30))
var noseRightNormal = this.pos.translate(this.dir.multiply(30).rotate(-.5))
var noseLeftNormal = this.pos.translate(this.dir.multiply(30).rotate(.5))
noseRightNormal = this.pos.translate(this.dir.multiply(30).add(this.dir.rotate90CW().multiply(20)))
noseLeftNormal = this.pos.translate(this.dir.multiply(30).add(this.dir.rotate90CCW().multiply(20)))
ctx.bezierCurveTo(rightTailBaseNormalInv.x, rightTailBaseNormalInv.y, noseRightNormal.x, noseRightNormal.y, nose.x, nose.y)
ctx.bezierCurveTo(noseLeftNormal.x, noseLeftNormal.y, leftTailBaseNormalInv.x, leftTailBaseNormalInv.y, leftTailBase.x, leftTailBase.y)
if (outline)
ctx.stroke()
else {
ctx.fill()
ctx.clip()
var eyeLeft = this.pos.translate(this.dir.rotate90CCW().multiply(20)).translate(this.dir.multiply(22))
var eyeRight = this.pos.translate(this.dir.rotate90CW().multiply(20)).translate(this.dir.multiply(22))
circle(eyeLeft.x, eyeLeft.y, 8, "black")
circle(eyeRight.x, eyeRight.y, 8, "black")
let eyecolors = ['0', '1', '2', '3', '4', '5', '6', '7', '9', 'b', 'c', 'd', 'd', 'd']
var eyeV = eyecolors[13 - Math.min(Math.floor(this.anger * 14 / 2500), 13)]
circle(eyeLeft.x, eyeLeft.y, 4, "#d" + eyeV + eyeV)
circle(eyeRight.x, eyeRight.y, 4, "#d" + eyeV + eyeV)
ctx.restore()
}
}
drawHorn(outline = false) {
ctx.lineWidth = 4
ctx.strokeStyle = "#030303"
var hornLeft = this.pos.translate(this.dir.rotate90CCW().multiply(12))
var hornPoint = this.pos.translate(this.dir.multiply(50))
var hornRight = this.pos.translate(this.dir.rotate90CW().multiply(12))
var hornLeftHalf = new Point((hornLeft.x + hornPoint.x * 4) / 5, (hornLeft.y + hornPoint.y * 4) / 5)
var hornRightHalf = new Point((hornRight.x + hornPoint.x * 4) / 5, (hornRight.y + hornPoint.y * 4) / 5)
ctx.fillStyle = "#dcba82"
ctx.beginPath()
ctx.moveTo(hornLeft.x, hornLeft.y)
ctx.lineTo(hornLeftHalf.x, hornLeftHalf.y)
ctx.quadraticCurveTo(hornPoint.x, hornPoint.y, hornRightHalf.x, hornRightHalf.y)
ctx.lineTo(hornRight.x, hornRight.y)
if (outline)
ctx.stroke()
else
ctx.fill()
}
}
class Ball {
constructor() {
this.xvel = Math.random() > .5 ? 1 : -1
this.yvel = 0
this.pos = new Point(width / 2 - width * this.xvel, Math.random() * 200)
this.radius = 30
this.active = true
this.stuck = false
this.rotation = 0
this.rotationSpeed = Math.random() * 8 - 4
}
update(delta) {
if (this.stuck) {
this.pos = narwhal.pos.translate(narwhal.dir.multiply(this.stuckDist).rotate(this.stuckAngle))
} else if (this.pos.distanceTo(narwhal.pos.translate(narwhal.dir.multiply(35)))[0] < this.radius) {
narwhal.hunting = true
narwhal.speed = 1.5
this.stuck = true
this.stuckDist = this.pos.distanceTo(narwhal.pos)[0]
this.stuckAngle = narwhal.dir.angleTo(new Vector(narwhal.pos, this.pos))
} else {
this.yvel += delta / 15
var ydelta = this.yvel * delta / 1000
if (this.pos.y + this.radius + ydelta > height) {
this.pos.y = height - this.radius
this.yvel *= -(Math.random() / 3 + .7)
}
else
this.pos = this.pos.translate(new Vector(this.xvel * delta / 15, ydelta))
this.active = true
if (this.xvel > 0 ? this.pos.x : width - this.pos.x > width + this.radius)
this.active = false
if (this.xvel > 0 ? this.pos.x : width - this.pos.x < this.radius)
this.active = false
}
circle(this.pos.x, this.pos.y, this.radius, "black")
var r = Math.PI / 3
if (!this.stuck)
this.rotation += this.rotationSpeed * delta / 1000
circle(this.pos.x, this.pos.y, this.radius - 2, "#fcfa4a", this.rotation + 0 * r - 1, this.rotation + r + 1)
circle(this.pos.x, this.pos.y, this.radius - 2, "#fdb44f", this.rotation + 1 * r, this.rotation + 2 * r + 1)
circle(this.pos.x, this.pos.y, this.radius - 2, "#fe79a9", this.rotation + 2 * r, this.rotation + 3 * r + 1)
circle(this.pos.x, this.pos.y, this.radius - 2, "#c788ff", this.rotation + 3 * r, this.rotation + 4 * r + 1)
circle(this.pos.x, this.pos.y, this.radius - 2, "#8ec9ff", this.rotation + 4 * r, this.rotation + 5 * r + 1)
circle(this.pos.x, this.pos.y, this.radius - 2, "#77ff86", this.rotation + 5 * r, this.rotation + 0)
circle(this.pos.x, this.pos.y, this.radius / 3.5, "white")
}
}
function wallRaycast(ray) {
var hit
for (var e of wall.edges) {
var intersections = ray.intersect(e.shape)
for (var h of intersections)
if (hit == undefined || (h.x - ray.pt.x) * (h.x - ray.pt.x) + (h.y - ray.pt.y) * (h.y - ray.pt.y) < (hit.x - ray.pt.x) * (hit.x - ray.pt.x) + (hit.y - ray.pt.y) * (hit.y - ray.pt.y))
hit = h
}
return hit
}
function angle(v1, v2) {
var raw = v1.angleTo(v2)
if (raw > Math.PI)
raw -= Math.PI * 2
return raw
}
Number.prototype.map = function (in_min, in_max, out_min, out_max) {
return (this - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
}
ball = new Ball()
narwhal = new Narwal(400, 200)
objects.push(narwhal)
objects.push(ball)
setInterval(() => {
ball = new Ball()
objects[1] = ball
}, 60000 + Math.random() * 20000)
function circle(x, y, radius, color, from = 0, to = Math.PI * 2) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.arc(x, y, radius, from, to, false)
ctx.fillStyle = color
ctx.fill()
}