main.nim 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. z_index: 1
  21. call move(300, 120)
  22. - Sprite player_sprite:
  23. call setTexture(charapter)
  24. - CollisionShape2D player_collision:
  25. call resize(PLAYER_SIZE, PLAYER_SIZE)
  26. call move(-8, -8)
  27. # Collision
  28. - CollisionShape2D top:
  29. call resize(LEVEL_WIDTH*16, 1)
  30. call move(0, -1)
  31. - CollisionShape2D bottom:
  32. call resize(LEVEL_WIDTH*16, 1)
  33. call move(0, 240)
  34. - CollisionShape2D left:
  35. call resize(1, 240)
  36. call move(PLAYER_SIZE, 0)
  37. - CollisionShape2D right:
  38. call resize(1, 240)
  39. call move(LEVEL_WIDTH*16, 0)
  40. - Camera2D camera:
  41. call setLimit(0, 0, LEVEL_WIDTH*16, 240)
  42. call setCurrent()
  43. call setTarget(player)
  44. call enableSmooth()
  45. # Draw grass
  46. for i in 0..512:
  47. map.drawTile(rand(99), rand(14), Vector2(0, 1), 1)
  48. # Draw trees
  49. for i in 0..128:
  50. map.drawTile(rand(99), rand(14), Vector2(rand(13..16).float, 0), 1)
  51. # Draw houses
  52. for i in 0..32:
  53. var
  54. pos = Vector2(rand(99).float, rand(14).float)
  55. collider = CollisionShape2D()
  56. map.drawTile(pos.x.int, pos.y.int, Vector2(rand(13..16).float, 2), 1)
  57. collider.resize(PLAYER_SIZE, PLAYER_SIZE)
  58. collider.move(pos.x*PLAYER_SIZE, pos.y*PLAYER_SIZE)
  59. main.addChild(collider)
  60. # Movement
  61. addKeyAction("forward", "w")
  62. addKeyAction("backward", "s")
  63. addKeyAction("right", "d")
  64. addKeyAction("left", "a")
  65. player@onProcess(self):
  66. if isActionPressed("right"):
  67. player.moveAndCollide(Vector2(PLAYER_SPEED, 0))
  68. elif isActionPressed("left"):
  69. player.moveAndCollide(Vector2(-PLAYER_SPEED, 0))
  70. if isActionPressed("forward"):
  71. player.moveAndCollide(Vector2(0, -PLAYER_SPEED))
  72. elif isActionPressed("backward"):
  73. player.moveAndCollide(Vector2(0, PLAYER_SPEED))
  74. addMainScene(main)
  75. windowLaunch()