Skip to content

Commit

Permalink
ui rework and other things
Browse files Browse the repository at this point in the history
  • Loading branch information
jtruscott committed Apr 21, 2012
1 parent bd8264c commit c130ded
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 46 deletions.
16 changes: 10 additions & 6 deletions gamelib/baddie.py
Expand Up @@ -9,7 +9,7 @@




#http://stackoverflow.com/questions/4159331/python-speed-up-an-a-star-pathfinding-algorithm #http://stackoverflow.com/questions/4159331/python-speed-up-an-a-star-pathfinding-algorithm
def aStar(current, end): def aStar(current, end, mover):


openSet = set() openSet = set()
openHeap = [] openHeap = []
Expand Down Expand Up @@ -65,6 +65,8 @@ def retracePath(c):
while cc.parent is not None: while cc.parent is not None:
if cc.character in terrainCosts: if cc.character in terrainCosts:
terrainCost += terrainCosts[cc.character] terrainCost += terrainCosts[cc.character]
if 'tower' in cc.effects:
terrainCost += mover.tower_avoidance
cc = cc.parent cc = cc.parent
tile.H += terrainCost tile.H += terrainCost


Expand All @@ -83,13 +85,17 @@ def __init__(self, x, y):
self.y = y self.y = y
self.health = 3 self.health = 3
self.move_delay = 2 self.move_delay = 2

self.tower_avoidance = random.randint(0,4)

self.dead = False self.dead = False


def set_path(self, path): def set_path(self, path):
self.full_path = path self.full_path = path
self.path = path[:] self.path = path[:]
for tile in path: for tile in path:
tile.in_path_of.add(self) tile.in_path_of.add(self)
tile.highlights.add('pathfinding')


def move(self): def move(self):
if self.dead: if self.dead:
Expand All @@ -106,7 +112,7 @@ def move(self):
leaving.baddies.discard(self) leaving.baddies.discard(self)


if len(self.path) <= 1: if len(self.path) <= 1:
log.debug("got there!") log.debug("got to the target with %r hp", self.health)
all_baddies.discard(self) all_baddies.discard(self)
return return


Expand Down Expand Up @@ -140,11 +146,9 @@ def on_input(key):
start = world.get_at(b.x, b.y) start = world.get_at(b.x, b.y)
start.baddies.add(b) start.baddies.add(b)
paths = [] paths = []
for target, highlight in ((world.north_pole, 'pathing_north'), (world.south_pole, 'pathing_south')): for target in (world.north_pole, world.south_pole):
path = aStar(start, target) path = aStar(start, target, b)
paths.append((len(path), path)) paths.append((len(path), path))
for tile in path:
tile.highlights.add(highlight)


#head down the shorter path #head down the shorter path
b.set_path(sorted(paths)[0][1]) b.set_path(sorted(paths)[0][1])
Expand Down
11 changes: 9 additions & 2 deletions gamelib/game.py
Expand Up @@ -17,7 +17,10 @@ class GameShutdown(Exception):
mode = "game" mode = "game"
active_panel = "map" active_panel = "map"


highlight_mode = "pathing_north" highlights = set(["tower"])

fps = 10
ticks = 0


def start(): def start():
log.debug("Game starting") log.debug("Game starting")
Expand All @@ -43,12 +46,16 @@ def run(event_name, *args):
pytality.term.flip() pytality.term.flip()


def tick(): def tick():
global ticks
delay = 1.0/fps
try: try:
log.debug("Tick thread started") log.debug("Tick thread started")
time.sleep(1) time.sleep(1)
while True: while True:
goal = time.time() + 0.1 goal = time.time() + delay
run('tick') run('tick')
ticks += 1

sleeping_for = goal - time.time() sleeping_for = goal - time.time()
if sleeping_for < 0.01: if sleeping_for < 0.01:
log.debug("uhoh, lagging, sleeping_for was %r", sleeping_for) log.debug("uhoh, lagging, sleeping_for was %r", sleeping_for)
Expand Down
2 changes: 1 addition & 1 deletion gamelib/main.py
Expand Up @@ -7,7 +7,7 @@
log = logging.getLogger(__name__) log = logging.getLogger(__name__)


screen_width = 100 screen_width = 100
screen_height = 80 screen_height = 70


def main(): def main():
pytality.term.init(width=screen_width, height=screen_height) pytality.term.init(width=screen_width, height=screen_height)
Expand Down
128 changes: 110 additions & 18 deletions gamelib/panels.py
Expand Up @@ -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) panel.children = (panel.disabled_box, panel.disabled_title, panel.core)
return panel 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) 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') @event.on('game.draw')
def on_draw(): def on_draw():
left_panel.draw() left_panel.draw()
map_panel.draw() map_panel.draw()
highlight_panel.draw()
bottom_panel.draw() bottom_panel.draw()


@event.on('game.input') @event.on('game.input')
def on_input(key): def on_input(key):
neighbors= dict( if key == '\t':
left=(bottom_panel, left_panel, map_panel), neighbors= dict(
map=(left_panel, map_panel, bottom_panel), left=(left_panel, map_panel),
bottom=(map_panel, bottom_panel, left_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) activate = next
if key == 'P':
activate = right
deactivate = current 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) if key in (' ', 'enter'):
activate.dirty = True 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) highlight_panel.dirty = True
deactivate.dirty = True
7 changes: 4 additions & 3 deletions gamelib/tower.py
@@ -1,5 +1,6 @@
import pytality import pytality


import game
import event import event
import world import world
import random import random
Expand Down Expand Up @@ -28,7 +29,7 @@ def tick(self):
for neighbor in cell.in_range(self.range): for neighbor in cell.in_range(self.range):
if neighbor.baddies: if neighbor.baddies:
target = list(neighbor.baddies)[0] 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 self.fg = pytality.colors.LIGHTGREEN
target.take_damage(1) target.take_damage(1)
self.fire_delay = 4 self.fire_delay = 4
Expand All @@ -39,8 +40,8 @@ def tick(self):
@event.on('game.input') @event.on('game.input')
def on_input(key): def on_input(key):
if key == 'T': if key == 'T':

x,y = world.cursor_pos()
t = Tower(x=random.randint(0, world.map_width), y=random.randint(0, world.map_height)) t = Tower(x=x, y=y)
start = world.get_at(t.x, t.y) start = world.get_at(t.x, t.y)
start.tower = t start.tower = t
all_towers.add(t) all_towers.add(t)
Expand Down
29 changes: 13 additions & 16 deletions gamelib/world.py
Expand Up @@ -13,8 +13,8 @@
Mountain = '\xb1' Mountain = '\xb1'


highlight_colors = dict( highlight_colors = dict(
pathing_north=colors.MAGENTA, pathfinding=colors.MAGENTA,
pathing_south=colors.CYAN tower=colors.GREEN
) )
map_width = 60 map_width = 60
map_height = 60 map_height = 60
Expand All @@ -24,6 +24,9 @@
view_x = 0 view_x = 0
view_y = 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): def clamp_width(value):
while value < 0: while value < 0:
value += map_width value += map_width
Expand Down Expand Up @@ -73,33 +76,35 @@ def __init__(self, x, y, character=' ', fg=colors.LIGHTGREY, bg=colors.BLACK):


def reset_effects(self): def reset_effects(self):
self.effects = set() self.effects = set()
if not self.in_path_of:
self.highlights.discard('pathfinding')


def add_effects(self): def add_effects(self):
if self.tower: if self.tower:
for neighbor in self.in_range(self.tower.range): for neighbor in self.in_range(self.tower.range):
neighbor.effects.add(self.tower.effect) neighbor.effects.add(self.tower.effect)
neighbor.highlights.add('tower')


def calculate_image(self, viewmode=None): def calculate_image(self, viewmode=None):
character = self.character character = self.character
fg = self.fg fg = self.fg
bg = self.bg bg = self.bg


if game.highlight_mode in self.highlights: for highlight in game.highlights:
bg = highlight_colors[game.highlight_mode] if highlight in self.highlights:
elif 'northpole' in self.effects: bg = highlight_colors[highlight]
if 'northpole' in self.effects:
bg = colors.RED bg = colors.RED
elif 'southpole' in self.effects: elif 'southpole' in self.effects:
bg = colors.BLUE bg = colors.BLUE
elif 'tower' in self.effects:
bg = colors.GREEN


if self.tower: if self.tower:
fg = self.tower.fg fg = self.tower.fg
bg = colors.GREEN bg = colors.GREEN
character = 'T' character = 'T'


elif len(self.baddies): elif len(self.baddies):
character = 'B' character = 'E'




self.cell[0] = fg self.cell[0] = fg
Expand Down Expand Up @@ -240,14 +245,6 @@ def on_input(key):
view_x = clamp_width(view_x) view_x = clamp_width(view_x)
view_y = clamp_height(view_y) 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') @event.on('game.predraw')
def on_tick(): def on_tick():


Expand Down

0 comments on commit c130ded

Please sign in to comment.