On Tue, 2012-04-10 at 23:20 -0300, claudio canepa wrote:
> On Tue, Apr 10, 2012 at 5:51 PM, Ivo F.A.C. Fokkema
> <[email protected]> wrote:
> If somebody wants to see a working example I would probably
> need to
> clean up the code a bit and isolate the needed lines of code
> from the
> rest and put it into one test file, let me know if anyone is
> interested.
>
>
>
> I want !
Alright, see attached! It's running in the test directory. It's not
super clean code, and I actually still have a hack there to make the
lifebar disappear when the unit dies, but oh well... you get the idea.
Ivo
--
You received this message because you are subscribed to the Google Groups
"cocos2d discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cocos-discuss?hl=en.
# This code is so you can run the samples without installing the package
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import cocos
from cocos.director import director
from cocos.actions import *
class LifeBar (cocos.layer.ColorLayer):
# Based on code (c) Claudio Canepa
def __init__(self, r, g, b, a, max_value, width=100, height=2, initial_value=None):
super(LifeBar, self).__init__(r, g, b, a, width, height)
self.max_value = max_value
self.widget_width = width
if initial_value == None:
initial_value = max_value
self.initial_value = initial_value
def updateWidth (self, value):
self.width = int(float(value) / self.max_value * self.widget_width)
self.regen_vertexes()
def on_enter (self):
super(LifeBar, self).on_enter()
self.updateWidth(self.initial_value)
def regen_vertexes (self):
x, y = self.width, self.height
ox, oy = 0, 0
self._vertex_list.vertices[:] = [
ox, oy,
ox, oy + y,
ox+x, oy+y,
ox+x, oy]
class Unit (cocos.sprite.Sprite):
def __init__(self, sImage):
super(Unit, self).__init__(sImage)
self.nLife = 100 # NORMAL, FULL life.
self.nHitPoints = self.nLife # CURRENT life.
# Now create a life bar.
nBarWidth = int(self.height * 0.9) # Maximum width, the actual width is taken care of by the meter itself.
# Black background:
self.oBarBackground = cocos.layer.ColorLayer(0, 0, 0, 255, width=nBarWidth + 2, height=4)
self.oBarBackground.position = (-self.oBarBackground.width/2, self.height/2)
self.add(self.oBarBackground, z=2)
# Actual bar, will get shorter as life decreases.
self.oBar = LifeBar(255, 0, 0, 255, self.nLife, width=nBarWidth, height=2, initial_value=self.nHitPoints)
self.oBar.position = (1, 1)
self.oBarBackground.add(self.oBar, z=2)
self.oBarText = cocos.text.Label('%d / %d' % (self.nHitPoints, self.nLife), font_name='Verdana', font_size=6, color=(255, 255, 255, 150), anchor_x='left', anchor_y='bottom')
self.oBarText.position = (self.oBar.x, self.oBar.y+1)
self.oBar.add(self.oBarText, z=2)
def on_enter (self):
super(Unit, self).on_enter()
self.updateBar()
def updateBar (self):
if self.nHitPoints < 0:
self.nHitPoints = 0
nDiv = (float(self.nHitPoints)/self.nLife)
if nDiv >= 0.5:
nGreen = 255
nRed = int((1 - nDiv) * 2 * 255)
else:
nGreen = int(nDiv * 2 * 255)
nRed = 255
# Now draw it.
self.oBar.color = (nRed, nGreen, 0)
self.oBarText.element.text = '%d / %d' % (self.nHitPoints, self.nLife)
# And change width.
self.oBar.updateWidth(self.nHitPoints)
def hit (self, nDamage):
nDamage *= 10
if self.nHitPoints > 0:
self.nHitPoints -= nDamage
if self.nHitPoints <= 0:
self.nHitPoints = 0
self.updateBar()
if not self.nHitPoints:
self.die()
def die (self):
#self.oBar.kill() # AttributeError: 'ColorLayer' object has no attribute 'set_batch'
#self.remove(self.oBar) # AttributeError: 'ColorLayer' object has no attribute 'set_batch'
self.oBarBackground._set_opacity(0) # Stupid hack.
self.oBar._set_opacity(0)
self.oBarText._set_opacity(0)
self.do(FadeOut(5) + CallFunc(self.kill))
class TestLayer(cocos.layer.ColorLayer):
def __init__(self):
super(TestLayer, self).__init__(100, 100, 100, 100)
self.unit = Unit("grossini.png")
self.unit.position = 320, 240
self.add(self.unit)
self.schedule(self.unit.hit)
description = """
An implementation of a life bar, that adapts size and color depending on the current life status.
"""
def main():
print description
director.init()
test_layer = TestLayer()
main_scene = cocos.scene.Scene (test_layer)
director.run (main_scene)
if __name__ == '__main__':
main()