diff --git a/camera.py b/camera.py index 9536d1d..07e4793 100644 --- a/camera.py +++ b/camera.py @@ -1,11 +1,10 @@ from bge import logic -scene = logic.getCurrentScene() -player = scene.objects["player"] +dict = logic.globalDict +player = dict['player'] def main(controller): own = controller.owner - own.worldPosition.x += (player.worldPosition.x - own.worldPosition.x) * .1 own.worldPosition.y += (player.worldPosition.y - own.worldPosition.y) * .1 \ No newline at end of file diff --git a/laser.py b/laser.py new file mode 100644 index 0000000..b789fb0 --- /dev/null +++ b/laser.py @@ -0,0 +1,15 @@ +from main import Projectile +from bge import logic +import random +import aud + +laser_sounds = [aud.Factory.file(logic.expandPath("//sound/laser/laserfire01_mono.wav")).volume(.5).fadeout(0, .4), aud.Factory.file(logic.expandPath("//sound/laser/laserfire02_mono.wav")).volume(.5).fadeout(0, .4)] + +def main(cont): + own = cont.owner + + if not "init" in own: + own["init"] = True + own = Projectile(own, sound=random.choice(laser_sounds)) + + own.main() \ No newline at end of file diff --git a/level_end.py b/level_end.py new file mode 100644 index 0000000..ba837f2 --- /dev/null +++ b/level_end.py @@ -0,0 +1,12 @@ +import bge +from main import nextlevel +player = bge.logic.globalDict['player'] + +def check_collision(object): + if object == player: + nextlevel() + +def main(cont): + own = cont.owner + + own.collisionCallbacks.append(check_collision) \ No newline at end of file diff --git a/light_trail.py b/light_trail.py index 359d2a4..d619454 100644 --- a/light_trail.py +++ b/light_trail.py @@ -3,6 +3,16 @@ import mathutils import utils + + +class LightTrail(types.KX_GameObject): + + def __init__(self, own): + self.length = 20 + self.resolution = 20 + + + tail_length=20 past_locations = collections.deque(maxlen=20) for i in range(tail_length): diff --git a/lonely_photon.blend b/lonely_photon.blend index 09df45a..ca8dc37 100644 Binary files a/lonely_photon.blend and b/lonely_photon.blend differ diff --git a/main.py b/main.py index 1f69203..04d072f 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,34 @@ -from bge import logic, types +from bge import logic, events, types, render +import math, mathutils +import numpy import aud import random import utils +#sound = aud.Factory.file(logic.expandPath("//sound/Adventure Meme.mp3")).volume(.1).loop(-1) +#sound = aud.Factory.file(logic.expandPath("//sound/Unwritten Return.mp3")).volume(1).loop(-1) +sound = aud.Factory.file(logic.expandPath("//sound/Destiny Day.mp3")).volume(1).loop(-1) +music = aud.device().play(sound) + +dict = logic.globalDict +scene = logic.getCurrentScene() +print(logic.getSceneList()) +for s in logic.getSceneList(): + print(s.name) + if s.name == "level1": + scene = s + if s.name == "props": + prop_scene = s + +levels = ['level1', 'level2'] +dict["current_level"] = 0 + class Sentry(types.KX_GameObject): def __init__(self, own): self.cont = self.controllers[0] -# scene = logic.getCurrentScene() - self.target = self.scene.objects["player"] + self.target = dict['player'] self.fireRate = .1 self.range = 8 @@ -21,7 +40,7 @@ def __init__(self, own): print(self.name, "using", self.projectile_type) - self.firenow = 0 + self.firenow = random.random() self.mparts = [] @@ -35,7 +54,7 @@ def __init__(self, own): def aim(self): - + hit = self.rayCast(self.target, self, 0.0, "solid", 0, 1, 0) if hit[0] == self.target: self["cansee"] = True @@ -49,7 +68,7 @@ def aim(self): def fire(self): - projectile = self.scene.addObject(self.projectile_type, self.firepoint, 0) + projectile = scene.addObject(self.projectile_type, self.firepoint, 0) projectile.worldOrientation = self.worldOrientation for part in self.mparts: @@ -71,7 +90,6 @@ def main(self): class Projectile(types.KX_GameObject): - def __init__(self, own, sound): self.sound = sound @@ -79,22 +97,179 @@ def __init__(self, own, sound): self.speed = -30 self.homing_factor = 0 - self.light_source = "standard_projectile_lamp" + self.collisionCallbacks.append(self.on_collision) + + self.sound_device = aud.device() + self.sound_device.distance_model = aud.AUD_DISTANCE_MODEL_LINEAR + self.sound_device.listener_location = dict['player'].worldPosition + self.sound_device.listener_velocity = dict['player'].getLinearVelocity() + self.sound_device.doppler_factor = 3 -# self.light = self.scene.addObject(self.light_source) + self.sound_handle = self.sound_device.play(self.sound) + self.sound_handle.relative = False + self.sound_handle.location = self.worldPosition + self.sound_handle.velocity = self.getLinearVelocity() + self.sound_handle.distance_maximum = 100 + self.sound_handle.distance_reference = 1 - self.sound_handler = aud.device().play(self.sound) -# self.setLinearVelocity((self.speed, 0, 0), True) + + def on_collision(self, object, point, normal): + + if "solid" in object: + + # Flip sign of normal if collision with triangle mesh + # to workaround this bug: https://developer.blender.org/T47036 + if "floor_triangle_mesh" in object.name: + normal = normal*-1 + + d = mathutils.Vector(self.getLinearVelocity()) + n = mathutils.Vector(normal) + +# d = d * d.dot(n) + + # determine reflection direction + r = d - ((2 * d * n) / (n*n)) * n + + vectsplosion = scene.addObject("vectsplosion") + vectsplosion["point"] = point + vectsplosion["direction"] = r + vectsplosion["color"] = (1, 0, 0) + + if object == dict['player']: + player_death() + else: + self.endObject() + def main(self): - + self.setLinearVelocity((self.speed, 0, 0), True) + + if self.sound_handle: + self.sound_handle.pitch = utils.map_range(logic.getTimeScale() + utils.map_range(random.random(), to_min=-.1, to_max=.1), .05, 1, .2, 1) + +class Vectsplosion(types.KX_GameObject): + + def __init__(self, own): + + self.origin = mathutils.Vector(self["point"]) + self.direction = mathutils.Vector(self["direction"]) + self.rcolor = self["color"] + + self.time = 0 + self.lines = [] + + for i in range(random.randrange(3, 5)): + self.lines.append(self.origin + self.direction + mathutils.Vector(numpy.random.normal(0, 5, 3))) + + def main(self): - #self.light.worldPosition = self.worldPosition + for line in self.lines: + render.drawLine(self.origin.lerp(line, utils.clamp(self.time*2-.5)), self.origin.lerp(line, self.time), self.rcolor) + + self.time += .1*logic.getTimeScale() - print(utils.map_range(logic.getTimeScale() + utils.map_range(random.random(), to_min=-.1, to_max=.1), .05, 1, .2, 1)) - if self.sound_handler: + if self.time >= 1: + self.endObject() + - self.sound_handler.pitch = utils.map_range(logic.getTimeScale() + utils.map_range(random.random(), to_min=-.1, to_max=.1), .05, 1, .2, 1) - print(self.sound_handler.pitch) - \ No newline at end of file +class Player(types.KX_GameObject): + + keyboard = logic.keyboard + + def __init__(self, own): + + self.alive = True + self.recouperating = False + self.movement_speed = 20 + self.light = scene.objects["player_light"] + + self.counter = 1 + + + def movement(self, keyboard=keyboard): + ACTIVE = logic.KX_INPUT_ACTIVE + + movement = self.getLinearVelocity() + + # forward + if keyboard.events[events.WKEY] == ACTIVE: + movement[1] = utils.clamp(movement[1] + self.movement_speed*.1, -self.movement_speed, self.movement_speed) + + # backward + if keyboard.events[events.SKEY] == ACTIVE: + movement[1] = utils.clamp(movement[1] + -self.movement_speed*.1, -self.movement_speed, self.movement_speed) + + # left + if keyboard.events[events.AKEY] == ACTIVE: + movement[0] = utils.clamp(movement[0] + -self.movement_speed*.1, -self.movement_speed, self.movement_speed) + + # right + if keyboard.events[events.DKEY] == ACTIVE: + movement[0] = utils.clamp(movement[0] + self.movement_speed*.1, -self.movement_speed, self.movement_speed) + + self.setLinearVelocity(movement) + + # cosmetic spinny stuff + if self.getAngularVelocity() < mathutils.Vector((1,1,1)): + self.setAngularVelocity(((random.random()*5)-2.5, (random.random()*5)-2.5, (random.random()*5)-2.5)) + + def main(self): + + if self.alive: + velocity = self.getLinearVelocity() + speed = utils.velocity2speed(velocity) + timescale = utils.clamp(speed/self.movement_speed, .05, 1) + + if timescale != logic.getTimeScale(): + logic.setTimeScale(timescale) + + self.light.energy = (math.sin(logic.getRealTime())*.3) + .7 + + self.movement() + else: + + self.light.energy = self.light.energy * utils.clamp(float(self.counter), .5, 1) + music.pitch = utils.clamp(self.counter) + + self.counter -= .01 + + if self.alive == False and self.counter < 0: + logic.sendMessage("player_death") + + self.alive = True + self.recouperating = True + + if self.recouperating and self.alive == True: + + self.light.energy = ((math.sin(logic.getRealTime())*.3) + .7) * self.counter + music.pitch = utils.clamp(self.counter) + + self.counter += .05 + + if self.counter >= 1: + self.recouperating = False + music.pitch = 1 + + + + #light_trail.main(cont) + +def update_camera(cont): + own = cont.owner + own.worldPosition.x += (dict['player'].worldPosition.x - own.worldPosition.x) * .1 + own.worldPosition.y += (dict['player'].worldPosition.y - own.worldPosition.y) * .1 + +def nextlevel(): + dict["current_level"] += 1 + lvl = dict["current_level"] + print("going to level", lvl) + + logic.getCurrentScene().replace(levels[lvl]) + +def player_death(): + logic.setTimeScale(.03) + dict['player'].alive = False + +#def main(): + \ No newline at end of file diff --git a/player.py b/player.py index 726875d..4b668c4 100644 --- a/player.py +++ b/player.py @@ -1,66 +1,18 @@ -from bge import logic, events -import light_trail -import bge -import random -import math -import mathutils -import utils +from bge import logic +from main import Player import aud -sound = aud.Factory.file(logic.expandPath("//sound/Adventure Meme.mp3")).volume(.1).loop(-1) -#music = aud.device().play(sound) +dict = logic.globalDict -scene = logic.getCurrentScene() - - - -def movement(movement_speed): - cont = logic.getCurrentController() - own = cont.owner - - keyboard = logic.keyboard - ACTIVE = logic.KX_INPUT_ACTIVE - - movement = own.getLinearVelocity() - - # forward - if keyboard.events[events.WKEY] == ACTIVE: - movement[1] = utils.clamp(movement[1] + movement_speed*.1, -movement_speed, movement_speed) - - # backward - if keyboard.events[events.SKEY] == ACTIVE: - movement[1] = utils.clamp(movement[1] + -movement_speed*.1, -movement_speed, movement_speed) - - # left - if keyboard.events[events.AKEY] == ACTIVE: - movement[0] = utils.clamp(movement[0] + -movement_speed*.1, -movement_speed, movement_speed) - - # right - if keyboard.events[events.DKEY] == ACTIVE: - movement[0] = utils.clamp(movement[0] + movement_speed*.1, -movement_speed, movement_speed) - - own.setLinearVelocity(movement) - - if own.getAngularVelocity() < mathutils.Vector((1,1,1)): - own.setAngularVelocity(((random.random()*5)-2.5, (random.random()*5)-2.5, (random.random()*5)-2.5)) - -def main(): - cont = logic.getCurrentController() +def main(cont): own = cont.owner - movement_speed = 20 - - velocity = own.getLinearVelocity() - speed = utils.velocity2speed(velocity) - timescale = utils.clamp(speed/movement_speed, .05, 1) - own['timescale'] = timescale - own['speed'] = speed - if timescale != logic.getTimeScale(): - logic.setTimeScale(timescale) + if not own.get("init", 0): + own["init"] = True + own = Player(own) +# print("assigning player") + dict['player'] = own - scene.objects["player_light"].energy = (math.sin(logic.getRealTime())*.3) + .7 + own.main() - movement(movement_speed) - light_trail.main(cont) -# music.pitch = utils.clamp(timescale*.2 + .85) \ No newline at end of file diff --git a/reset_player b/reset_player new file mode 100644 index 0000000..85d846f --- /dev/null +++ b/reset_player @@ -0,0 +1,12 @@ +from bge import logic + +cont = logic.getCurrentController() +own = cont.owner +message = cont.sensors["Message"] + +logic.getCurrentScene().objects["player"].worldPosition = own.worldPosition + +if 'init' not in own: + logic.getCurrentScene().objects["camera"].worldPosition.x = own.worldPosition.x + logic.getCurrentScene().objects["camera"].worldPosition.y = own.worldPosition.y + own['init'] = True diff --git a/sound/Destiny Day.mp3 b/sound/Destiny Day.mp3 new file mode 100644 index 0000000..d219fcf Binary files /dev/null and b/sound/Destiny Day.mp3 differ diff --git a/sound/Unwritten Return.mp3 b/sound/Unwritten Return.mp3 new file mode 100644 index 0000000..d3ddc02 Binary files /dev/null and b/sound/Unwritten Return.mp3 differ diff --git a/sound/laser/laserfire01_mono.wav b/sound/laser/laserfire01_mono.wav new file mode 100644 index 0000000..6b21827 Binary files /dev/null and b/sound/laser/laserfire01_mono.wav differ diff --git a/sound/laser/laserfire02_mono.wav b/sound/laser/laserfire02_mono.wav new file mode 100644 index 0000000..5cf9460 Binary files /dev/null and b/sound/laser/laserfire02_mono.wav differ diff --git a/utils.py b/utils.py index fc0c681..73d612c 100644 --- a/utils.py +++ b/utils.py @@ -27,4 +27,5 @@ def map_range(value, from_min=0, from_max=1, to_min=0, to_max=1): Thanks to this SO post: http://stackoverflow.com/a/5295202/2730823 """ - return ((to_max-to_min)*(value - from_min) / (from_max - from_min)) + to_min \ No newline at end of file + return ((to_max-to_min)*(value - from_min) / (from_max - from_min)) + to_min + diff --git a/vectsplosion.py b/vectsplosion.py new file mode 100644 index 0000000..eb9d748 --- /dev/null +++ b/vectsplosion.py @@ -0,0 +1,12 @@ +from main import Vectsplosion + +def main(cont): + own = cont.owner + + if not "init" in own: + own["init"] = True + own = Vectsplosion(own) + + own.main() + +