Tangent 4 years ago
commit
d2d4d57509
5 changed files with 118 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 17
    0
      src/Player.moon
  3. 51
    0
      src/World.moon
  4. 3
    0
      src/constants.moon
  5. 46
    0
      src/main.moon

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+*.lua

+ 17
- 0
src/Player.moon View File

@@ -0,0 +1,17 @@
1
+import floor from math
2
+import tileSize from require "constants"
3
+
4
+class Player
5
+  new: (@x=0, @y=0) =>
6
+    @speed = 2500 -- 100
7
+  update: (dt) =>
8
+    if love.keyboard.isDown "w"
9
+      @y -= @speed * dt
10
+    if love.keyboard.isDown "a"
11
+      @x -= @speed * dt
12
+    if love.keyboard.isDown "s"
13
+      @y += @speed * dt
14
+    if love.keyboard.isDown "d"
15
+      @x += @speed * dt
16
+  tile: => return floor(@x/tileSize), floor(@y/tileSize)
17
+  offset: => return @x % tileSize, @y % tileSize

+ 51
- 0
src/World.moon View File

@@ -0,0 +1,51 @@
1
+import random, noise from love.math
2
+
3
+class World
4
+  new: =>
5
+    @generateSeed!
6
+  get: (x, y) =>
7
+    map = @map
8
+    unless map[x]
9
+      map[x] = {}
10
+    unless map[x][y]
11
+      p = @precipitation(x, y)
12
+      -- map[x][y] = { 1 - p, 1 - p, 1, 1 }
13
+      h = @height(x, y)
14
+      -- map[x][y] = { h, h, h, 1 }
15
+      t = @temperature(x, y)
16
+      -- map[x][y] = { t, 0, 0, 1 }
17
+      map[x][y] = { t, h, p, 1 }
18
+    return map[x][y]
19
+  generateSeed: =>
20
+    @map = {}
21
+    @precipitationXOffset = random! * 256
22
+    @precipitationYOffset = random! * 256
23
+    @precipitationScale = random! * 0.009 + 0.001
24
+    @temperatureXOffset = random! * 256
25
+    @temperatureYOffset = random! * 256
26
+    @temperatureScale = random! * 0.009 + 0.001
27
+    @heightXOffset = random! * 256
28
+    @heightYOffset = random! * 256
29
+    @heightScale = random! * 0.009 + 0.001
30
+    @heightAmplitude = random! * 0.9 + 0.1 -- max: 1, min: 0.1
31
+    @heightXOffset2 = random! * 256
32
+    @heightYOffset2 = random! * 256
33
+    @heightScale2 = random! * 0.012 + 0.008
34
+    @heightAmplitude2 = random! * 0.09 + 0.01 -- max: 0.1, min: 0.01
35
+    @heightXOffset3 = random! * 256
36
+    @heightYOffset3 = random! * 256
37
+    @heightScale3 = random! * 0.092 + 0.01
38
+    @heightAmplitude3 = random! * 0.009 + 0.001 -- max: 0.01, min: 0.001
39
+  precipitation: (x, y) =>
40
+    return noise((x + @precipitationXOffset) * @precipitationScale, (y + @precipitationYOffset) * @precipitationScale)
41
+  temperature: (x, y) =>
42
+    return noise((x + @temperatureXOffset) * @temperatureScale, (y + @temperatureYOffset) * @temperatureScale)
43
+  height: (x, y) =>
44
+    h = noise((x + @heightXOffset) * @heightScale, (y + @heightYOffset) * @heightScale) * @heightAmplitude +
45
+      noise((x + @heightXOffset2) * @heightScale2, (y + @heightYOffset2) * @heightScale2) * @heightAmplitude2 +
46
+      noise((x + @heightXOffset3) * @heightScale3, (y + @heightYOffset3) * @heightScale3) * @heightAmplitude3
47
+    -- max: 1.11, min: 0.111
48
+    -- slope = (output_end - output_start) / (input_end - input_start)
49
+    -- output = output_start + slope * (input - input_start)
50
+    slope = 1 / (1.11 - 0.111)
51
+    return slope * (h - 0.111)

+ 3
- 0
src/constants.moon View File

@@ -0,0 +1,3 @@
1
+return {
2
+  tileSize: 20
3
+}

+ 46
- 0
src/main.moon View File

@@ -0,0 +1,46 @@
1
+import random from love.math
2
+import tileSize from require "constants"
3
+w, h = love.graphics.getDimensions!
4
+screen = { w: math.floor(w/tileSize), h: math.floor(h/tileSize)}
5
+
6
+Player = require "Player"
7
+player = Player!
8
+
9
+World = require "World"
10
+world = World!
11
+
12
+time, step = 0, 1/30
13
+love.update = (dt) ->
14
+  time += dt
15
+  if time >= step
16
+    time -= step
17
+    player\update step
18
+
19
+love.draw = ->
20
+  x, y = player\tile!
21
+  screenX, screenY = 0, 0
22
+  xOffset, yOffset = player\offset!
23
+  for x = x - screen.w/2, x + screen.w/2
24
+    for y = y - screen.h/2, y + screen.h/2
25
+      tile = world\get x, y
26
+      if tile
27
+        love.graphics.setColor tile
28
+      else
29
+        love.graphics.setColor 0, 0, 0, 1
30
+      love.graphics.rectangle "fill", screenX * tileSize - xOffset, screenY * tileSize - yOffset, tileSize, tileSize
31
+      screenY += 1
32
+    screenY = 0
33
+    screenX += 1
34
+  love.graphics.setColor 1, 0, 0, 1
35
+  love.graphics.circle "line", w/2, h/2, 5
36
+
37
+love.keypressed = (key) ->
38
+  love.event.quit! if key == "escape"
39
+
40
+  if key == "="
41
+    player.speed *= 5
42
+  if key == "-"
43
+    player.speed /= 5
44
+
45
+  if key == "r"
46
+    world\generateSeed!