Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ui rework and other things

  • Loading branch information...
commit c130deda76e54f846c185a8d8b30e375bee711da 1 parent bd8264c
@jtruscott authored
View
16 gamelib/baddie.py
@@ -9,7 +9,7 @@
#http://stackoverflow.com/questions/4159331/python-speed-up-an-a-star-pathfinding-algorithm
-def aStar(current, end):
+def aStar(current, end, mover):
openSet = set()
openHeap = []
@@ -65,6 +65,8 @@ def retracePath(c):
while cc.parent is not None:
if cc.character in terrainCosts:
terrainCost += terrainCosts[cc.character]
+ if 'tower' in cc.effects:
+ terrainCost += mover.tower_avoidance
cc = cc.parent
tile.H += terrainCost
@@ -83,6 +85,9 @@ def __init__(self, x, y):
self.y = y
self.health = 3
self.move_delay = 2
+
+ self.tower_avoidance = random.randint(0,4)
+
self.dead = False
def set_path(self, path):
@@ -90,6 +95,7 @@ def set_path(self, path):
self.path = path[:]
for tile in path:
tile.in_path_of.add(self)
+ tile.highlights.add('pathfinding')
def move(self):
if self.dead:
@@ -106,7 +112,7 @@ def move(self):
leaving.baddies.discard(self)
if len(self.path) <= 1:
- log.debug("got there!")
+ log.debug("got to the target with %r hp", self.health)
all_baddies.discard(self)
return
@@ -140,11 +146,9 @@ def on_input(key):
start = world.get_at(b.x, b.y)
start.baddies.add(b)
paths = []
- for target, highlight in ((world.north_pole, 'pathing_north'), (world.south_pole, 'pathing_south')):
- path = aStar(start, target)
+ for target in (world.north_pole, world.south_pole):
+ path = aStar(start, target, b)
paths.append((len(path), path))
- for tile in path:
- tile.highlights.add(highlight)
#head down the shorter path
b.set_path(sorted(paths)[0][1])
View
11 gamelib/game.py
@@ -17,7 +17,10 @@ class GameShutdown(Exception):
mode = "game"
active_panel = "map"
-highlight_mode = "pathing_north"
+highlights = set(["tower"])
+
+fps = 10
+ticks = 0
def start():
log.debug("Game starting")
@@ -43,12 +46,16 @@ def run(event_name, *args):
pytality.term.flip()
def tick():
+ global ticks
+ delay = 1.0/fps
try:
log.debug("Tick thread started")
time.sleep(1)
while True:
- goal = time.time() + 0.1
+ goal = time.time() + delay
run('tick')
+ ticks += 1
+
sleeping_for = goal - time.time()
if sleeping_for < 0.01:
log.debug("uhoh, lagging, sleeping_for was %r", sleeping_for)
View
2  gamelib/main.py
@@ -7,7 +7,7 @@
log = logging.getLogger(__name__)
screen_width = 100
-screen_height = 80
+screen_height = 70
def main():
pytality.term.init(width=screen_width, height=screen_height)
View
128 gamelib/panels.py
@@ -27,37 +27,129 @@ def make_panel(name, x=0, y=0, width=0, height=0, core=None, title='', active=Fa
panel.children = (panel.disabled_box, panel.disabled_title, panel.core)
return panel
-left_panel = make_panel('left', width=main.screen_width - world.view_width - 4, height=world.view_height, core=pytality.buffer.Buffer(0, 0), title="Left!")
+def make_option_label(name, value, active=False, selected=False, **kwargs):
+ class Label( pytality.buffer.RichText):
+ def set_active(self, active=None, selected=None):
+ if active is not None:
+ self.active = active
+ else:
+ active = self.active
+ if selected is not None:
+ self.selected = selected
+ else:
+ selected = self.selected
+
+ color = "LIGHTGREY"
+ l,r = " ", " "
+ if active:
+ color = "WHITE"
+ if selected:
+ l,r = "(", ")"
+
+ self.format((color, l, r))
+
+ panel = Label("<%s>%s" + name + "%s</>", **kwargs)
+ panel.active = active
+ panel.selected = selected
+ panel.set_active(active)
+
+ panel.value = value
+ return panel
+
+highlight_buffer = pytality.buffer.Buffer(0, 0)
+bottom_buffer = pytality.buffer.Buffer(0, 0)
+
+left_panel = make_panel('left', width=main.screen_width - world.view_width - 4, height=main.screen_height-44, core=pytality.buffer.Buffer(0, 0), title="Left!")
map_panel = make_panel('map', x=left_panel.width, width=world.view_width, height=world.view_height, core=world.map_buffer, title="Game Map", active=True)
-bottom_panel = make_panel('bottom', y=left_panel.height, width=main.screen_width-2, height=main.screen_width-left_panel.height-2, core=pytality.buffer.Buffer(0, 0), title="Bottom!")
+highlight_panel = make_panel('highlight', x=left_panel.width, y=map_panel.height, height=main.screen_height - map_panel.height-2, width=map_panel.width-2, core=highlight_buffer, title="Highlighting")
+bottom_panel = make_panel('bottom', y=left_panel.height, width=main.screen_width - world.view_width - 4, height=40, core=bottom_buffer, title="Bottom!")
+
+status_bar = pytality.buffer.PlainText("X: %-4i Y: %-4i Time: %s", y=bottom_panel.height-3, x=bottom_panel.width-37)
+bottom_buffer.children.append(status_bar)
+
+@event.on('setup')
+def on_setup():
+ highlight_buffer.children = [
+ make_option_label("Tower Range", "tower", x=2, y=0, selected=True),
+ make_option_label("Enemy Pathfinding", "pathfinding", x=highlight_panel.width-23),
+ make_option_label("Thing", "thing", x=2, y=2),
+ ]
+ highlight_panel.child_index = None
+
+@event.on('game.predraw')
+def on_predraw():
+ x,y = world.cursor_pos()
+ cursor_cell = world.get_at(x, y)
+
+ #overlay the cursor
+ #(next draw cell.cell will get overwritten anyway)
+ n,e,w,s = cursor_cell.neighbors()
+ n.cell[2] = '\xc2'
+ e.cell[2] = '\xb4'
+ s.cell[2] = '\xc1'
+ w.cell[2] = '\xc3'
+
+ #update status bar
+ t = '%3i:%02i' % divmod(game.ticks/game.fps, 60)
+ status_bar.format((x, y, t))
@event.on('game.draw')
def on_draw():
left_panel.draw()
map_panel.draw()
+ highlight_panel.draw()
bottom_panel.draw()
@event.on('game.input')
def on_input(key):
- neighbors= dict(
- left=(bottom_panel, left_panel, map_panel),
- map=(left_panel, map_panel, bottom_panel),
- bottom=(map_panel, bottom_panel, left_panel),
- )
+ if key == '\t':
+ neighbors= dict(
+ left=(left_panel, map_panel),
+ map=(map_panel, highlight_panel),
+ highlight=(highlight_panel, bottom_panel),
+ bottom=(bottom_panel, left_panel),
+ )
- left, current, right = neighbors[game.active_panel]
+ current, next = neighbors[game.active_panel]
- log.debug(key)
- if key == 'P':
- activate = right
+ activate = next
deactivate = current
- else:
- return
- game.active_panel = activate.name
+ game.active_panel = activate.name
+
+ activate.children = (activate.enabled_box, activate.enabled_title, activate.core)
+ activate.dirty = True
+
+ deactivate.children = (deactivate.disabled_box, deactivate.disabled_title, deactivate.core)
+ deactivate.dirty = True
+
+ if activate.name == 'highlight':
+ activate.child_index = 0
+ highlight_buffer.children[0].set_active(active=True)
+
+ elif game.active_panel == 'highlight':
+ if key in ('left', 'right', 'up', 'down', ' ', 'enter'):
+ highlight_input(key)
+
+
+def highlight_input(key):
+ if key in ('left', 'up'):
+ highlight_buffer.children[highlight_panel.child_index].set_active(active=False)
+ highlight_panel.child_index = max(highlight_panel.child_index-1, 0)
+ highlight_buffer.children[highlight_panel.child_index].set_active(active=True)
+
+ if key in ('right', 'down'):
+ highlight_buffer.children[highlight_panel.child_index].set_active(active=False)
+ highlight_panel.child_index = min(len(highlight_buffer.children)-1, highlight_panel.child_index+1)
+ highlight_buffer.children[highlight_panel.child_index].set_active(active=True)
- activate.children = (activate.enabled_box, activate.enabled_title, activate.core)
- activate.dirty = True
+ if key in (' ', 'enter'):
+ child = highlight_buffer.children[highlight_panel.child_index]
+ if child.selected:
+ child.set_active(selected=False)
+ game.highlights.discard(child.value)
+ else:
+ child.set_active(selected=True)
+ game.highlights.add(child.value)
- deactivate.children = (deactivate.disabled_box, deactivate.disabled_title, deactivate.core)
- deactivate.dirty = True
+ highlight_panel.dirty = True
View
7 gamelib/tower.py
@@ -1,5 +1,6 @@
import pytality
+import game
import event
import world
import random
@@ -28,7 +29,7 @@ def tick(self):
for neighbor in cell.in_range(self.range):
if neighbor.baddies:
target = list(neighbor.baddies)[0]
- log.debug("%r firing at %r", self, target)
+ log.debug("%r firing at %r on tick %r", self, target, game.ticks)
self.fg = pytality.colors.LIGHTGREEN
target.take_damage(1)
self.fire_delay = 4
@@ -39,8 +40,8 @@ def tick(self):
@event.on('game.input')
def on_input(key):
if key == 'T':
-
- t = Tower(x=random.randint(0, world.map_width), y=random.randint(0, world.map_height))
+ x,y = world.cursor_pos()
+ t = Tower(x=x, y=y)
start = world.get_at(t.x, t.y)
start.tower = t
all_towers.add(t)
View
29 gamelib/world.py
@@ -13,8 +13,8 @@
Mountain = '\xb1'
highlight_colors = dict(
- pathing_north=colors.MAGENTA,
- pathing_south=colors.CYAN
+ pathfinding=colors.MAGENTA,
+ tower=colors.GREEN
)
map_width = 60
map_height = 60
@@ -24,6 +24,9 @@
view_x = 0
view_y = 0
+def cursor_pos():
+ return clamp_width(view_x + (view_width/2)), clamp_height(view_y + (view_height/2))
+
def clamp_width(value):
while value < 0:
value += map_width
@@ -73,25 +76,27 @@ def __init__(self, x, y, character=' ', fg=colors.LIGHTGREY, bg=colors.BLACK):
def reset_effects(self):
self.effects = set()
+ if not self.in_path_of:
+ self.highlights.discard('pathfinding')
def add_effects(self):
if self.tower:
for neighbor in self.in_range(self.tower.range):
neighbor.effects.add(self.tower.effect)
+ neighbor.highlights.add('tower')
def calculate_image(self, viewmode=None):
character = self.character
fg = self.fg
bg = self.bg
- if game.highlight_mode in self.highlights:
- bg = highlight_colors[game.highlight_mode]
- elif 'northpole' in self.effects:
+ for highlight in game.highlights:
+ if highlight in self.highlights:
+ bg = highlight_colors[highlight]
+ if 'northpole' in self.effects:
bg = colors.RED
elif 'southpole' in self.effects:
bg = colors.BLUE
- elif 'tower' in self.effects:
- bg = colors.GREEN
if self.tower:
fg = self.tower.fg
@@ -99,7 +104,7 @@ def calculate_image(self, viewmode=None):
character = 'T'
elif len(self.baddies):
- character = 'B'
+ character = 'E'
self.cell[0] = fg
@@ -240,14 +245,6 @@ def on_input(key):
view_x = clamp_width(view_x)
view_y = clamp_height(view_y)
- if key in ('n', 's', 'c'):
- if key == 'n':
- game.highlight_mode = 'pathing_north'
- elif key == 's':
- game.highlight_mode = 'pathing_south'
- else:
- clear_highlight()
-
@event.on('game.predraw')
def on_tick():
Please sign in to comment.
Something went wrong with that request. Please try again.