Skip to content

Commit

Permalink
Added basic system for collision callback function/scripts - replaced…
Browse files Browse the repository at this point in the history
… UI option for sensor with object type setting
  • Loading branch information
chozabu committed May 24, 2014
1 parent 7089939 commit 12caeb3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
14 changes: 10 additions & 4 deletions kivented.kv
Expand Up @@ -140,6 +140,7 @@ TestGame:
massLabel: massLabel
elasLabel: elasLabel
texLabel: texLabel
colTypeSpinner: colTypeSpinner
delButton: delButton
Label:
id: selectedLabel
Expand Down Expand Up @@ -191,10 +192,15 @@ TestGame:
id: elasLabel
text: "Elasticity"
on_text: root.elasChanged(self)
ToggleButton:
id: sensorButton
text:"Sensor"
on_press: root.sensorPressed(self)
#ToggleButton:
# id: sensorButton
# text:"Sensor"
# on_press: root.sensorPressed(self)
Spinner:
id: colTypeSpinner
text: "default"
on_text: root.colTypeChanged(self)
values: ["default", "vortex"]
BoxLayout:
id: shapeInfo
size_hint_y:None
Expand Down
49 changes: 39 additions & 10 deletions main.py
Expand Up @@ -8,6 +8,7 @@
import os

import serialisation
from util import TwoWayDict

from kivy.app import App
from kivy.uix.widget import Widget
Expand All @@ -30,6 +31,12 @@ def __init__(self, **kwargs):
super(TestGame, self).__init__(**kwargs)
Clock.schedule_once(self.init_game)
self.entIDs = []
self.collision_types = TwoWayDict()
self.collision_types['default'] = 0
self.collision_types['vortex'] = 1
self.collision_types['physzone'] = 2
self.collision_funcs = {'pull2_first':self.pull2_first}
self.collision_handlers = {'vortex':{'pre_solve':('default','pull2_first')}}
self.mainTools = self.ids['gamescreenmanager'].ids['main_screen'].ids['mainTools']
self.mainTools.setRef(self)
self.mainTools.setTool("draw")
Expand All @@ -45,12 +52,25 @@ def __init__(self, **kwargs):
except:
print 'Python python no keyboard'


def init_game(self, dt):
try:
self._init_game(0)
except:
print 'failed: rescheduling init'
Clock.schedule_once(self.init_game)
#try:
self._init_game(0)
#except:
# print 'failed: rescheduling init'
# Clock.schedule_once(self.init_game)

def pull2_first(self, space, arbiter):
first_body = arbiter.shapes[0].body
second_body = arbiter.shapes[1].body
first_pos = first_body.position
second_pos = second_body.position
diff = cy.Vec2d(first_pos.x-second_pos.x,first_pos.y-second_pos.y)
#diff.x*=10
#diff.y*=10
second_body.apply_impulse(diff)
#print diff
return False

def _init_game(self, dt):
self.setup_map()
Expand All @@ -59,6 +79,15 @@ def _init_game(self, dt):

self.draw_some_stuff()
self.space = self.gameworld.systems['physics'].space
for typeastr, ch in self.collision_handlers.iteritems():
typea = self.collision_types[typeastr]
for funcstr, argstr in ch.iteritems():
typeb = self.collision_types[argstr[0]]
func = self.collision_funcs[argstr[1]]
funcdict = {funcstr:func}
print typea, typeb, func
self.space.add_collision_handler(typea, typeb, **funcdict)
#self.space.add_collision_handler(1, 0, begin = self.pull2_first)
self.serials = serialisation.Serials(self)
Clock.schedule_interval(self.update, 0)
Clock.schedule_once(self.init_sprites)
Expand Down Expand Up @@ -86,7 +115,7 @@ def draw_some_stuff(self):
size = Window.size
for x in range(50):
pos = (randint(size[0] / 3, size[0]), randint(0, size[1]))
self.create_circle(pos, y_vel=random() * -20, texture="sheep", radius=15, selectNow=False)
self.create_circle(pos, y_vel=random() * -20, texture="sheep", radius=15, selectNow=False, collision_type=1)
self.create_box((size[0] / 2.0, 0), mass=0, width=size[0] * 2, height=10, angle=0, selectNow=False)

def _keyboard_closed(self):
Expand Down Expand Up @@ -117,11 +146,11 @@ def create_decoration(self, pos=(0, 0), width=40, height=40, angle=0, texture="s
return entityID

def create_circle(self, pos, radius=6., mass=10., friction=1.0, elasticity=.5, angle=.0, x_vel=.0, y_vel=.0,
angular_velocity=0., texture="sheep", selectNow=True, sensor = False):
angular_velocity=0., texture="sheep", selectNow=True, sensor = False, collision_type = 0):
shape_dict = {'inner_radius': 0, 'outer_radius': radius,
'mass': mass, 'offset': (0, 0)}
col_shape = {'shape_type': 'circle', 'elasticity': elasticity,
'collision_type': 1, 'shape_info': shape_dict, 'friction': friction}
'collision_type': collision_type, 'shape_info': shape_dict, 'friction': friction}
col_shapes = [col_shape]
physics_component = {'main_shape': 'circle',
'velocity': (x_vel, y_vel),
Expand All @@ -143,13 +172,13 @@ def create_circle(self, pos, radius=6., mass=10., friction=1.0, elasticity=.5, a
return entityID

def create_box(self, pos, width=40., height=40., mass=10., friction=1.0, elasticity=.5, angle=.0, x_vel=.0, y_vel=.0,
angular_velocity=.0, texture="face_box", selectNow=True, sensor = False):
angular_velocity=.0, texture="face_box", selectNow=True, sensor = False, collision_type = 0):
box_dict = {
'width': width,
'height': height,
'mass': mass}
col_shape = {'shape_type': 'box', 'elasticity': elasticity,
'collision_type': 1, 'shape_info': box_dict, 'friction': friction}
'collision_type': collision_type, 'shape_info': box_dict, 'friction': friction}
col_shapes = [col_shape]
physics_component = {'main_shape': 'box',
'velocity': (x_vel, y_vel),
Expand Down
7 changes: 7 additions & 0 deletions ui_elements.py
Expand Up @@ -230,6 +230,8 @@ def setShape(self, shape):
self.selectedMenu.frictionLabel.text = "%0.2f" % shape.friction
self.selectedMenu.massLabel.text = "%0.2f" % shape.body.mass
self.selectedMenu.elasLabel.text = "%0.2f" % shape.elasticity
print self.gameref.collision_types[shape.collision_type], shape.collision_type
self.selectedMenu.colTypeSpinner.text = self.gameref.collision_types[shape.collision_type]
self.selectedMenu.shapeInfo.clear_widgets()
if shape.__class__.__name__ == "Circle":
cs = CircleSettings()
Expand Down Expand Up @@ -261,6 +263,11 @@ def sensorPressed(self, instance):
newval = not self.selectedItem.sensor
self.selectedItem.sensor = newval
instance.pressed = newval
def colTypeChanged(self, instance):
if self.selectedItem and self.gameref:
newval = self.gameref.collision_types[instance.text]
print newval, instance.text
self.selectedItem.collision_type = newval


def clearl2(self):
Expand Down
24 changes: 24 additions & 0 deletions util.py
@@ -0,0 +1,24 @@
__author__ = 'chozabu'

#from http://stackoverflow.com/a/13276237/445831 - Sasha Chedygov
class TwoWayDict(dict):
#def __init__(self,iterable=None, **kwargs):
# super(TwoWayDict,self).__init__(iterable, **kwargs)

def __setitem__(self, key, value):
# Remove any previous connections with these values
if key in self:
del self[key]
if value in self:
del self[value]
dict.__setitem__(self, key, value)
dict.__setitem__(self, value, key)

def __delitem__(self, key):
dict.__delitem__(self, self[key])
dict.__delitem__(self, key)

def __len__(self):
"""Returns the number of connections"""
# The int() call is for Python 3
return int(dict.__len__(self) / 2)

0 comments on commit 12caeb3

Please sign in to comment.