-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.js
86 lines (73 loc) · 2.49 KB
/
layer.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
//********************************************************************
//********Layer.js
//********Contains all the function related to DOM(i.e. canvas)
//********ATTENTION: Premise - contains jQuery and kernel.js above
//********Programmer: Zhao Leiyu, Wang Anqi
//********Date: 2014-9-26
//********************************************************************
$(function() //the funciton being called after finishing initilizing the DOM
{
$("#CellBase").attr("width",GLOBAL_CONST.CANVAS_SIZE)
.attr("height",GLOBAL_CONST.CANVAS_SIZE);
/*Begin: delete this to use per-pixel drawing*/
$("#CellBase").attr("width",GLOBAL_CONST.WIDTH)
.attr("height",GLOBAL_CONST.HEIGHT);
/*End*/
//get the dc of canvas that will be passed to the draw function later:
GLOBAL_CONST.CANVAS=$("#CellBase")[0].getContext("2d");
//generate the global arena and initialize it
GLOBAL_CONST.ARENA=new Arena();
RefreshOnce();
//Bind the event of two buttons
$("#RefreshButton").click(RefreshOnce);
$("#PlayButton").click(function()
{
if (GLOBAL_CONST.TIMER==-1)
StartPlay();
else
PausePlay();
});
});
function RefreshCanvas(canvas) //called periodicly to lay the arena to the canvas as well as to calculate the next frame
{
var perWidth=GLOBAL_CONST.CANVAS_SIZE*1.0/GLOBAL_CONST.WIDTH;
var perHeight=GLOBAL_CONST.CANVAS_SIZE*1.0/GLOBAL_CONST.HEIGHT;
/*Begin: delete this to use per-pixel drawing*/
perWidth=perHeight=1;
/*End*/
for (var i=0;i<GLOBAL_CONST.HEIGHT;i++)
for (var j=0;j<GLOBAL_CONST.WIDTH;j++)
{
if (!GLOBAL_CONST.ARENA.changed[i][j]) continue;
if (GLOBAL_CONST.ARENA.terrain[i][j])
canvas.fillStyle="#000000";
else
canvas.fillStyle="#FFFFFF";
canvas.fillRect(j*perWidth,i*perHeight,perWidth,perHeight);
}
GLOBAL_CONST.ARENA=RefreshArena(GLOBAL_CONST.ARENA);
}
//Followings are functions binded to click-event of the buttons:
function PausePlay() //Pause the refreshing and change the image of button
{
if (GLOBAL_CONST.TIMER!=-1)
{
$("#PlayButton").attr("src","img/play.png");
clearInterval(GLOBAL_CONST.TIMER);
}
GLOBAL_CONST.TIMER=-1;
}
function StartPlay() //Continue the refreshing and change the image of button
{
if (GLOBAL_CONST.TIMER==-1)
{
$("#PlayButton").attr("src","img/play-r.png");
GLOBAL_CONST.TIMER=setInterval(function(){RefreshCanvas(GLOBAL_CONST.CANVAS)},1000/GLOBAL_CONST.FPS);
}
}
function RefreshOnce() //Reshuffle the whole arena but pause refreshing
{
PausePlay();
RandomShuffle(GLOBAL_CONST.ARENA);
RefreshCanvas(GLOBAL_CONST.CANVAS);
}