Overflown/gamedata/objects/levels.py
2021-11-28 11:33:00 +01:00

162 lines
6.7 KiB
Python

from gamedata.objects.base import BaseObject
from gamedata.objects.transition import Transition
class Levels(BaseObject):
def __init__(self,x,y,game,nodes,customvalues):
super().__init__(0,0,game)
self.nodes = []
for i in nodes:
self.nodes.append(i.copy())
self.customvalues = customvalues
self.game.globals["nblevels"] = len(self.nodes)
self.bgm = self.game.sound_lib["bgm/overworld.ogg"]
self.bgm.play(-1)
self.sfx = self.game.sound_lib["sfx/mapmove.wav"]
self.launchsfx = self.game.sound_lib["sfx/select.wav"]
self.launched = False
self.blueflags = self.game.getSpriteDir("flags/blue/")
self.greenflags= self.game.getSpriteDir("flags/green/")
self.flagbase = self.game.sprite_lib["flags/base.png"]
self.playerwalking = self.game.getSpriteDir("player/walking/")
self.playerstill = self.game.sprite_lib["player/still.png"]
self.medalwave = self.game.sprite_lib["medals/wave.png"]
self.medalwaveoff = self.game.sprite_lib["medals/waveoff.png"]
self.medalclock = self.game.sprite_lib["medals/clock.png"]
self.medalclockoff = self.game.sprite_lib["medals/clockoff.png"]
self.flip = False
self.moving = False
self.spriteindex = 0
self.flagsindex = []
self.names = []
self.textnumbers = []
for i,v in enumerate(self.nodes):
v["x"]+=8 # Center on tiles
v["y"]+=8
self.flagsindex.append(self.game.lib.randint(1,3))
# Get sprites for "Level N"
sprite = self.game.getchars("Level "+str(i+1))
self.textnumbers.append(sprite)
# Get sprites for the level's name
if str(i) in customvalues.keys():
sprites = self.game.getchars(customvalues[str(i)].split(";")[0])
self.names.append(sprites)
else:
self.names.append(None)
self.animspeed = 7
self.cursor = 0
if self.game.globals["levelname"]!=None:
self.cursor = self.game.globals["levelname"] # Which level is selected
self.playerx = self.nodes[self.cursor]["x"]
self.playery = self.nodes[self.cursor]["y"]
self.tileset = self.game.gameloop.findname("TilesetRenderer")[0]
self.maxwidth = self.tileset.layers[0]["surface"].get_width()
self.movecamera()
def step(self):
for i in range(len(self.flagsindex)):
self.flagsindex[i]+=self.animspeed*self.game.dt
if not self.moving and not self.launched:
if self.game.inputs["keys"]["right"]["timer"]>0:
if "Level "+str(self.cursor+1) in self.game.globals["finishedlevels"] or self.game.globals["allunlocked"]:
self.cursor+=1
self.sfx.play()
self.flip = False
if self.game.inputs["keys"]["left"]["timer"]>0:
self.cursor-=1
self.flip = True
self.sfx.play()
if self.game.inputs["keys"]["up"]["timer"]==1:
# Launch the level
t = Transition(self.game,level = "Level "+str(self.cursor+1))
self.launched = True
self.launchsfx.play()
self.game.globals["levelname"] = self.cursor
if str(self.cursor) in self.customvalues.keys():
props = self.customvalues[str(self.cursor)].split(";")
self.game.globals["levellore"] = props[0]
self.game.globals["timetobeat"] = props[1]
else:
self.game.globals["levellore"] = None
self.game.gameloop.summon(t)
self.bgm.stop()
self.cursor = min(self.cursor,len(self.nodes)-1)
self.cursor = max(0,self.cursor)
speedx = (self.nodes[self.cursor]["x"]-self.playerx)
speedy = (self.nodes[self.cursor]["y"]-self.playery)
self.moving = (abs(speedx)+1)*(abs(speedy)+1)>3
if self.moving:
self.spriteindex += self.animspeed*self.game.dt
else:
self.spriteindex = 0
self.playerx += speedx*4*self.game.dt
self.playery += speedy*4*self.game.dt
self.movecamera()
def movecamera(self):
self.game.globals["camerax"] = self.playerx-self.game.globals["cameraw"]/2
self.game.globals["camerax"] = max(0,self.game.globals["camerax"])
self.game.globals["camerax"] = min(self.maxwidth-self.game.globals["cameraw"],self.game.globals["camerax"])
def draw(self):
for i,v in enumerate(self.nodes): # Draw flags
sprites = self.blueflags
if i in self.game.globals["finishedlevels"]:
sprites = self.greenflags
sprite = sprites[int(self.flagsindex[i])%len(sprites)]
x = v["x"]-sprite.get_width()/2-self.game.globals["camerax"]+8
y = v["y"]-sprite.get_height()-self.game.globals["cameray"]
self.game.window.blit(self.flagbase,[v["x"]-self.game.globals["camerax"]-8,v["y"]-self.game.globals["cameray"]-2])
self.game.window.blit(sprite,[x,y])
# Draw player
if self.moving:
sprite = self.playerwalking[int(self.spriteindex)%len(self.playerwalking)]
else:
sprite = self.playerstill
sprite = self.game.pygame.transform.flip(sprite,self.flip,False)
x = self.playerx-self.game.globals["camerax"]-sprite.get_width()/2
y = self.playery-self.game.globals["cameray"]-sprite.get_height()/2-6
self.game.window.blit(sprite,[x,y])
# Draw level's number
sprite = self.textnumbers[self.cursor]
x = self.nodes[self.cursor]["x"]-self.game.globals["camerax"]-sprite.get_width()/2
y = self.nodes[self.cursor]["y"]-self.game.globals["cameray"]-sprite.get_height()/2-50
self.game.window.blit(sprite,[x,y])
# Medals
wave = self.medalwaveoff
if self.cursor in self.game.globals["completedlevels"]:
wave = self.medalwave
clock = self.medalclockoff
if self.cursor in self.game.globals["speedrunlevels"]:
clock = self.medalclock
x = self.nodes[self.cursor]["x"]-self.game.globals["camerax"]
y = self.nodes[self.cursor]["y"]-72
self.game.window.blit(wave,[x-wave.get_width()-1,y])
self.game.window.blit(clock,[x+1,y])
# Draw the level's name
if self.names[self.cursor]:
sprite = self.names[self.cursor]
x = self.nodes[self.cursor]["x"]-self.game.globals["camerax"]-sprite.get_width()/2
y = self.nodes[self.cursor]["y"]-self.game.globals["cameray"]-sprite.get_height()/2-40
self.game.window.blit(sprite,[x,y])