2DPhysics_CollisionOnly.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. class "Detector" (EventHandler)
  2. function Detector:Detector(shape)
  3. self.shape = shape
  4. self:EventHandler()
  5. end
  6. function Detector:handleEvent(e)
  7. if e:getDispatcher() == screen then
  8. local pe = PhysicsScreenEvent(e)
  9. if e:getEventCode() == EVENT_NEW_SHAPE_COLLISION then
  10. if pe:getFirstEntity() == self.shape or pe:getSecondEntity() == self.shape then
  11. pe:getFirstEntity():setColor(1.0, 0.0, 0.0, 1.0)
  12. pe:getSecondEntity():setColor(1.0, 0.0, 0.0, 1.0)
  13. end
  14. elseif e:getEventCode() == EVENT_END_SHAPE_COLLISION then
  15. pe:getFirstEntity():setColor(1.0, 1.0, 1.0, 1.0)
  16. pe:getSecondEntity():setColor(1.0, 1.0, 1.0, 1.0)
  17. end
  18. end
  19. end
  20. screen = PhysicsScreen(10, 60)
  21. checkShape = ScreenShape(SHAPE_RECT, 90,10)
  22. screen:addCollisionChild(checkShape, ENTITY_RECT)
  23. for i=0,50 do
  24. shape = ScreenShape(SHAPE_RECT, 30,15)
  25. shape:setRotation(random(360))
  26. shape:setPosition(random(640), random(480))
  27. screen:addCollisionChild(shape, ENTITY_RECT)
  28. end
  29. detector = Detector(checkShape)
  30. screen:addEventListener(detector, EVENT_NEW_SHAPE_COLLISION)
  31. screen:addEventListener(detector, EVENT_END_SHAPE_COLLISION)
  32. function Update(e)
  33. local mouse = Services.Core:getInput():getMousePosition()
  34. checkShape:setPosition(mouse.x, mouse.y)
  35. checkShape:setRotation(checkShape:getRotation() + (e*100))
  36. delete(mouse)
  37. end