-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (119 loc) · 3.01 KB
/
Copy pathscript.js
File metadata and controls
136 lines (119 loc) · 3.01 KB
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
var canvas = document.getElementById("canvas");
// 描画域をリサイズ
function canvas_resize(){
var windowInnerWidth = window.innerWidth;
var windowInnerHeight = window.innerHeight;
canvas.setAttribute('width',windowInnerWidth);
canvas.setAttribute('height',windowInnerHeight/2);
}
window.addEventListener('resize', canvas_resize, false);
canvas_resize();
// グローバル変数
var ctx = canvas.getContext("2d");
var ball_r = Math.min(canvas.width/40, canvas.height/40);
// クラス定義
// Pos: 座標
class Pos {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
// Person: 人の描画や座標保持など
class Person {
constructor() {
this.pos = new Pos(0, canvas.height/2);
this.ball_r = Math.min(canvas.width/40, canvas.height/40);
}
get position() {
return this.pos;
}
walk() {
this.pos.x += 1;
}
draw() {
// 問題点: x軸方向にウィンドウサイズが変更されると人のx座標がずれる
this.pos.y = canvas.height / 2;
ctx.beginPath();
ctx.arc(this.pos.x, this.pos.y, this.ball_r, 0, Math.PI*2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
this.walk();
}
}
// People: Personのリスト 全体の人数管理など
class People {
constructor() {
this.people = [];
}
get length() {
return this.people.length;
}
enter() {
this.people.push(new Person());
}
exit() {
this.people.shift();
}
draw() {
for (var person of this.people) {
if (person.position.x >= Cashier.x + ball_r)
this.exit();
person.draw();
}
}
}
// Cashier: レジ
class Cashier {
constructor(x,y) {
this.height = ball_r * 8;
this.width = ball_r * 2;
this.x = x;
this.y = y;
this.pos = new Pos(this.x, this.y - this.height/2);
}
static get position() {
return canvas.width * 7/8;
}
draw() {
this.pos.x = this.x();
this.pos.y = this.y() - this.height/2;
ctx.beginPath();
ctx.fillStyle = "#0095DD";
ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height);
ctx.closePath();
}
}
// Line: 客の並ぶ列
class Line {
constructor() {
this.height = ball_r * 3;
}
draw() {
ctx.beginPath();
ctx.fillStyle = "#CCC";
ctx.fillRect(0, canvas.height/2 - this.height/2, canvas.width, this.height);
ctx.closePath();
}
}
// 初期化
function init() {
people = new People();
cashier = new Cashier(() => canvas.width * 7/8, () => canvas.height/2);
line = new Line();
}
// 描画関数
function draw() {
// 画面クリア
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 乱数で入店
var random = Math.floor( Math.random() * 100 );
if (random == 50)
people.enter();
line.draw();
cashier.draw();
people.draw();
}
init();
setInterval(draw, 1);