Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
DoNaTio s.r.o.
PiPo
Commits
4f7ac269
Commit
4f7ac269
authored
Dec 09, 2014
by
DoNaTio s.r.o.
Browse files
server interval
parent
0a73c89a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Ball.js
0 → 100644
View file @
4f7ac269
/**
* Ball
* @returns {{init: Function, getAxis: Function, getVectors: Function, setAxis: Function, setVectors: Function, calculateNewAxis: Function}}
*/
module
.
exports
=
function
()
{
var
attributes
=
{
x
:
100
,
y
:
100
,
z
:
0
,
// :)
vx
:
-
1.5
+
Math
.
random
()
*
3
,
vy
:
5
*
Math
.
random
()
*
1.5
,
speed
:
10
,
width
:
20
,
height
:
20
};
return
{
init
:
function
()
{
},
getAxis
:
function
(){
return
{
x
:
attributes
.
x
,
y
:
attributes
.
y
};
},
getVectors
:
function
(){
return
{
vx
:
attributes
.
vx
,
vy
:
attributes
.
vy
};
},
setAxis
:
function
(
x
,
y
)
{
attributes
.
x
=
x
;
attributes
.
y
=
y
;
},
setVectors
:
function
(
vx
,
vy
)
{
attributes
.
vx
=
vx
;
attributes
.
vy
=
vy
;
},
calculateNewAxis
:
function
(){
attributes
.
x
+=
attributes
.
vx
;
attributes
.
y
+=
attributes
.
vy
;
}
}
}
Collision.js
0 → 100644
View file @
4f7ac269
module
.
exports
=
function
(
Playground
,
Ball
)
{
var
attributes
=
{
'
ball
'
:
Ball
,
'
playground
'
:
Playground
};
return
{
reflect
:
function
(
v
,
n
)
{
var
d
=
this
.
dot
(
v
,
n
);
return
{
x
:
v
.
vx
-
2
*
d
*
n
.
vx
,
y
:
v
.
vy
-
2
*
d
*
n
.
vy
}
},
dot
:
function
(
v1
,
v2
)
{
return
(
v1
.
vx
*
v2
.
vx
)
+
(
v1
.
vy
*
v2
.
vy
);
},
normalize
:
function
(
v
)
{
var
len
=
this
.
getLength
(
v
);
v
.
vx
/=
len
;
v
.
vy
/=
len
;
return
v
;
},
getLength
:
function
(
v
)
{
return
Math
.
sqrt
(
v
.
vx
*
v
.
vx
+
v
.
vy
*
v
.
vy
);
},
collision
:
function
()
{
var
ballAxis
=
attributes
.
ball
.
getAxis
();
var
ballVectors
=
attributes
.
ball
.
getVectors
();
var
playgroundDimension
=
attributes
.
playground
;
if
(
ballAxis
.
y
>
playgroundDimension
.
h
)
{
ballVectors
.
vy
=
-
ballVectors
.
vy
;
ballAxis
.
y
=
playgroundDimension
.
h
;
}
else
if
(
ballAxis
.
y
<
0
)
{
ballVectors
.
vy
=
-
ballVectors
.
vy
;
ballAxis
.
y
=
0
;
}
// If ball strikes the vertical walls, invert the
// x-velocity vector of ball
if
(
ballAxis
.
x
>
playgroundDimension
.
w
)
{
ballVectors
.
vx
=
-
ballVectors
.
vx
;
ballAxis
.
x
=
playgroundDimension
.
w
;
}
else
if
(
ballAxis
.
x
<
0
)
{
ballVectors
.
vx
=
-
ballVectors
.
vx
;
ballAxis
.
x
=
0
;
}
//console.log(ballVectors.vx, ballVectors.vy);
attributes
.
ball
.
setAxis
(
ballAxis
.
x
,
ballAxis
.
y
);
attributes
.
ball
.
setVectors
(
ballVectors
.
vx
,
ballVectors
.
vy
);
return
false
;
}
}
}
public/ClientMain.js
View file @
4f7ac269
...
...
@@ -71,24 +71,18 @@ $(function () {
}
racket
.
setEl
(
$
(
'
div.racket
'
+
data
.
id
));
function
animloop
()
{
init
=
requestAnimFrame
(
animloop
);
if
(
user
.
getIsTurn
()){
ball
.
redraw
();
ball
.
calculateNewAxis
();
var
newAxis
=
ball
.
getAxis
();
socket
.
emit
(
'
ball position
'
,
newAxis
);
if
(
collision
.
collision
()){
socket
.
emit
(
'
how played
'
,
{
userId
:
data
.
id
});
}
}
}
animloop
();
//function animloop() {
// init = requestAnimFrame(animloop);
//
// if(user.getIsTurn()){
// if(collision.collision()){
// socket.emit('how played', {userId: data.id});
// }
// }
//
//}
//
//animloop();
});
...
...
server.js
View file @
4f7ac269
...
...
@@ -8,16 +8,28 @@ var port = process.env.PORT || 3000;
var
Player
=
require
(
'
./Player.js
'
);
var
PlayersCollection
=
require
(
'
./PlayersCollection.js
'
)();
var
racket
=
require
(
'
./Racket.js
'
);
var
ball
=
require
(
'
./Ball.js
'
)();
var
collision
=
require
(
'
./Collision.js
'
)({
h
:
776
,
w
:
968
},
ball
);
server
.
listen
(
port
,
function
()
{
console
.
log
(
'
Server listening at port %d
'
,
port
);
});
// Routing
app
.
use
(
express
.
static
(
__dirname
+
'
/public
'
));
io
.
on
(
'
connection
'
,
function
(
socket
)
{
setInterval
(
function
(){
collision
.
collision
();
ball
.
calculateNewAxis
();
var
newAxis
=
ball
.
getAxis
();
socket
.
emit
(
'
ball position
'
,
{
'
x
'
:
newAxis
.
x
,
'
y
'
:
newAxis
.
y
});
},
1
);
// when the client emits 'add user', this listens and executes
socket
.
on
(
'
add user
'
,
function
(
username
)
{
// we store the username in the socket session for this client
...
...
@@ -65,14 +77,14 @@ io.on('connection', function (socket) {
});
});
socket
.
on
(
'
ball position
'
,
function
(
data
)
{
//console.log(data);
socket
.
broadcast
.
emit
(
'
ball position
'
,
{
'
x
'
:
data
.
x
,
'
y
'
:
data
.
y
,
});
});
//
socket.on('ball position', function (data) {
//
//console.log(data);
//
//
socket.broadcast.emit('ball position', {
//
'x': data.x,
//
'y': data.y,
//
});
//
});
// when the user disconnects.. perform this
socket
.
on
(
'
disconnectUser
'
,
function
(
data
)
{
...
...
@@ -89,8 +101,4 @@ io.on('connection', function (socket) {
socket
.
disconnect
();
});
});
setInterval
(
function
(){},
100
);
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment