main.nim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import
  2. nodesnim,
  3. random
  4. Window("Roguelike", 480, 240)
  5. env.background_color = "#e0f8cf"
  6. var
  7. tileset = TileSet("assets/colored_tilemap.png", Vector2(16, 16), GL_RGBA)
  8. charapter = load("assets/player.png", GL_RGBA)
  9. const
  10. PLAYER_SPEED: float = 4
  11. LEVEL_WIDTH: float = 100
  12. PLAYER_SIZE: float = 16
  13. build:
  14. - Scene main:
  15. - TileMap map:
  16. call setTileSet(tileset)
  17. call resizeMap(Vector2(LEVEL_WIDTH, 15), 2)
  18. # Player
  19. - KinematicBody2D player:
  20. call move(300, 120)
  21. - Sprite player_sprite:
  22. call setTexture(charapter)
  23. - CollisionShape2D player_collision:
  24. call resize(PLAYER_SIZE, PLAYER_SIZE)
  25. call move(-8, -8)
  26. # Collision
  27. - CollisionShape2D top:
  28. call resize(LEVEL_WIDTH*16, 1)
  29. call move(0, -1)
  30. - CollisionShape2D bottom:
  31. call resize(LEVEL_WIDTH*16, 1)
  32. call move(0, 240)
  33. - CollisionShape2D left:
  34. call resize(1, 240)
  35. call move(PLAYER_SIZE, 0)
  36. - CollisionShape2D right:
  37. call resize(1, 240)
  38. call move(LEVEL_WIDTH*16, 0)
  39. - Camera2D camera:
  40. call setLimit(0, 0, LEVEL_WIDTH*16, 240)
  41. call setCurrent()
  42. call setTarget(player)
  43. call enableSmooth()
  44. # Draw grass
  45. for i in 0..512:
  46. map.drawTile(rand(99), rand(14), Vector2(0, 1), 1)
  47. # Draw trees
  48. for i in 0..128:
  49. map.drawTile(rand(99), rand(14), Vector2(rand(13..16).float, 0), 1)
  50. # Draw houses
  51. for i in 0..32:
  52. var
  53. pos = Vector2(rand(99).float, rand(14).float)
  54. collider = CollisionShape2D()
  55. map.drawTile(pos.x.int, pos.y.int, Vector2(rand(13..16).float, 2), 1)
  56. collider.resize(PLAYER_SIZE, PLAYER_SIZE)
  57. collider.move(pos.x*PLAYER_SIZE, pos.y*PLAYER_SIZE)
  58. main.addChild(collider)
  59. # Movement
  60. addKeyAction("forward", "w")
  61. addKeyAction("backward", "s")
  62. addKeyAction("right", "d")
  63. addKeyAction("left", "a")
  64. player@onProcess(self):
  65. if isActionPressed("right"):
  66. player.moveAndCollide(Vector2(PLAYER_SPEED, 0))
  67. elif isActionPressed("left"):
  68. player.moveAndCollide(Vector2(-PLAYER_SPEED, 0))
  69. if isActionPressed("forward"):
  70. player.moveAndCollide(Vector2(0, -PLAYER_SPEED))
  71. elif isActionPressed("backward"):
  72. player.moveAndCollide(Vector2(0, PLAYER_SPEED))
  73. addMainScene(main)
  74. windowLaunch()