├── images ├── player.png ├── player │ ├── black.png │ ├── blue.png │ ├── green.png │ ├── pink.png │ ├── red.png │ ├── white.png │ ├── orange.png │ └── yellow.png └── player-2015-02-26.zip ├── README.md ├── package.json ├── style └── jogo.css ├── main.js ├── js ├── jogo.js └── player.js └── index.html /images/player.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hue_game 2 | A Simple capture the flag style game using crafty and socket.io 3 | -------------------------------------------------------------------------------- /images/player/black.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/black.png -------------------------------------------------------------------------------- /images/player/blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/blue.png -------------------------------------------------------------------------------- /images/player/green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/green.png -------------------------------------------------------------------------------- /images/player/pink.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/pink.png -------------------------------------------------------------------------------- /images/player/red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/red.png -------------------------------------------------------------------------------- /images/player/white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/white.png -------------------------------------------------------------------------------- /images/player/orange.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/orange.png -------------------------------------------------------------------------------- /images/player/yellow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player/yellow.png -------------------------------------------------------------------------------- /images/player-2015-02-26.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt-br/hue_game/master/images/player-2015-02-26.zip -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hue-game", 3 | "version": "0.0.1", 4 | "description": "my first socket.io game", 5 | "dependencies": { 6 | "express": "^4.10.2", 7 | "socket.io": "^1.3.4" 8 | }, 9 | "scripts": { 10 | "start": "node main.js" 11 | } 12 | } -------------------------------------------------------------------------------- /style/jogo.css: -------------------------------------------------------------------------------- 1 | 2 | body, html { 3 | overflow: hidden; 4 | } 5 | 6 | form#login { 7 | position: fixed; 8 | background-color: white; 9 | z-index: 2; 10 | width: 100%; 11 | height: 100%; 12 | } 13 | 14 | div#field { 15 | position: relative; 16 | background-color: brown; 17 | height: 1920px; 18 | width: 1080px; 19 | } 20 | 21 | div.player { 22 | position: absolute; 23 | } -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var app = express(); 3 | var http = require('http').Server(app); 4 | var io = require('socket.io')(http); 5 | 6 | app.use("/style", express.static(__dirname + '/style')); 7 | app.use("/js", express.static(__dirname + '/js')); 8 | app.use("/images", express.static(__dirname + '/images')); 9 | 10 | app.get('/', function(req, res){ 11 | res.sendFile(__dirname + '/index.html'); 12 | }); 13 | 14 | var players = {}; 15 | 16 | io.on('connection', function(socket){ 17 | players[socket.id] = {}; 18 | 19 | socket.on('add player', function(player) { 20 | io.to(socket.id).emit("populate", players); 21 | players[socket.id] = player; 22 | console.log(JSON.stringify(players)); 23 | io.emit('new player', player); 24 | console.log(player.name+" joined the room"); 25 | }) 26 | 27 | socket.on('disconnect', function(){ 28 | console.log(players[socket.id].name+" left the room"); 29 | io.emmit("remove user", socket.id); 30 | delete players[socket.id] 31 | }); 32 | 33 | socket.on("move", function(user) { 34 | players[user.id].position = user.position; 35 | io.emit("move", user); 36 | }) 37 | }); 38 | 39 | var port = process.env.PORT || 3000 40 | 41 | http.listen(port, function(){ 42 | console.log('listening on *:'+port); 43 | }); -------------------------------------------------------------------------------- /js/jogo.js: -------------------------------------------------------------------------------- 1 | Crafty.init(1920,1080, document.getElementById("field")); 2 | 3 | Crafty.sprite(64, "images/player.png", { 4 | player_black: [0,0], 5 | player_blue: [0,1], 6 | player_green: [0,2], 7 | player_orange: [0,3], 8 | player_pink: [0,4], 9 | player_yellow: [0,5], 10 | player_red: [0,6], 11 | player_white: [0,7] 12 | }); 13 | 14 | var players = []; 15 | var me, socket, name, color; 16 | 17 | socket = io(); 18 | 19 | $("#start").click(function (e) { 20 | e.preventDefault(); 21 | name = $("#nickname").val(); 22 | color = $('input[name=color]:checked', '#login').val(); 23 | if(nickname != "" && color != null && socket.connected) { 24 | connect(); 25 | $("#login").remove(); 26 | } 27 | }); 28 | 29 | function connect() { 30 | me = {"id": socket.id, "name": name, "color": color}; 31 | addPlayer(me); 32 | socket.emit("add player", me); 33 | players[me.id].crafty.fourway(4) 34 | players[me.id].crafty.bind("Move", function(pos) { 35 | me.position = {x: pos._x, y: pos._y, h: pos._h, w: pos._w}; 36 | socket.emit("move", {"id": me.id, "position": me.position}); 37 | }); 38 | } 39 | 40 | function addPlayer(player) { 41 | players[player.id] = new Player(player); 42 | players[player.id].draw(); 43 | } 44 | 45 | socket.on("new player", function(user) { 46 | if(user.id !== me.id) 47 | addPlayer(user); 48 | }); 49 | 50 | socket.on("populate", function (users) { 51 | for(var id in users) { 52 | addPlayer(users[id]); 53 | } 54 | }) 55 | 56 | socket.on("move", function (user) { 57 | if(user.id !== me.id) { 58 | players[user.id].crafty.attr(user.position); 59 | } 60 | }) -------------------------------------------------------------------------------- /js/player.js: -------------------------------------------------------------------------------- 1 | window.Player = function(playerObject) { 2 | 3 | this.config = playerObject; 4 | 5 | this.draw = function() { 6 | var field = $("#field") 7 | // field.append(""); 8 | 9 | // var canvas = document.getElementById(player.id); 10 | // var context = canvas.getContext('2d'); 11 | // var centerX = canvas.width / 2; 12 | // var centerY = canvas.height / 2; 13 | // var radius = 30; 14 | // var eyeRadius = 5; 15 | // var eyeXOffset = 12; 16 | // var eyeYOffset = 25; 17 | 18 | // // draw the yellow circle 19 | // context.beginPath(); 20 | // context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); 21 | // context.fillStyle = player.color; 22 | // context.fill(); 23 | // context.lineWidth = 2; 24 | // context.strokeStyle = 'black'; 25 | // if(player.color == "#000") { 26 | // context.strokeStyle = 'white'; 27 | // } 28 | // context.stroke(); 29 | 30 | // // draw the eyes 31 | // context.beginPath(); 32 | // var eyeX = centerX - eyeXOffset; 33 | // var eyeY = centerY - eyeXOffset; 34 | // context.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false); 35 | // var eyeX = centerX + eyeXOffset; 36 | // context.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false); 37 | // context.fillStyle = 'black'; 38 | // context.fill(); 39 | 40 | // // draw the mouth 41 | // context.beginPath(); 42 | // context.arc(centerX, centerY, 20, 0, Math.PI, false); 43 | // context.stroke(); 44 | 45 | if(typeof(this.config.position) === "undefined") { 46 | this.config['position'] = {x: 0, y: 0, w: 100, h: 100}; 47 | } 48 | 49 | this["crafty"] = Crafty.e('2D, Canvas, player_'+this.config.color+', Fourway') 50 | .attr(this.config.position); 51 | 52 | //this.config["position"] = this.crafty.attr(); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Joguinho HUE 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 |
21 | 22 | 57 | 58 |
59 |
60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | --------------------------------------------------------------------------------