test31.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # --- Test 31. 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. main = Scene("Main")
  7. shape1 = CollisionShape2D()
  8. shape2 = CollisionShape2D()
  9. shape3 = CollisionShape2D()
  10. shape4 = CollisionShape2D()
  11. body = KinematicBody2D()
  12. shape1.move(100, 100)
  13. shape2.move(125, 125)
  14. shape4.move(360, 25)
  15. shape1.setShapeTypeCircle(0, 0, 35)
  16. shape2.resize(150, 50)
  17. # shape3.setShapeTypeCircle(0, 0, 35)
  18. shape3.setShapeTypePolygon(Vector2(0, 0), Vector2(15, 5), Vector2(28, 15), Vector2(35, 25), Vector2(5, 45))
  19. shape4.setShapeTypePolygon(Vector2(0, 0), Vector2(150, 65), Vector2(25, 150))
  20. Input.addButtonAction("left", BUTTON_LEFT)
  21. body.on_process =
  22. proc(self: NodeRef) =
  23. if Input.isActionPressed("left"):
  24. let
  25. mouse_pos = body.getGlobalMousePosition()
  26. distance = body.global_position.distance(mouse_pos)
  27. direction = body.global_position.directionTo(mouse_pos)
  28. speed = 3f
  29. if distance >= 5:
  30. body.moveAndCollide(direction*speed)
  31. main.addChild(shape1)
  32. main.addChild(shape2)
  33. main.addChild(shape4)
  34. main.addChild(body)
  35. body.addChild(shape3)
  36. addScene(main)
  37. setMainScene("Main")
  38. windowLaunch()