Skip to content

Commit

Permalink
Build stuff at build sites! - #5
Browse files Browse the repository at this point in the history
Seperate construction and spawning of entities
Move build sites to map
  • Loading branch information
v4nz666 committed Mar 10, 2016
1 parent c4a022e commit 0eb8eed
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 48 deletions.
15 changes: 7 additions & 8 deletions RoguePy/Game/Entity.py
Expand Up @@ -2,19 +2,18 @@


class Entity:
def __init__(self, map, x, y, name, ch, fg):
c = map.getCell(x, y)
if ( c.entity != None ):
raise Exception("Entity already present in map cell (%d,%d)" % (x, y))
c.entity = self
self.map = map
self.x = x
self.y = y
def __init__(self, name, ch, fg):
self.name = name
self.ch = ch
self.fg = fg
self.item = None

def spawn(self, map, x, y):
self.map = map
self.x = x
self.y = y
return map.addEntity(self, x, y)

def pickup(self, item):
if not self.item:
self.item = item
Expand Down
20 changes: 19 additions & 1 deletion RoguePy/Game/Map.py
@@ -1,10 +1,11 @@
from .. import UI
from RoguePy.libtcod import libtcod
from RoguePy.UI import Colors
import chars

import config



class Map:
def __init__(self, w, h, cells=None):
self.width = w
Expand All @@ -15,6 +16,23 @@ def __init__(self, w, h, cells=None):
self.cells = cells
self.listeners = {}

self.buildSites = {}

def addBuildSite(self, x, y, b):
if not (x, y) in self.buildSites:
self.buildSites[(x, y)] = b
def removeBuildSite(self, x, y):
if (x,y) in self.buildSites:
del self.buildSites[(x,y)]

def purgeBuildSites(self):
rm = []
for x, y in self.buildSites:
if self.buildSites[x,y].timer <= 0:
rm.append((x,y))
for x, y in rm:
self.removeBuildSite(x, y)

@staticmethod
def FromFile(path):
lines = open(path).read().splitlines()
Expand Down
6 changes: 6 additions & 0 deletions buildsite.py
@@ -0,0 +1,6 @@
__author__ = 'jripley'

class BuildSite():
def __init__(self, timer, entity):
self.timer = timer
self.entity = entity
3 changes: 0 additions & 3 deletions entities/shroom.py
Expand Up @@ -10,9 +10,6 @@ class Shroom(Node):
def activate(self, player):
self.active = True
self.player = player



self.initNetwork()

def initNetwork(self):
Expand Down
4 changes: 3 additions & 1 deletion states/generatestate.py
Expand Up @@ -193,7 +193,9 @@ def spawnShroom(self):
y = minY + config.randint(maxY)

if self.map.getCell(x, y).type == 'grass':
self.map.shroom = Shroom(self.map, x, y, 'Shroom', chars.shroom, Colors.white)
shroom = Shroom('Shroom', chars.shroom, Colors.white)
shroom.spawn(self.map, x, y)
self.map.shroom = shroom
else:
self.spawnShroom()

Expand Down
62 changes: 39 additions & 23 deletions states/playstate.py
@@ -1,7 +1,8 @@
import RoguePy
from RoguePy.Game.Item import Item
from buildsite import BuildSite
import chars
from entities import Shroom
from entities import Shroom, Node
from entities import Construct
import items
import uielements
Expand All @@ -24,9 +25,9 @@ def init(self):

def setupHandlers(self):

self.addHandler('buildAnim', 12, self.mapElement.updateBuildChar)
self.addHandler('buildAnim', 12, self.mapElement.updateBuildAnimation)
self.addHandler('hudRefresh', 1, self.hudRefresh)
self.turnHandlers = []
self.turnHandlers = [self.buildSiteUpdate]

def setupView(self):

Expand Down Expand Up @@ -161,7 +162,7 @@ def movePlayer(self,dx,dy):
# Item use callbacks
def setupItems(self):
def useSpore(x, y):
self.mapElement.addBuildSite(x, y)
self.map.addBuildSite(x, y, BuildSite(5, Node("Node", chars.shroom, Colors.white)))
print "BAM Spore deployed"
items.spore.use = useSpore

Expand Down Expand Up @@ -201,7 +202,10 @@ def spawnPlayer(self):
try:
if self.map.getCell(_x, _y).type == 'grass':
print "Player: %d, %d" % (_x, _y)
return RoguePy.Game.Entity(self.map, _x, _y, 'Sporaculous', '@', Colors.white)
player = RoguePy.Game.Entity('Sporaculous', '@', Colors.white)
player.spawn(self.map, _x, _y)
return player

except IndexError:
pass
print "Player not placed!"
Expand Down Expand Up @@ -254,24 +258,6 @@ def setupWaves(self):
self.initNextWave(first=True)
self.turnHandlers.append(self.waveUpdate)

def waveUpdate(self):
wave = self.waves[0]
if wave.timer > 0:
wave.timer -= 1
return
else:
self.activateWave()

if len(wave.enemies):
pass #updateEnemies
else:
self.messageList.message("wave complete")
self.initNextWave()

if len(self.waves) == 0:
self.messageList.message("you win!!!!")
self.turnHandlers.remove(self.waveUpdate)

# The enemies have arrived
def activateWave(self):
self.waves[0].active = True
Expand Down Expand Up @@ -317,11 +303,41 @@ def updateNetFrame(self):
self.netFrame.setDirty()


##############################
# Turn handlers

# One time only, then remove
def waveUpdate(self):
wave = self.waves[0]
if wave.timer > 0:
wave.timer -= 1
return
else:
self.activateWave()

if len(wave.enemies):
pass #updateEnemies
else:
self.messageList.message("wave complete")
self.initNextWave()

if len(self.waves) == 0:
self.messageList.message("you win!!!!")
self.turnHandlers.remove(self.waveUpdate)

def collectMana(self):
cells = self.map.shroom.netSize
self.mana += round(cells * cfg.manaRate, 2)
self.updateNetFrame()

def buildSiteUpdate(self):
for x, y in self.map.buildSites:
site = self.map.buildSites[(x,y)]
site.timer -= 1
if not site.timer:
site.entity.spawn(self.map, x, y)
self.map.purgeBuildSites()


class Wave():
def __init__(self, timer, items, enemies):
Expand Down
15 changes: 3 additions & 12 deletions uielements/gamemap.py
Expand Up @@ -15,28 +15,19 @@ class GameMap(Elements.Map):
def __init__(self, x, y, w, h, map):
super(GameMap, self).__init__(x, y, w, h, map)
self.player = None
self.buildSites = []


def setPlayer(self, player):
self.player = player
self._initFovMap()
self.calculateFovMap()

def addBuildSite(self, x, y):
if not (x, y) in self.buildSites:
self.buildSites.append((x, y))
def removeBuildSite(self, x, y):
if (x,y) in self.buildSites:
self.buildSites.remove((x,y))

def updateBuildChar(self):
def updateBuildAnimation(self):
self.buildCharIndex += 1
if self.buildCharIndex >= len(self.buildChars):
self.buildCharIndex = 0

onScreen = False
for (x, y) in self.buildSites:
for (x, y) in self.map.buildSites:
if not self.onScreen(x, y):
continue
onScreen = True
Expand Down Expand Up @@ -68,7 +59,7 @@ def draw(self):

def cellToView(self, c, x, y):
result = super(GameMap, self).cellToView(c)
if (x, y) in self.buildSites:
if (x, y) in self.map.buildSites and not (c.item or c.entity):
result = CellView(self.buildChars[self.buildCharIndex], result.fg, result.bg)
return result

Expand Down

0 comments on commit 0eb8eed

Please sign in to comment.