test33.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # --- Test 32. 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. body_obj: KinematicBody2DObj
  15. body = KinematicBody2D(body_obj)
  16. shape1.move(100, 100)
  17. shape2.move(125, 125)
  18. shape1.setShapeTypeCircle(0, 0, 35)
  19. shape2.resize(150, 50)
  20. shape3.setShapeTypeCircle(0, 0, 35)
  21. Input.addButtonAction("left", BUTTON_LEFT)
  22. body.process =
  23. proc() =
  24. if Input.isActionPressed("left"):
  25. let
  26. mouse_pos = body.getGlobalMousePosition()
  27. distance = body.global_position.distance(mouse_pos)
  28. direction = body.global_position.directionTo(mouse_pos)
  29. speed = 3f
  30. if distance >= 5:
  31. discard body.moveAndCollide(direction*speed)
  32. main.addChild(shape1)
  33. main.addChild(shape2)
  34. main.addChild(body)
  35. body.addChild(shape3)
  36. addScene(main)
  37. setMainScene("Main")
  38. windowLaunch()