Skip to content

Commit

Permalink
- Added input handler (command line at the moment)
Browse files Browse the repository at this point in the history
- Added ability to quit the game (was not possible until now xP)
- Added first ASCII UI and widgets:
    - Need bar
    - Activity bar
    - World view
  • Loading branch information
Florian R. A. Angermeier committed Jun 30, 2011
1 parent 012e005 commit 896e46d
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 35 deletions.
36 changes: 31 additions & 5 deletions Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,52 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

import os
from time import sleep
from twisted.internet import reactor

import gameEngine
from gameEngine.ServerFactory import ServerFactory
from gameEngine.InputHandler import InputHandler
from gameEngine.World import World

import asciiWidgets
from asciiWidgets.AsciiUI import AsciiUI

class Server:
def __init__( self ):
# Initializing the simulation
self.world = World( 2 )
self.stop = False
self.threadPool = None
self.world = World( self, 2 )
self.inputHandler = InputHandler( self )
self.asciiUI = AsciiUI( self, self.world )

def quitLoop( self ):
while( self.stop == False ):
sleep( 0.1 )
reactor.stop()

def run( self ):
self.threadPool = reactor.getThreadPool()

print '\033[32mInfo\033[0m Starting world thread...'
reactor.callInThread( self.world.run )
reactor.callInThread( self.world.run, self.threadPool )

print '\033[32mInfo\033[0m Starting input handler thread...'
reactor.callInThread( self.inputHandler.run, self.threadPool )

print '\033[32mInfo\033[0m Starting ASCII UI thread...'
reactor.callInThread( self.asciiUI.run, self.threadPool )

reactor.callLater( 1, self.quitLoop )

print '\033[32mInfo\033[0m Starting listening socket...'
reactor.listenTCP( 65000, ServerFactory() )

print '\033[32mInfo\033[0m Starting reactor...'
reactor.run()

if( __name__ == '__main__' ):
Server = Server()
Server.run()
os.system( 'clear' )
32 changes: 32 additions & 0 deletions asciiWidgets/ActivityBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/python
#*** encoding:utf-8 ***

# This file is part of La Vida
# Copyright (C) 2011 Florian R. A. Angermeier
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

from AsciiWidget import AsciiWidget

class ActivityBar( AsciiWidget ):
def __init__( self, pos, character ):
self.cmdPrefix = '\033['
self.cmdSeperator = ';'
self.cmdPostfixPosition = 'H'
self.cmdPostfixColor = 'm'
self.cmdTab = '\t'
self.pos = pos
self.character = character

def printWidget( self ):
cmdPosition = '%s%i%s%i%s' % ( self.cmdPrefix, self.pos[0], self.cmdSeperator, self.pos[1], self.cmdPostfixPosition )
output = '%sActivity: %s (%s)' % ( cmdPosition, self.character.brain.activity, self.character.brain.activityTimer )
print '%s' % ( output )
55 changes: 55 additions & 0 deletions asciiWidgets/AsciiUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/python
#*** encoding:utf-8 ***

# This file is part of La Vida
# Copyright (C) 2011 Florian R. A. Angermeier
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

import os
from time import sleep
from twisted.internet import reactor

from GameClock import GameClock
from NeedBar import NeedBar
from ActivityBar import ActivityBar
from MapView import MapView

class AsciiUI:
# The ASCII user interface base class which provides the drawing loop.

def __init__( self, server, world ):
self.server = server
self.gameClock = GameClock( ( 1, 1 ) , world)
self.needBarSleep = NeedBar( ( 2, 1 ), 'Sleep ', world.characters[ 0 ], 'sleep' )
self.needBarFood = NeedBar( ( 3, 1 ), 'Food ', world.characters[ 0 ], 'food' )
self.needBarWater = NeedBar( ( 4, 1 ), 'Water ', world.characters[ 0 ], 'water' )
self.needBarUrination = NeedBar( ( 2, 30 ), 'Urination ', world.characters[ 0 ], 'urination' )
self.needBarHygiene = NeedBar( ( 3, 30 ), 'Hygiene ', world.characters[ 0 ], 'hygiene' )
self.needBarFun = NeedBar( ( 4, 30 ), 'Fun ', world.characters[ 0 ], 'fun' )
self.needBarSocial = NeedBar( ( 2, 60 ), 'Social ', world.characters[ 0 ], 'social' )
self.activityBar = ActivityBar( ( 5, 1 ), world.characters[ 0 ] )
self.mapView = MapView( (6, 0), world )

def run( self, treadPool ):
os.system( 'clear' )
while( self.server.stop != True ):
self.gameClock.printWidget()
self.needBarSleep.printWidget()
self.needBarFood.printWidget()
self.needBarWater.printWidget()
self.needBarUrination.printWidget()
self.needBarHygiene.printWidget()
self.needBarFun.printWidget()
self.needBarSocial.printWidget()
self.activityBar.printWidget()
self.mapView.printWidget()
sleep( 1 )
26 changes: 26 additions & 0 deletions asciiWidgets/AsciiWidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python
#*** encoding:utf-8 ***

# This file is part of La Vida
# Copyright (C) 2011 Florian R. A. Angermeier
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

class AsciiWidget:
# The abstract base class for all ascii widgets.

def __init__( self, pos ):
self.cmdPrefix = '\033['
self.cmdSeperator = ';'
self.cmdPostfixPosition = 'H'
self.cmdPostfixColor = 'm'
self.cmdTab = '\t'
self.pos = pos
36 changes: 36 additions & 0 deletions asciiWidgets/GameClock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python
#*** encoding:utf-8 ***

# This file is part of La Vida
# Copyright (C) 2011 Florian R. A. Angermeier
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

from AsciiWidget import AsciiWidget

class GameClock( AsciiWidget ):
def __init__( self, pos, world ):
self.cmdPrefix = '\033['
self.cmdSeperator = ';'
self.cmdPostfixPosition = 'H'
self.cmdPostfixColor = 'm'
self.cmdTab = '\t'
self.pos = pos
self.world = world

def printWidget( self ):
cmdPosition = '%s%i%s%i%s' % ( self.cmdPrefix, self.pos[0], self.cmdSeperator, self.pos[1], self.cmdPostfixPosition )
cmdColor = '%s1%s' % ( self.cmdPrefix, self.cmdPostfixColor )
output = '%s%sGame date: %i/%i/%i%sGame time: %i:%i:%i' % ( cmdPosition, cmdColor, self.world.gameMonth, self.world.gameDay, self.world.gameYear,
self.cmdTab, self.world.gameHour, self.world.gameMinute, self.world.gameSecond )
cmdColor = '%s0%s' % ( self.cmdPrefix, self.cmdPostfixColor )
output += cmdColor
print '%s' % ( output )
61 changes: 61 additions & 0 deletions asciiWidgets/MapView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python
#*** encoding:utf-8 ***

# This file is part of La Vida
# Copyright (C) 2011 Florian R. A. Angermeier
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

from AsciiWidget import AsciiWidget

class MapView( AsciiWidget ):
def __init__( self, pos, world ):
self.cmdPrefix = '\033['
self.cmdSeperator = ';'
self.cmdPostfixPosition = 'H'
self.cmdPostfixColor = 'm'
self.cmdTab = '\t'
self.pos = pos
self.world = world
self.asciiTiles = { 0: '%s33%s42%s"%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
1: '%s30%s40%s:%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
2: '%s31%s43%s#%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
3: '%s30%s44%s~%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
4: '%s30%s44%s+%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ) }
self.asciiObject = { 'Bed': '%s1%s37%s43%sH%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
'Cooker': '%s1%s30%s47%s8%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
'Refrigerator': '%s1%s30%s47%s*%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
'Toilet': '%s1%s30%s47%sO%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
'Shower': '%s1%s30%s47%s^%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ),
'TvSet': '%s1%s30%s43%sY%s0%s' % ( self.cmdPrefix, self.cmdSeperator, self.cmdSeperator, self.cmdPostfixColor, self.cmdPrefix, self.cmdPostfixColor ) }

def printWidget( self ):
cmdPosition = '%s%i%s%i%s' % ( self.cmdPrefix, self.pos[0], self.cmdSeperator, self.pos[1], self.cmdPostfixPosition )
print '%sMap view' % ( cmdPosition )
rowId = 0
colId = 0
for row in self.world.typeMap:
rowId += 1
cmdPosition = '%s%i%s%i%s' % ( self.cmdPrefix, self.pos[0] + rowId, self.cmdSeperator, self.pos[1], self.cmdPostfixPosition )
output = '%s' % ( cmdPosition )
for col in row:
output += self.asciiTiles[ col ]
print '%s' % ( output )

for object in self.world.objects:
cmdPosition = '%s%i%s%i%s' % ( self.cmdPrefix, self.pos[0] + object.pos[0], self.cmdSeperator, self.pos[1] + object.pos[1], self.cmdPostfixPosition )
output = '%s%s' % ( cmdPosition, self.asciiObject[ object.type ] )
print output

for character in self.world.characters:
cmdPosition = '%s%i%s%i%s' % ( self.cmdPrefix, self.pos[0] + character.pos[0], self.cmdSeperator, self.pos[1] + character.pos[1], self.cmdPostfixPosition )
output = '%sC' % ( cmdPosition )
print output
51 changes: 51 additions & 0 deletions asciiWidgets/NeedBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/python
#*** encoding:utf-8 ***

# This file is part of La Vida
# Copyright (C) 2011 Florian R. A. Angermeier
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

from AsciiWidget import AsciiWidget

class NeedBar( AsciiWidget ):
def __init__( self, pos, label, character, need ):
self.cmdPrefix = '\033['
self.cmdSeperator = ';'
self.cmdPostfixPosition = 'H'
self.cmdPostfixColor = 'm'
self.cmdTab = '\t'
self.pos = pos
self.label = label
self.character = character
self.need = need

def printWidget( self ):
cmdPosition = '%s%i%s%i%s' % ( self.cmdPrefix, self.pos[0], self.cmdSeperator, self.pos[1], self.cmdPostfixPosition )

cmdColor = '%s40%s' % ( self.cmdPrefix, self.cmdPostfixColor )
output = '%s%s%s ' % ( cmdPosition, self.label, cmdColor )
cmdColor = '%s0%s' % ( self.cmdPrefix, self.cmdPostfixColor )
output += cmdColor
print '%s' % ( output )

if( self.character.needs[ self.need ] >= 750 ):
cmdColor = '%s42%s' % ( self.cmdPrefix, self.cmdPostfixColor )
elif( self.character.needs[ self.need ] >= 500 ):
cmdColor = '%s43%s' % ( self.cmdPrefix, self.cmdPostfixColor )
else:
cmdColor = '%s41%s' % ( self.cmdPrefix, self.cmdPostfixColor )
output = '%s%s%s' % ( cmdPosition, self.label, cmdColor )
for i in range( int( self.character.needs[ self.need ] / 100 ) ):
output += ' '
cmdColor = '%s0%s' % ( self.cmdPrefix, self.cmdPostfixColor )
output += '%s' % ( cmdColor )
print '%s' % ( output )
Empty file added asciiWidgets/__init__.py
Empty file.
16 changes: 8 additions & 8 deletions gameEngine/Character.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Character:
def __init__( self, id, world, gender ):
self.world = world # The world a character belongs to.
self.id = id # Unique integer number to identify a character.
self.pos = [ 0, 0, 0 ] # A characters current position.
self.destination = [ 0, 0, 0 ] # A characters destination for movement.
self.pos = [ 1, 1, 0 ] # A characters current position.
self.destination = [ 1, 1, 0 ] # A characters destination for movement.
self.radii = { 'talk': 1 }

if( gender.lower() == 'random' ): # A characters gender.
Expand All @@ -43,31 +43,31 @@ def __init__( self, id, world, gender ):
'social': 500 } #

def teleportTo( self, x, y, z ):
print 'DEBUG: Character %i teleports to X: %i Y: %i Z: %i.' % ( self.id, x, y, z )
# print 'DEBUG: Character %i teleports to X: %i Y: %i Z: %i.' % ( self.id, x, y, z )
self.pos = [ x, y, z ]

def goTo( self, x, y, z ):
print 'DEBUG: Character %i goes to X: Y: %i %i Z: %i.' % ( self.id, x, y, z )
# print 'DEBUG: Character %i goes to X: Y: %i %i Z: %i.' % ( self.id, x, y, z )
self.destination = [ x, y, z ]

def move( self ):
# X axis
if( self.pos[ 0 ] != self.destination[ 0 ] ):
print 'DEBUG: Character %i is on his/her way on the x axis.'
# print 'DEBUG: Character %i is on his/her way on the x axis.'
if( self.pos[ 0 ] < self.destination[ 0 ] ):
self.pos[ 0 ] += 1
elif( self.pos[ 0 ] > self.destination[ 0 ] ):
self.pos[ 0 ] -= 1
# Y axis
if( self.pos[ 1 ] != self.destination[ 1 ] ):
print 'DEBUG: Character %i is on his/her way on the y axis.'
# print 'DEBUG: Character %i is on his/her way on the y axis.'
if( self.pos[ 1 ] < self.destination [ 1 ] ):
self.pos[ 1 ] += 1
elif( self.pos[ 1 ] > self.destination[ 1 ] ):
self.pos[ 1 ] -= 1
# Z axis
if( self.pos[ 2 ] != self.destination[ 2 ] ):
print 'DEBUG: Character %i is on his/her way on the z axis.'
# print 'DEBUG: Character %i is on his/her way on the z axis.'
if( self.pos[ 2 ] < self.destination [ 2 ] ):
self.pos[ 2 ] += 1
elif( self.pos[ 2 ] > self.destination[ 2 ] ):
Expand Down Expand Up @@ -96,7 +96,7 @@ def decreaseNeeds( self ):
self.needs['hygiene'] -= 0.005787037
if( self.brain.activity != 'watch' ):
self.needs['fun'] -= 0.034722222
if( self.brain.activity != 'chat' ):
if( self.brain.activity != 'talk' ):
self.needs['social'] -= 0.011574074
except:
pass
Expand Down
Loading

0 comments on commit 896e46d

Please sign in to comment.