From 01c835567e4c38fcfcc26ae2963f90b77c2acc86 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Sun, 4 Nov 2018 00:47:55 +0900 Subject: [PATCH] Map generator is split from Map class. --- src/main.cr | 161 ++++++++++++++++++++++++++-------------------------- 1 file changed, 81 insertions(+), 80 deletions(-) diff --git a/src/main.cr b/src/main.cr index a9db35f..0266cc1 100644 --- a/src/main.cr +++ b/src/main.cr @@ -5,37 +5,11 @@ require "./naka.cr" class Map @save_directory : String - getter altitude_maps : Naka::Noise2D - getter humidity_maps : Naka::Noise2D - getter evil_maps : Naka::Noise2D - getter flowers_map : Naka::Noise2D - def initialize(@save_directory) + getter generator + def initialize(@save_directory, &block : Proc(Tile, Int32, Int32, Tile?)) Dir.mkdir_p @save_directory - @altitude_maps = Naka::Noise2D.new.tap do |a| - a.add_octave(32, 1) - a.add_octave(16, 0.5) - a.add_octave(8, 0.25) - a.add_octave(4, 0.125) - a.add_octave(2, 1/16) # wtf, too smol - end - - @humidity_maps = Naka::Noise2D.new.tap do |a| - a.add_octave(64, 1) - a.add_octave(32, 0.5) - a.add_octave(16, 0.25) - a.add_octave(8, 0.20) - a.add_octave(4, 0.15) - a.add_octave(2, 0.10) - end - - @evil_maps = Naka::Noise2D.new.tap do |a| - a.add_octave(48, 0.6) - a.add_octave(32, 0.6) - a.add_octave(16, 0.3) - end - - @flowers_map = Naka::Noise2D.new(1, 1) + @generator = block end class Object @@ -73,56 +47,8 @@ class Map Array.new(CHUNK_SIZE) do |y| y += @y * CHUNK_SIZE - Tile.new(Object.new "stone").tap do |i| - altitude = map.altitude_maps.get(x, y) + 0.5 - - humidity = map.humidity_maps.get(x, y) + 0.5 - - evil = map.evil_maps.get(x, y) - - if altitude < 0.25 - i << Object.new "sand" - - if altitude < 0.175 - i << Object.new "water" - else - if humidity < 0.33 - i << Object.new "sand" - else - i << Object.new "dirt" - end - end - elsif altitude < 0.75 - i << Object.new "dirt" - - if evil > 0.5 - i << Object.new "haunted" - else - i << Object.new "grass" - end - - if altitude > 0.35 && altitude < 0.65 - if humidity > 0.4 - if humidity > 0.8 - i << Object.new "tree" - elsif humidity < 0.6 - i << Object.new "pine" - else - if Random.rand(2) == 0 - i << Object.new "tree" - else - i << Object.new "pine" - end - end - else - if Random.rand(10) == 0 - i << Object.new "flowers" - end - end - end - elsif altitude > 0.85 - i << Object.new "rocks" - end + Tile.new(Object.new "stone").tap do |tile| + map.generator.call tile, x, y end end end @@ -205,7 +131,82 @@ images = ImagesLoader.new window connection_font = window.newFont "Connection.otf", 30.5 window.set_font connection_font -map = Map.new "map-test-01" +altitude_map = Naka::Noise2D.new.tap do |a| + a.add_octave(32, 1) + a.add_octave(16, 0.5) + a.add_octave(8, 0.25) + a.add_octave(4, 0.125) + a.add_octave(2, 1/16) # wtf, too smol +end + +humidity_map = Naka::Noise2D.new.tap do |a| + a.add_octave(64, 1) + a.add_octave(32, 0.5) + a.add_octave(16, 0.25) + a.add_octave(8, 0.20) + a.add_octave(4, 0.15) + a.add_octave(2, 0.10) +end + +evil_map = Naka::Noise2D.new.tap do |a| + a.add_octave(48, 0.6) + a.add_octave(32, 0.6) + a.add_octave(16, 0.3) +end + +flowers_map = Naka::Noise2D.new(1, 1) + +map = Map.new "map-test-01" do |tile, x, y| + altitude = altitude_map.get(x, y) + 0.5 + + humidity = humidity_map.get(x, y) + 0.5 + + evil = evil_map.get(x, y) + + if altitude < 0.25 + tile << Map::Object.new "sand" + + if altitude < 0.175 + tile << Map::Object.new "water" + else + if humidity < 0.33 + tile << Map::Object.new "sand" + else + tile << Map::Object.new "dirt" + end + end + elsif altitude < 0.75 + tile << Map::Object.new "dirt" + + if evil > 0.5 + tile << Map::Object.new "haunted" + else + tile << Map::Object.new "grass" + end + + if altitude > 0.35 && altitude < 0.65 + if humidity > 0.4 + if humidity > 0.8 + tile << Map::Object.new "tree" + elsif humidity < 0.6 + tile << Map::Object.new "pine" + else + if Random.rand(2) == 0 + tile << Map::Object.new "tree" + else + tile << Map::Object.new "pine" + end + end + else + if Random.rand(10) == 0 + tile << Map::Object.new "flowers" + end + end + end + elsif altitude > 0.85 + tile << Map::Object.new "rocks" + end +end Naka::Event.loop window do |event| case event