Skip to content

Commit

Permalink
remove unused code in sentry.py and add map_range function to utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalf3 committed Dec 21, 2015
1 parent 1121d83 commit 0d68b8e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 60 deletions.
Binary file modified lonely_photon.blend
Binary file not shown.
59 changes: 44 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from bge import logic, types
import aud
import random
import utils

class Sentry(types.KX_GameObject):
Expand All @@ -21,6 +23,15 @@ def __init__(self, own):

self.firenow = 0

self.mparts = []

for child in self.children:
if "firepoint" in child.name:
self.firepoint = child
if "mpart" in child.name:
self.mparts.append(child)




def aim(self):
Expand All @@ -32,40 +43,58 @@ def aim(self):
else:
self["cansee"] = False

return
return self["cansee"]



def fire(self):

print("pow")
projectile = self.scene.addObject(self.projectile_type, self, 0)
projectile = self.scene.addObject(self.projectile_type, self.firepoint, 0)
projectile.worldOrientation = self.worldOrientation
projectile.setLinearVelocity((-20, 0, 0), True)

for part in self.mparts:
part.playAction(name="sentry", start_frame=1, end_frame=4)

self.firenow = 0



def main(self):
self.aim()

print(self.firenow*logic.getTimeScale())
if self.firenow*logic.getTimeScale() >= 1:
aim = self.aim()

if self.firenow > 1 and aim:
self.fire()

self.firenow += 1*self.fireRate
self.firenow += 1*self.fireRate*logic.getTimeScale()



class Projectile(types.KX_GameObject):

light_source = "standard_projectile_lamp"

def __init__(self, own):
self.cont = self.controllers[0]
def __init__(self, own, sound):

self.light = self.scene.addObject(light_source)
self.light.setParent(self,0,0)
self.sound = sound

self.speed = -30
self.homing_factor = 0

self.light_source = "standard_projectile_lamp"

# self.light = self.scene.addObject(self.light_source)

self.sound_handler = aud.device().play(self.sound)
# self.setLinearVelocity((self.speed, 0, 0), True)

def main(self):

self.setLinearVelocity((self.speed, 0, 0), True)

#self.light.worldPosition = self.worldPosition

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:

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)

44 changes: 0 additions & 44 deletions sentry.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
from bge import logic
from main import Sentry
import random
import math
import aud
import utils

laser_sound = aud.Factory.file(logic.expandPath("//sound/laser/laserfire01.ogg")).volume(.5).fadeout(0, .4)


def search():
cont = logic.getCurrentController()
own = cont.owner

scene = logic.getCurrentScene()
target = scene.objects["player"]

hit = own.rayCast(target, own, 0.0, "solid", 0, 1, 0)

if hit[0] == target:
own["cansee"] = True
if own.get("fireclock", 0)*logic.getTimeScale() > 1:
# l.pitch = random.randrange(5, 15)*.1

fire(hit[2])
own["fireclock"] = 0

else:
own["cansee"] = False

own["fireclock"] = own.get("fireclock", 0) + 1

def fire(direction):
cont = logic.getCurrentController()
own = cont.owner

scene = logic.getCurrentScene()

projectile = scene.addObject("standard_projectile", own, 0)
projectile.alignAxisToVect(direction, 0, 1)
projectile.setLinearVelocity((-20, 0, 0), True)

projectile["sound_handler"] = aud.device().play(laser_sound)
if projectile.get("sound_handler", 0):
projectile["sound_handler"].pitch = logic.getTimeScale()

def main(cont):
own = cont.owner
Expand Down
16 changes: 15 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""
Basic utility functions
"""

def clamp(value, min_value=0, max_value=1):
return max(min(value, max_value), min_value)

def velocity2speed(velocity):
"""
Turns a vector into a scalar. Is never negative.
"""
speed = 0
for i in velocity:
speed += abs(i)
Expand All @@ -13,4 +19,12 @@ def distance(point1, point2):
Distance on the X/Y plane between two points
"""

return sqrt((point1.x - point2.x)**2 + (point1.y - point2.y))
return sqrt((point1.x - point2.x)**2 + (point1.y - point2.y))

def map_range(value, from_min=0, from_max=1, to_min=0, to_max=1):
"""
Scale value from
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

0 comments on commit 0d68b8e

Please sign in to comment.