-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtest.js
116 lines (99 loc) · 2.91 KB
/
test.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
var sdl = require("./build/Release/node_sdl.node");
var eng = require("./engine.js");
var width = 640;
var height = 480;
var title = "Pong";
var fps = 60;
var paddleSize = {x: width / 10, y: width / 20};
var paddleSpeed = 300;
var paddleInput = function(event, world) {
if(event.repeat !== undefined && event.repeat) {
return;
}
if(event.type === "KEYDOWN") {
if(event.scancode === sdl.SCANCODE.LEFT) {
this.vel.x += -paddleSpeed;
}
else if(event.scancode === sdl.SCANCODE.RIGHT) {
this.vel.x += paddleSpeed;
}
}
else if(event.type === "KEYUP") {
if(event.scancode === sdl.SCANCODE.LEFT) {
this.vel.x += paddleSpeed;
}
else if(event.scancode === sdl.SCANCODE.RIGHT) {
this.vel.x += -paddleSpeed;
}
}
}
var paddleTick = function(dt, world) {
eng.Entity.prototype.tick.apply(this, [dt, world]);
if(this.x < world.leftWall.x + world.leftWall.w) {
this.x = world.leftWall.x + world.leftWall.w - 1;
}
else if(this.x + this.w > world.rightWall.x) {
this.x = world.rightWall.x - this.w;
}
}
var ballTick = function(dt, world) {
eng.Entity.prototype.tick.apply(this, [dt, world]);
if(this.x < world.leftWall.x + world.leftWall.w) {
this.vel.x = -this.vel.x;
}
else if(this.x + this.w > world.rightWall.x) {
this.vel.x = -this.vel.x;
}
else if(this.y < 0) {
this.vel.y = -this.vel.y;
}
else if(this.y + this.h > height) {
this.vel.y = -this.vel.y;
}
}
var shouldQuit = function(event, world) {
if(event.type === "KEYDOWN") {
if(event.scancode === sdl.SCANCODE.Q) {
return true;
}
}
return false;
}
var setup = function(window, renderer) {
var world = {};
var surface = new sdl.Surface(paddleSize.x, paddleSize.y);
surface.fillRect(new sdl.Color(255, 255, 255).getColor(surface.getPixelFormat()));
world.paddleTex = new sdl.Texture(renderer, surface);
surface = new sdl.Surface(width / 30, width / 30);
surface.fillRect(new sdl.Color(255, 255, 255).getColor(surface.getPixelFormat()));
world.ballTex = new sdl.Texture(renderer, surface);
surface = new sdl.Surface(width / 100, height);
surface.fillRect(new sdl.Color(255, 255, 255).getColor(surface.getPixelFormat()));
world.wallTex = new sdl.Texture(renderer, surface);
world.paddle = new eng.Entity({
texture: world.paddleTex,
processInput: paddleInput,
tick: paddleTick,
pos: {y: height - paddleSize.y * 2, x: width / 2 - paddleSize.x},
size: paddleSize
});
world.leftWall = new eng.Entity({
texture: world.wallTex,
pos: {y: 0, x: (width / 100) * 2},
size: {x: width / 100, y: height}
});
world.rightWall = new eng.Entity({
texture: world.wallTex,
pos: {y: 0, x: width - (width / 100) * 2},
size: {x: width / 100, y: height}
});
world.ball = new eng.Entity({
texture: world.ballTex,
tick: ballTick,
pos: {y: height / 2, x: width / 2},
size: {x: width / 30, y: width / 30},
vel: {x: 100, y: 100}
});
return world;
}
eng.gameLoop(fps, setup, shouldQuit, {width: width, height: height, title: title});