Browse files

basic map rendering

  • Loading branch information...
1 parent 70d4695 commit 0d8bd43b3f801af53cfe63b72a484f4edb627b9b @caseman committed Oct 11, 2011
Showing with 118 additions and 4 deletions.
  1. +4 −0 gravita/game.py
  2. +19 −0 gravita/map.py
  3. +6 −1 gravita/race.py
  4. +20 −0 gravita/restapi.py
  5. +27 −0 gravita/www/gravita.css
  6. +15 −0 gravita/www/gravita.js
  7. +27 −3 gravita/www/index.html
View
4 gravita/game.py
@@ -11,6 +11,9 @@
#
#############################################################################
+active_games = []
+
+
class Game(object):
def __init__(self, map):
@@ -35,3 +38,4 @@ def begin_turn(self):
for player in self.players:
player.begin_turn()
+
View
19 gravita/map.py
@@ -103,6 +103,16 @@ def sectors_in_circle(self, center_location, radius):
pass
return sectors
+ def as_dict(self):
+ d = {}
+ d['width'] = self.width
+ d['height'] = self.height
+ sectors = self._sectors
+ d['sectors'] = [
+ [sectors[x, y].as_dict() for x in range(self.width)]
+ for y in range(self.height)]
+ return d
+
class Sector(object):
@@ -111,5 +121,14 @@ def __init__(self, x, y):
self.ship = None
self.location = (x, y)
+ def as_dict(self):
+ d = {}
+ if self.planet is not None:
+ pname, ptype, _ = self.planet
+ d['planet'] = {'name': pname, 'type': ptype}
+ if self.ship is not None:
+ d['ship']
+ return d
+
def __repr__(self):
return '<Sector%r planet=%r ship=%r>' % (self.location, self.planet, self.ship)
View
7 gravita/race.py
@@ -14,7 +14,7 @@
class Race(object):
- """Gravtia race base class"""
+ """Gravita race base class"""
home_planet_type = None
planet_yields = {}
@@ -38,3 +38,8 @@ class Human(Race):
planet_yields['desert'] = planet.Yield(resources=3, research=2, bonus=4)
+races = {
+ 'human': Human(),
+ 'naree': Human(),
+ 'rone': Human(),
+}
View
20 gravita/restapi.py
@@ -12,6 +12,10 @@
#############################################################################
from pyramid.view import view_config
from gravita.user_profile import UserProfile
+from gravita.player import Player
+from gravita.race import races
+from gravita.game import Game, active_games
+from gravita.map import Map
class RestApi(object):
@@ -42,5 +46,21 @@ def profile_info(self):
'is_new': is_new,
}
+ @view_config(name='create_game', renderer='json', request_method='POST')
+ def create_game(self):
+ params = self.request.POST
+ profile, is_new = self.get_user_profile()
+ map_width = map_height = int(params['map_size'])
+ game_map = Map(map_width, map_height)
+ game_map.add_planets(int(float(params['density']) * map_width * map_height))
+ profile.game = Game(game_map)
+ profile.game.add_player(Player(profile.name, races[params['race']]))
+ active_games.append(profile.game)
+ return profile.game.map.as_dict()
+
+ @view_config(name='map', renderer='json')
+ def map(self):
+ profile, is_new = self.get_user_profile()
+ return profile.game.map.as_dict()
View
27 gravita/www/gravita.css
@@ -3,6 +3,10 @@ body {
color: lightyellow;
font-family: 'Orbitron', sans-serif;
background: url('/static/images/starfield.jpg');
+ -webkit-background-size: cover;
+ -moz-background-size: cover;
+ -o-background-size: cover;
+ background-size: cover;
}
.title {
@@ -75,4 +79,27 @@ label {
text-shadow: 0 0 2px black;
}
+.map {
+ table-layout: fixed;
+ border-collapse: collapse;
+}
+
+.map td {
+ width: 64px;
+ height: 64px;
+ text-align: center;
+ vertical-align: middle;
+ border: 1px dotted #335;
+}
+
+.map td:hover {
+ border: 1px solid #557;
+ box-shadow: 0 0 10px #fff;
+}
+
+.map img.planet {
+ width: 60px;
+ height: 60px;
+}
+
View
15 gravita/www/gravita.js
@@ -14,6 +14,21 @@ $gravita.load = function() {
$gravita.profile_info = user;
if (!user.in_game) {
$gravita.showTmpl("title-template", user);
+ } else {
+ $.get('/map', '', $gravita.renderMap);
}
});
}
+
+$gravita.createGame = function() {
+ $.post('/create_game',
+ $("#create-game-form").serialize(),
+ $gravita.renderMap);
+}
+
+$gravita.renderMap = function(map) {
+ $gravita.showTmpl("map-template", map);
+ var map_table = $(".map");
+ map_table.width(map.width * 64);
+ map_table.height(map.height * 64);
+}
View
30 gravita/www/index.html
@@ -38,9 +38,9 @@ <h2 class="title">Map Size</h2>
</div>
<div class="group">
<h2 class="title">Planet Density</h2>
- <label><input name="density" type="radio" value="high"/> High</label>
- <label><input name="density" type="radio" value="medium"/> Medium</label>
- <label><input name="density" type="radio" value="low"/> Low</label>
+ <label><input name="density" type="radio" value="0.075"/> High</label>
+ <label><input name="density" type="radio" value="0.05"/> Medium</label>
+ <label><input name="density" type="radio" value="0.025"/> Low</label>
</div>
<button type="submit">Create Game</button>
<button onClick="$gravita.showTmpl('title-template', $gravita.profile_info)"
@@ -49,9 +49,33 @@ <h2 class="title">Planet Density</h2>
</div>
</script>
+<script id="map-template" type="text/x-jquery-template">
+ <table class="map">
+ {{each(y, row) sectors}}
+ <tr>
+ {{each(x, sector) row}}
+ <td>
+ <a name="${x},${y}">
+ {{if sector.planet}}
+ <img class="planet"
+ src="/static/images/planets/${sector.planet.type + (((x+y*2) % 3) + 1)}.png"
+ title="${sector.planet.name}"/>
+ {{else}}
+ &nbsp;
+ {{/if}}
+ </a>
+ </td>
+ {{/each}}
+ </tr>
+ {{/each}}
+ </table>
+</script>
+
<script>
+ // Compile templates
$("#title-template").template("title-template");
$("#create-game-template").template("create-game-template");
+ $("#map-template").template("map-template");
</script>
</body>

0 comments on commit 0d8bd43

Please sign in to comment.