Skip to content

Commit

Permalink
* Implemented rolling.
Browse files Browse the repository at this point in the history
   ... a little buggy when transitioning from uphill to downhill.
   Angle detection bug really shows in rolling.
* Began state machine.
* Brought debug text onto screen again.
  • Loading branch information
DMAshura committed Apr 28, 2011
1 parent 3388702 commit 1e3961f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
62 changes: 51 additions & 11 deletions character.py
Expand Up @@ -18,6 +18,10 @@ def __init__(self, game, res):
self.x = x
self.y = y

# State Machine

self.state = "Stopped"

# Sensors

self.sensor_bottom = Sensor(self.res)
Expand All @@ -41,9 +45,12 @@ def __init__(self, game, res):

self.acc = 0.046875 * const.SCALE
self.frc = 0.046875 * const.SCALE
self.rfrc = 0.0234375 * const.SCALE
self.dec = 0.5 * const.SCALE
self.max = 6.0 * const.SCALE

self.rol = 1.03125 * const.SCALE

self.slp = 0.125 * const.SCALE
self.ruslp = 0.078125 * const.SCALE
self.rdslp = 0.3125 * const.SCALE
Expand Down Expand Up @@ -87,6 +94,8 @@ def __init__(self, game, res):
# Control lock timers
self.hlock = 0

def play_sound(self, string):
self.res.sound_dict[string].play()

def release_jump(self):
self.dv = min(self.dv, self.jmpweak)
Expand All @@ -101,63 +110,93 @@ def set_position(self, x = None, y = None):
self.update_sensors()

def handle_physics(self):
# See if rolling
if (abs(self.dh) >= self.rol):
if (self.keyDown and self.flagGround and self.state != "Rolling"):
self.state = "Rolling"
self.play_sound('spin.wav')
print "Rolling!"

# Slope factor
'''if not self.Rolling:'''
if self.flagGround:
self.dh -= self.slp * sin(self.rangle)
'''
if self.state != "Rolling":
if self.flagGround:
self.dh -= self.slp * sin(self.rangle)
else:
if cmp(self.dh,0) == cmp(sin(self.rangle)):
if cmp(self.dh,0) == cmp(sin(self.rangle),0):
self.dh -= self.ruslp * sin(self.rangle)
elif cmp(self.dh,0) == -cmp(sin(self.rangle)):
elif cmp(self.dh,0) == -cmp(sin(self.rangle),0) or self.dh == 0:
self.dh -= self.rdslp * sin(self.rangle)
'''

# Falling off walls and ceilings
if 45 < self.rangle < 315 and abs(self.dh) < 2.5 * const.SCALE:
self.set_gravity(self.gangle)
self.flagFellOffWallOrCeiling = True

if self.state == "Rolling" and self.dh == 0 and not self.keyDown:
self.state = "Stopped"
print "Stopped!"

# Speed input and management
if self.flagAllowHorizMovement:
if self.keyLeft:
if self.dh > 0 and self.flagGround:
# Decelerate
self.dh -= self.dec
if -self.dec < self.dh < 0:
self.dh = -self.dec
elif self.dh > -self.max:
elif self.dh > -self.max and self.state != "Rolling":
# Accelerate
if self.flagGround:
self.dh = max(self.dh - self.acc, -self.max)
else:
self.dh = max(self.dh - self.air, -self.max)
elif self.keyRight:
if self.dh < 0 and self.flagGround:
# Decelerate
self.dh += self.dec
if 0 < self.dh < self.dec:
self.dh = self.dec
elif self.dh < self.max:
elif self.dh < self.max and self.state != "Rolling":
# Accelerate
if self.flagGround:
self.dh = min(self.dh + self.acc, self.max)
else:
self.dh = min(self.dh + self.air, self.max)
elif not self.keyLeft and not self.keyRight and self.flagGround:
self.dh -= min(abs(self.dh), self.frc) * cmp(self.dh,0)
if self.state == "Rolling":
self.dh -= min(abs(self.dh), self.rfrc) * cmp(self.dh,0)
else:
self.dh -= min(abs(self.dh), self.frc) * cmp(self.dh,0)

#Air Drag
if 0 < self.dv < 4*const.SCALE and abs(self.dh) >= 0.125:
self.dh *= self.drg

#Jumping
if self.flagJumpNextFrame:
self.res.sound_dict['jump.wav'].play()
self.play_sound('jump.wav')
self.dv = self.jmp
self.set_gravity(self.gangle)
self.state = "Jumping"
self.flagGround = False
self.flagAllowJump = False
self.flagJumpNextFrame = False
if self.keyJump and self.flagGround and self.flagAllowJump:
self.flagJumpNextFrame = True

def calculate_state(self):
if self.flagGround:
if self.state != "Rolling":
if self.dh == 0:
self.state = "Stopped"
elif 0 < abs(self.dh) < self.max:
self.state = "Walking"
elif abs(self.dh) >= self.max:
self.state = "Running"
else:
if self.state != "Jumping":
self.state = "Falling"

def set_gravity(self, gangle):
_ = rotate_basis(self.dh, self.dv, self.angle, gangle)
self.dh = _[0]
Expand Down Expand Up @@ -409,6 +448,7 @@ def update(self, dt):
self.perform_speed_movement(dt)
if not self.flagGround or not self.perform_ground_test():
self.perform_gravity_movement(dt)
self.calculate_state()
if self.flagGround:
self.angle = self.calculate_angle()
self.calculate_rangle()
Expand Down
Binary file added gamedata/sounds/spin.wav
Binary file not shown.
14 changes: 7 additions & 7 deletions porcupyne.py
Expand Up @@ -125,13 +125,13 @@ def __init__(self):
SlantPlatform(328, 96, res)
]
self.controller = Controller(self.window, self.player1)
self.fps_display = font.Text(pyglet.font.load('', 36, bold = True), '',
color=(0.5, 0.5, 0.5, 0.5), x=-300, y=-200)
self.fps_display = font.Text(pyglet.font.load('', 18, bold = True), '',
color=(0.5, 0.5, 0.5, 0.5), x=-150, y=-100)

ft = font.load('Arial', 20)
self.debug_text = [font.Text(ft, x=200, y=200),
font.Text(ft, x=200, y=170),
font.Text(ft, x=200, y=140)]
ft = font.load('Arial', 10)
self.debug_text = [font.Text(ft, x=-150, y=100),
font.Text(ft, x=-150, y=85),
font.Text(ft, x=-150, y=70)]

# alpha channels¨
rabbyt.set_default_attribs()
Expand Down Expand Up @@ -197,7 +197,7 @@ def on_draw(self, dt):
self.fps_display.draw()

self.debug_text[0].text = str(int(self.player1.hlock))
self.debug_text[1].text = str(0)
self.debug_text[1].text = str(self.player1.state)
self.debug_text[2].text = str(self.player1.rangle)
self.debug_text[0].draw()
self.debug_text[1].draw()
Expand Down

0 comments on commit 1e3961f

Please sign in to comment.