test33.nim 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # --- Test 33. use KinematicBody2D node. --- #
  2. # Please, compile with `--define:debug` or with `-d:debug` for see collision shapes.
  3. import nodesnim
  4. Window("hello world")
  5. var
  6. mainobj: SceneObj
  7. main = Scene("Main", mainobj)
  8. shape1_obj: CollisionShape2DObj
  9. shape1 = CollisionShape2D(shape1_obj)
  10. shape2_obj: CollisionShape2DObj
  11. shape2 = CollisionShape2D(shape2_obj)
  12. shape3_obj: CollisionShape2DObj
  13. shape3 = CollisionShape2D(shape3_obj)
  14. shape4_obj: CollisionShape2DObj
  15. shape4 = CollisionShape2D(shape4_obj)
  16. body_obj: KinematicBody2DObj
  17. body = KinematicBody2D(body_obj)
  18. shape1.move(100, 100)
  19. shape2.move(125, 125)
  20. shape4.move(360, 25)
  21. shape1.setShapeTypeCircle(0, 0, 35)
  22. shape2.resize(150, 50)
  23. # shape3.setShapeTypeCircle(0, 0, 35)
  24. shape3.setShapeTypePolygon(Vector2(0, 0), Vector2(15, 5), Vector2(28, 15), Vector2(35, 25), Vector2(5, 45))
  25. shape4.setShapeTypePolygon(Vector2(0, 0), Vector2(150, 65), Vector2(25, 150))
  26. Input.addButtonAction("left", BUTTON_LEFT)
  27. body.on_process =
  28. proc(self: NodePtr) =
  29. if Input.isActionPressed("left"):
  30. let
  31. mouse_pos = body.getGlobalMousePosition()
  32. distance = body.global_position.distance(mouse_pos)
  33. direction = body.global_position.directionTo(mouse_pos)
  34. speed = 3f
  35. if distance >= 5:
  36. body.moveAndCollide(direction*speed)
  37. main.addChild(shape1)
  38. main.addChild(shape2)
  39. main.addChild(shape4)
  40. main.addChild(body)
  41. body.addChild(shape3)
  42. addScene(main)
  43. setMainScene("Main")
  44. windowLaunch()