-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlockvBlock.js
More file actions
50 lines (46 loc) · 1.1 KB
/
BlockvBlock.js
File metadata and controls
50 lines (46 loc) · 1.1 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
// Initiate the objects
var Shapes = new Meteor.Collection('shapes')
var Players = new Meteor.Collection('players')
function Game ()
{
// Set up canvas
this.setup = function()
{
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
}
// Set up click events
this.setupEvents = function()
{
canvas.addEventListener('click', function(e)
{
Shapes.insert(
{
positionx: e.pageX,
positiony: e.pageY
});
var me = Players.findOne(Session.get('player_id'));
newscore = me.score + 1;
Players.update(Session.get('player_id'), {$set: {score: newscore }});
});
}
// Set up listeners for the draw method
this.startUpdateListener = function()
{
var redrawCanvas = function()
{
var context = new Meteor.deps.Context()
context.on_invalidate(redrawCanvas)
context.run(function()
{
var shapes = Shapes.find({})
shapes.forEach(function(shape)
{
ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
ctx.fillRect (shape.positionx, shape.positiony, 20, 20);
})
})
}
redrawCanvas()
}
}