Permalink
Browse files

Merge branch 'master' of github.com:psubocz/garden-dWARves

Conflicts:
	client/src/world.js
	client/statics/index.html
	client/statics/style.css
  • Loading branch information...
2 parents fa4bb7c + 99a2179 commit 6af074fb12741d78f51058166dea5ab200a2e49f @p1niu p1niu committed Apr 15, 2012
View
@@ -1,5 +1,6 @@
var gamejs = require("gamejs"),
matrix = require("gamejs/utils/matrix"),
+ vector = require("gamejs/utils/vectors"),
world = require("world"),
font = require("gamejs/font"),
Castle = require("elements/castle").Castle;
@@ -17,6 +18,7 @@ var BattleScene = exports.BattleScene = function(director, playerA, playerB) {
this._font = new font.Font('48px sans');
this.scrolling = false;
+ this.forking = false;
this._locked = false;
this._boundingbox = new gamejs.Rect(0, 0, this.width, this.height);
@@ -35,9 +37,13 @@ var BattleScene = exports.BattleScene = function(director, playerA, playerB) {
this.playerA = playerA;
this.playerB = playerB;
+
+ this.castleA = new Castle(director, playerA.name, new gamejs.Rect(80, 230, 400, 300), false);
+ this.add(this.castleA);
+ this.castleB = new Castle(director, playerB.name, new gamejs.Rect(1120, 230, 400, 300), true)
+ this.add(this.castleB);
- this.add(new Castle(director, playerA.name, new gamejs.Rect(150, 270, 250, 300)));
- this.add(new Castle(director, playerB.name, new gamejs.Rect(1150, 270, 250, 300)));
+ this.fork = this.castleA.fork;
};
BattleScene.prototype.drawBackground = function() {
@@ -77,25 +83,34 @@ BattleScene.prototype.handleEvent = function handleEvent(event) {
return;
switch(event.type) {
case gamejs.event.MOUSE_DOWN:
+ var vpos = vector.add(this._viewport.topleft, event.pos);
+ var bit = this.fork.rect.collidePoint(vpos);
+ if(bit) {
+ this.fork.start_sling(event);
+ this.forking = true;
+ break;
+ }
this.scrolling = true;
break;
case gamejs.event.MOUSE_UP:
+ if(this.forking) {
+ this.fork.stop_sling(event);
+ this.forking = false;
+ break;
+ }
this.scrolling = false;
break;
case gamejs.event.MOUSE_MOTION:
- if(!this.scrolling)
+ if(this.forking) {
+ console.log(event);
+ this.fork.sling_moved(event);
+ break;
+ }
+ if(!this.scrolling) {
break;
+ }
this.scrollNowBy(event.rel[0], event.rel[1]);
break;
-// case gamejs.event.MOUSE_WHEEL:
-// var new_zoom = this.zoom * (1 - event.delta/100);
-// this.zoom = Math.max(this.min_zoom, Math.min(this.max_zoom, new_zoom));
-// this._zoomedport = scaleRect(this._viewport,
-// this._boundingbox,
-// new gamejs.Rect(0, 0, this.director.width, this.director.height),
-// this.zoom
-// );
-// break;
}
};
@@ -4,21 +4,42 @@
var gamejs = require("gamejs"),
sprite = require("gamejs/sprite"),
game_world = require("world"),
- transform = require("gamejs/transform");
+ transform = require("gamejs/transform"),
+ Dwarf = require("elements/dwarf").Dwarf,
+ Fork = require("elements/fork").Fork;
/**
* @param elements - Castle pieces.
*/
-var Castle = exports.Castle = function Castle(director, name, bbox, elements) {
+var Castle = exports.Castle = function Castle(director, name, bbox, flip, elements) {
Castle.superConstructor.apply(this, arguments);
this.name = name;
this.elements = elements || [];
this.rect = bbox.clone();
+ this._font = new gamejs.font.Font("36px serif");
+ this._name = this._font.render(this.name);
+ this._flipped = !!flip;
+
+ var dwarf = new Dwarf(director, 100);
+ dwarf.layout(this.rect, this.flipX(200), 0);
+ this.add(dwarf);
+
+ var fork = this.fork = new Fork(director, flip);
+ fork.layout(this.rect, this.flipX(100), -20);
+ this.add(fork);
};
gamejs.utils.objects.extend(Castle, sprite.Group);
+Castle.prototype.flipX = function(x) {
+ return !this._flipped ? x : this.rect.width - x;
+};
+
Castle.prototype.draw = function(canvas) {
sprite.Group.prototype.draw.apply(this, arguments);
- // canvas.blit(this._font.render(this.name), [this.rect.left, this.rect.top]);
+ var nsize = this._name.getSize();
+ canvas.blit(this._name, [
+ this.rect.left + (this.rect.width - nsize[0])/2,
+ this.rect.top + this.rect.height+10
+ ]);
};
@@ -9,27 +9,23 @@ var gamejs = require("gamejs"),
/**
* @param elements - Castle pieces.
*/
-var Dwarf = exports.Dwarf = function Castle(director, health, id) {
+var Dwarf = exports.Dwarf = function Dwarf(director, health, id) {
Dwarf.superConstructor.apply(this, arguments);
this.id = id || "dwarf";
- this.image = game_world.get_image(id);
+ this.image = game_world.get_image(this.id);
this.max_health = this.health = health;
this.director = director;
};
gamejs.utils.objects.extend(Dwarf, sprite.Sprite);
Dwarf.prototype.draw = function(canvas) {
- Sprite.prototype.draw.apply(this, arguments);
- gamejs.draw.rect(canvas,
- new gamejs.Rect(this.rect.left,
- this.rect.top-20,
- this.rect.width,
- 20), "red");
-
- gamejs.draw.rect(canvas,
- new gamejs.Rect(this.rect.left,
- this.rect.top-20,
- this.rect.width * (this.healt/this.max_health),
- 20), "green")
+ gamejs.sprite.Sprite.prototype.draw.apply(this, arguments);
+ gamejs.draw.rect(canvas, "red",
+ new gamejs.Rect(this.rect.left-0.5,
+ this.rect.top+this.rect.height+2, this.rect.width, 6));
+ gamejs.draw.rect(canvas, "green",
+ new gamejs.Rect(this.rect.left-0.5,
+ this.rect.top+this.rect.height+2,
+ this.rect.width*(this.health/this.max_health), 6.5));
};
View
@@ -24,6 +24,16 @@ gamejs.Surface.prototype.raw_blit = function(src, sx, sy, sw, sh, dx, dy, dw,
this.context.restore();
};
+gamejs.sprite.Sprite.prototype.layout = function(bbox, left, bottom) {
+ var sizes = this.image.getSize();
+ this.rect = new gamejs.Rect(
+ bbox.left + left - sizes[0]/2,
+ bbox.top + bbox.height - bottom - sizes[1],
+ sizes[0], sizes[1]
+ );
+ console.log(this.rect);
+};
+
gamejs.ready(function() {
function Director(width, height) {
@@ -73,7 +83,7 @@ gamejs.ready(function() {
return this.activeScene;
};
- var director = new Director(800, 600);
+ var director = new Director(1024, 600);
director.start(new SplashScene(director));
$("#start").click(function(event) {
View
@@ -3,7 +3,7 @@ var gamejs = require("gamejs"), transform = require("gamejs/transform");
var sprites = exports.OBJECT_SPRITES = {
"cannonball": {
path: "./statics/images/cannonball.png",
- scale: 0.5
+ scale: 0.35
},
"cloud_1": {path: "./statics/images/cloud_1.png"},
"cloud_2": {path: "./statics/images/cloud_2.png"},
@@ -19,13 +19,15 @@ var sprites = exports.OBJECT_SPRITES = {
"metal": {path: "./statics/images/metal.png"},
"wood": {path: "./statics/images/wood.png"},
"moss": {path: "./statics/images/moss.png"},
- "dwarf": {path: "./statics/images/dwarf.png", scale: 0.3},
+ "dwarf": {path: "./statics/images/dwarf.png", scale: 0.5},
+ "fork": {path: "./statics/images/fork.png", scale: 0.7}
};
exports.get_image = function(id) {
var obj = sprites[id];
- if(!obj)
+ if(!obj) {
throw Error("No sprite for id: " + id);
+ }
var img = gamejs.image.load(obj.path), sizes = img.getSize();
return sprites[id].scale !== undefined ?
transform.scale(img, [sizes[0]*obj.scale, sizes[1]*obj.scale]) : img;
@@ -1,4 +1,4 @@
-<!DOCTYPE html>
+<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
@@ -35,6 +35,6 @@
<span id="load_msg"></span>
</div>
</div>
- </div>
+ <!-- </div> -->
</body>
</html>
View
@@ -17,7 +17,7 @@ body {
margin: 35px;
position: relative;
overflow: hidden;
- min-width: 800px;
+ width: 1024px;
min-height: 600px;
width: 800px;
}
@@ -26,8 +26,6 @@ body {
display: block;
position: absolute;
background-color: black;
- width: 800px;
- height: 600px;
}
button#start {
@@ -106,4 +104,3 @@ label#player_name_label {
font-size: 1.4em;
margin-top: 17px;
}
-
View
@@ -67,11 +67,14 @@ def on_search_for_opponent(self, filters):
def on_actor_joined(self, actor, arena):
if actor == self:
self._arena = arena
+ self.emit('joined_arena', self.user_data())
else:
self.emit('opponent_joined', actor.user_data())
def on_arena_actors(self, actors):
for actor in actors:
+ if actor == self:
+ continue
self.emit('opponent_joined', actor.user_data())
def on_game_ready(self):
View
@@ -24,10 +24,10 @@ def on_chat(self, actor, txt):
self._send_all('chat', (actor, txt))
def _send(self, actor, event, args=()):
- gevent.spawn(actor.inbox.put, (event, args))
+ actor.inbox.put((event, args))
def _sendbyid(self, actorid, event, args=()):
- gevent.spawn(self._actors[actorid].inbox.put, (event, args))
+ self._actors[actorid].inbox.put((event, args))
def _send_all(self, event, args=()):
for actor in self._actors:
View
@@ -0,0 +1,47 @@
+from Box2D import *
+
+class RealArena(object):
+
+ def __init__(self):
+ self._last_sleeping = []
+
+ gravity = b2Vec2(0, -10)
+ doSleep = True
+ self._world = b2World(gravity=gravity, doSleep=doSleep)
+
+ groundBody = self._world.CreateStaticBody( position=(0,0), shapes=b2PolygonShape(box=(100, 0.36)))
+
+ self._body = self._world.CreateDynamicBody(position=(50,4), userData=123)
+ box = self._body.CreatePolygonFixture(box=(0.11,0.11), density=1, friction=0.3)
+ self._body.did = 123
+ self._timeStep = 1.0 / 60
+ self._vel_iters = 10
+ self._pos_iters = 8
+
+ def tick(self):
+
+ self._world.Step(self._timeStep, self._vel_iters, self._pos_iters)
+ new_sleeping = []
+ result = []
+ for body in self._world.bodies:
+ if body.userData == None:
+ continue
+ if not body.awake:
+ new_sleeping.append(body.userData)
+ if not body.awake and body.userData in self._last_sleeping:
+ continue
+ result.append((body.userData, body.position.x, body.position.y, body.angle))
+
+ self._last_sleeping = new_sleeping
+ return result
+
+if __name__ == "__main__":
+
+ arena = RealArena()
+ while True:
+ r = arena.tick()
+ if r == []:
+ break
+ print r
+
+
View
@@ -0,0 +1,8 @@
+# http://code.google.com/p/pybox2d/wiki/BuildingfromSource
+svn+http://pybox2d.googlecode.com/svn/trunk/
+bottle==0.10.9
+gevent==0.13.7
+git+http://github.com/abourget/gevent-socketio.git
+gevent-websocket==0.3.4
+greenlet==0.3.4
+wsgiref==0.1.2

0 comments on commit 6af074f

Please sign in to comment.