-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhj_raf.js
121 lines (99 loc) · 1.95 KB
/
hj_raf.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
window.__redraw = window.__redraw || window.redraw;
window.redraw = function()
{
window.__redraw();
if (hj_draw)
{
try
{
hj_draw();
}
catch (e)
{
console.error(e);
}
}
};
function marker(g, x, y, col)
{
g.save();
g.translate(x, y);
g.strokeStyle = col || "red";
g.beginPath();
g.moveTo(-10, -10)
g.lineTo(10, 10);
g.moveTo(-10, 10);
g.lineTo(10, -10);
g.stroke();
g.restore();
}
function bigmarker(g, x, y, col)
{
g.save();
g.translate(x, y);
g.strokeStyle = col || "red";
g.beginPath();
g.arc(0,0,12, 0,Math.PI*2, false);
g.moveTo(-20,0)
g.lineTo(20,0);
g.moveTo(0,-20);
g.lineTo(0,20);
g.stroke();
g.restore();
}
function showObject(g, obj)
{
if (!obj)
{
return;
}
var x = obj.rx || obj.xx;
var y = obj.ry || obj.yy
marker(g, x, y);
g.fillText(""+obj.sz, x, y);
}
function showSnake(g, obj, col)
{
if (!obj || obj == snake)
{
return;
}
g.save();
g.translate(obj.xx, obj.yy);
var safe_radius = Math.max(100, Math.hypot(obj.xx-snake.xx, obj.yy-snake.yy) * obj.sp / (obj.sp + snake.sp));
g.strokeStyle = col || "red";
g.beginPath();
g.arc(0,0,safe_radius, 0,Math.PI*2, false);
g.moveTo(-10, -10)
g.lineTo(10, 10);
g.moveTo(-10, 10);
g.lineTo(10, -10);
g.stroke();
g.restore();
}
var hj_objects = [];
function hj_draw()
{
if (!snake)
{
return;
}
var c = window.mc;
var g = c.getContext("2d");
g.save();
g.translate(c.width/2, c.height/2);
g.scale(gsc, gsc);
g.translate(-view_xx, -view_yy);
marker(g, snake.xx, snake.yy);
bigmarker(g, snake.xx + xm, snake.yy + ym);
g.fillStyle = "lime";
g.font = "20px sans";
foods.forEach((food) => { showObject(g, food); });
preys.forEach((prey) => { showObject(g, prey); });
snakes.forEach((snake) => { if(snake) showSnake(g, snake); });
for (var i=0 ; i<hj_objects.length ; i++)
{
hj_objects[i].draw(g);
}
g.restore();
}