diff --git a/app.js b/app.js index 98c2cdd..b80ee1b 100644 --- a/app.js +++ b/app.js @@ -38,10 +38,10 @@ db.once("open", function(){ })); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); - app.use(browserify(__dirname+"/client/game-index.js", { - mount: "/game-index.js", - cache: false - })); + // app.use(browserify(__dirname+"/client/game-index.js", { + // mount: "/game-index.js", + // cache: false + // })); }); app.configure('development', function(){ @@ -97,4 +97,4 @@ db.once("open", function(){ }); -}); \ No newline at end of file +}); diff --git a/client/collections/MapCollection.js b/client/collections/MapCollection.js new file mode 100644 index 0000000..8999110 --- /dev/null +++ b/client/collections/MapCollection.js @@ -0,0 +1,3 @@ +module.exports = Backbone.Collection.extend({ + model:require("../models/MapElement"), +}); \ No newline at end of file diff --git a/client/collections/PlayersCollection.js b/client/collections/PlayersCollection.js new file mode 100644 index 0000000..547d943 --- /dev/null +++ b/client/collections/PlayersCollection.js @@ -0,0 +1,3 @@ +module.exports = Backbone.Collection.extend({ + model:require("../models/Player"), +}); \ No newline at end of file diff --git a/client/game-index.js b/client/game-index.js index b2f1382..56cf967 100644 --- a/client/game-index.js +++ b/client/game-index.js @@ -1,103 +1,129 @@ -_ = require("underscore"); var socket = io.connect(); +var PlayersCollection = require("./collections/PlayersCollection"); +var players = new PlayersCollection(); -var Player = require("./views/Player"); -var players = []; - -var getPlayerByUsername = function(username){ - for(var i = 0; i
"); -} - -_.extend(module.exports.prototype, { - render: function(){ - this.$el.find(".name").html(this.username); - this.$el.addClass("player"); - - if(this.hasTreasure) - this.$el.find(".coin").show();//addClass("hasTreasure"); - else - this.$el.find(".coin").hide();//this.$el.removeClass("hasTreasure"); - - this.$el.css({ - left: this.x, - top: this.y - }); - return this; - }, - remove: function(){ - this.$el.remove(); - } -}); \ No newline at end of file diff --git a/client/views/PlayerView.js b/client/views/PlayerView.js new file mode 100644 index 0000000..4a4eda4 --- /dev/null +++ b/client/views/PlayerView.js @@ -0,0 +1,49 @@ +module.exports = require("./View").extend({ + className: "player", + coin: "
", + name:"
", + turnLeft:{ + "-moz-transform": "scaleX(-1)", + "-o-transform": "scaleX(-1)", + "-webkit-transform": "scaleX(-1)", + "transform": "scaleX(-1)" + }, + turnRight:{ + "-moz-transform": "scaleX(1)", + "-o-transform": "scaleX(1)", + "-webkit-transform": "scaleX(1)", + "transform": "scaleX(1)" + }, + + initialize:function(){ + this.model.on("change:x change:y change:z", this.render, this); + this.model.on("change:hasTreasure", this.treasure, this); + this.model.on("remove", this.remove, this); + var name = $(this.name); + name.html(this.model.get('username')); + this.$el.append($(this.coin), name); + this.treasure(); + }, + + render: function(){ + var x = this.model.get("x"); + var prevx = this.model.previous("x"); + if(x!=prevx) + x 0 && !pressed) { //moving in that direction and not pressing - stopping @@ -32,6 +37,29 @@ module.exports = function(user){ if (pressed && val < MAX_SPEED) { //apply acceleration acceleration[dir] = Math.min(MAX_SPEED, (val + ACCELERATION)) * coef; } + }; + + var JUMP_POWER = 5; + var GROUND = 1; + var G = 1; + var onAir = false; + + var jump = function(pressed, z){ + if (pressed && !onAir){//Jumping + onAir = true; + acceleration.z = JUMP_POWER; + return z + acceleration.z; + } + if(onAir && z>GROUND){ + acceleration.z -= G; + return z + acceleration.z; + } + if(onAir && z<=GROUND){ + onAir = false; + acceleration.z = 0; + return GROUND; + } + return z; } @@ -45,6 +73,7 @@ module.exports = function(user){ //actually change position this.state.x += acceleration.x; this.state.y += acceleration.y; + this.state.z = jump(this.direction.jump, this.state.z); } this.getDistance = function (player) { diff --git a/public/game-index.js b/public/game-index.js new file mode 100644 index 0000000..1c8427a --- /dev/null +++ b/public/game-index.js @@ -0,0 +1,352 @@ +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o", + name:"
", + turnLeft:{ + "-moz-transform": "scaleX(-1)", + "-o-transform": "scaleX(-1)", + "-webkit-transform": "scaleX(-1)", + "transform": "scaleX(-1)" + }, + turnRight:{ + "-moz-transform": "scaleX(1)", + "-o-transform": "scaleX(1)", + "-webkit-transform": "scaleX(1)", + "transform": "scaleX(1)" + }, + + initialize:function(){ + this.model.on("change:x change:y change:z", this.render, this); + this.model.on("change:hasTreasure", this.treasure, this); + this.model.on("remove", this.remove, this); + var name = $(this.name); + name.html(this.model.get('username')); + this.$el.append($(this.coin), name); + this.treasure(); + }, + + render: function(){ + var x = this.model.get("x"); + var prevx = this.model.previous("x"); + if(x!=prevx) + x=b))this.iframe=i('