forked from manny/Laser-Tank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
201 lines (181 loc) · 4.9 KB
/
Copy pathcontroller.js
File metadata and controls
201 lines (181 loc) · 4.9 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
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
var databaseUrl = "mydb";
var collections = ["debug_log"];
//var db = require("mongojs").connect(databaseUrl, collections);
var Canvas = require('./node-canvas/lib/canvas')
, canvas = new Canvas(2000, 1000)
, ctx = canvas.getContext('2d')
, http = require('http');
//db.debug_log.insert({command: "fireUp"});
console.log('Server running at localhost:3000');
draw();
var damage1 = 0;
var damage2 = 0;
//db.debug_log.find({}, console.log);
var five = require("./johnny-five"),
board, motor, led,laser, button, photoresistor;
board = new five.Board();
board.on("ready", function() {
//phptoresistor
photoresistor = new five.Sensor({
pin: "A2",
freq: 250
});
//led
fireLight = new five.Led(6);
hitLight = new five.Led(10);
laser = new five.Led(11);
//motors
motorL = new five.Motor(13);
motorR = new five.Motor(12);
//buttons
left = new five.Button(4);
forward = new five.Button(3);
right = new five.Button(2);
fire = new five.Button(5);
board.repl.inject({
pot: photoresistor,
laser: laser,
motorL: motorL,
motorR: motorR,
left: left,
forward: forward,
right: right,
fire: fire
});
//left controls
left.on("down", function(){
motorL.start();
console.log("downL");
// db.debug_log.insert({command: "downL"});
});
left.on("up", function(){
motorL.stop();
//console.log("upL");
//`db.debug_log.insert({command: "upL"});
});
//forward controls
forward.on("down", function(){
motorR.start();
motorL.start();
console.log("downF");
//db.debug_log.remove({});
// db.debug_log.insert({command: "downF"});
});
forward.on("up", function(){
motorR.stop();
motorL.stop();
//console.log("upF");
//db.debug_log.insert({command: "upF"});
});
right.on("down", function(){
motorR.start();
console.log("downR");
// db.debug_log.insert({command: "downR"});
});
right.on("up", function(){
motorR.stop();
//console.log("upR");
// db.debug_log.insert({command: "upR"});
});
fire.on("down", function(){
//console.log("fireDown");
fireLight.on();
laser.on();
// db.debug_log.insert({command: "fireDown"});
setTimeout(function(){
laser.off();
fireLight.off();
},300);
});
fire.on("up", function(){
//console.log("fireUp");
fireLight.off();
laser.off();
// db.debug_log.insert({command: "fireUp"});
//db.debug_log.find({}, console.log);
});
//motor functions
motorL.on("start", function( err, timestamp ) {
//console.log( "startL", timestamp );
});
motorL.on("stop", function( err, timestamp ) {
//console.log( "stopL", timestamp );
});
motorR.on("start", function( err, timestamp ) {
//console.log( "startR", timestamp );
});
motorR.on("stop", function( err, timestamp ) {
//console.log( "stopR", timestamp );
});
photoresistor.on("read", function( err, value ) {
//console.log( value, this.normalized );
//lower the value if room is too bright
if(value <45){
ctx.font = "bold 18pt Verdian";
ctx.fillStyle = "red";
ctx.fillText("HIT -100", 950, 90);
console.log("");
console.log("**** **** ********* ******** **");
console.log("**** **** ** ** **");
console.log("************* ** ** **");
console.log("************* ** ** **");
console.log("************* ** ** **");
console.log("**** **** ** ** ");
console.log("**** **** ** ** **");
console.log("**** **** ******** ** **");
console.log("");
if(damage2 + 110 >= 550){
damage2 = 550;
}else{
damage2 = damage2 + 110;
}
hitLight.on();
//hitLight.strobe(100);
setTimeout(function(){
hitLight.off();
}, 3000);
}
});
});
function draw(){
ctx.clearRect(0, 0, 2000, 1000);
ctx.fillStyle = "rgb(1, 1, 1)";
ctx.fillRect(0,0, 2000 , 1000);
//green health
ctx.fillStyle = "rgb(2, 200, 2)";
ctx.fillRect(60, 150, 550 , 35);
ctx.fillStyle = "rgb(2, 200, 2)";
ctx.fillRect(740, 150, 550 , 35);
//red damage
ctx.fillStyle = "rgb(200, 2, 2)";
ctx.fillRect(60, 150, damage1, 35);
ctx.fillStyle = "rgb(200, 2, 2)";
ctx.fillRect(740+550 -damage2, 150, damage2 , 35);
//title + player names
ctx.font = "bold 24pt Verdian";
ctx.fillStyle = "green";
ctx.fillText("Laser Tanks", 550, 40);
ctx.fillText("New York Tech Meetup", 450, 90);
ctx.font = "bold 22pt Verdian";
ctx.fillStyle = "red";
ctx.fillText("Manny", 100, 130);
ctx.font = "bold 22pt Verdian";
ctx.fillStyle = "red";
ctx.fillText("Player 2", 1100,130);
if(damage2 == 550){
//console.log("player one wins");
ctx.font = "bold 30pt Verdian";
ctx.fillStyle = "red";
ctx.fillText("Manny Wins!!!", 540, 400);
}
}
setInterval(function(){
draw();
}, 1000);
http.createServer(function (req, res) {
// clock(ctx);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(''
+ '<meta http-equiv="refresh" content="1;" />'
+ '<img src="' + canvas.toDataURL() + '" />');
}).listen(3000);