summaryrefslogtreecommitdiff
path: root/src/corridor.fnl
blob: 53634344fbe77098ca2d1ccbfb072d4c960ee266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;; corridor
;; maybe make this generic enough to have multiple
;; corridors with the same code, eventually

(local player (require :src.player))
(local bump (require :vendor.bump))

(fn clamp [x min max]
    (if (< x min) min
        (> x max) max
        x))

(fn draw-rectangle [mode x1 y1 x2 y2]
    ; instead of width and height
    (love.graphics.polygon mode
                           x1 y1   x2 y1
                           x2 y2   x1 y2))

(local WIDTH 640)
(local HALFWIDTH (/ WIDTH 2))

(local border-left -30)
(local border-right 1230)

(fn effective-ppos-x [player-x]
    (let [threshold-left  (+ border-left  HALFWIDTH)
          threshold-right (- border-right HALFWIDTH)]
         (clamp player-x threshold-left threshold-right)))

(fn trans-amt-x [player-x]
    (let [eff (effective-ppos-x player-x)]
         (- HALFWIDTH eff)))


(fn draw-stars [cpos]
    (love.graphics.setColor 0.05 0.05 0.2)
    (love.graphics.rectangle :fill border-left 0 (- border-right border-left) 480)
    (math.randomseed 62869420)
    (love.graphics.setColor 1 1 1)
    (for [i 1 200]
         (let [sz (math.random 3 10)
               x (math.random border-left border-right)
               y (math.random 0 480)
               c (- 0.7 (* 0.3 (math.random)))
               d (+ 0.5 (math.random))]
              (love.graphics.setColor 1 1 c)
              (love.graphics.rectangle :fill (+ x (* 0.1 d cpos)) y sz sz))))

(fn add-rect-bounds [world x1 y1 x2 y2 ?inset ?prefix ?thickness]
    (let [w (- x2 x1)
          h (- y2 y1)
          prefix (or ?prefix "")
          thickness (or ?thickness 10)
          inset (or ?inset 0)
          Lu (.. prefix :-Wu)
          Ld (.. prefix :-Wd)
          Ll (.. prefix :-Wl)
          Lr (.. prefix :-Wr)]          
         (print thickness)
         (world:add Lu x1 (- y1 thickness) w (+ thickness inset))
         (world:add Ld x1 (- y2 inset) w (+ thickness inset))
         (world:add Ll (- x1 thickness) y1 (+ thickness inset) h)
         (world:add Lr (- x2 inset) y1 (+ thickness inset) h)))


(fn dbg-world [world]
    (love.graphics.setLineWidth 1)
    (love.graphics.setColor 1 0 0)
    (let [items (world:getItems)]
         (each [_ item (pairs items)]
               (let [(x y w h) (world:getRect item)]
                    (love.graphics.rectangle :line x y w h)
	                (if (= (type item) :string)
	                    (love.graphics.print item x y))))))


(fn make [font]
    (local title (love.graphics.newText font "Corridor A2"))
    (local player (player.make 100 340))
    (local windows [[10  100 400 80]
                    [600 100 400 80]
                    [1050 50 800 300]])
    (local world (bump.newWorld))
    (world:add :player player.x player.y 30 30)
    (add-rect-bounds world 0 250 1000 450 5)
        
    
    (fn draw-windows []
        (love.graphics.stencil (fn []
                                   (each [_ [x y w h] (pairs windows)]
                                         (love.graphics.rectangle :fill x y w h))
                                   :replace 1))
        (love.graphics.setStencilTest :greater 0)

        (love.graphics.push)
        (let [cpos (trans-amt-x player.x)]
             (love.graphics.origin)
             (love.graphics.translate (* 0.25 cpos) 0)
	         (draw-stars cpos))
        (love.graphics.pop)
        
        (love.graphics.setStencilTest)
        (love.graphics.setColor 0 0 0)
        (love.graphics.setLineWidth 7)
        (each [_ [x y w h] (pairs windows)]
              (love.graphics.rectangle :line x y w h)))
    
        
    (fn draw []
        (love.graphics.setColor 0 0 0)
        
        (fn dbg-print [...]
            ; bad
            (var out {})
            (for [i 1 (select :# ...) 2]
                  (table.insert out (..
                                     (select i ...)
                                     " "
                                     (select (+ i 1) ...))))
            (let [txt (table.concat out "\n")]
                 (love.graphics.print txt 6 28)
                 (love.graphics.print txt 500 400)))
        
        ;(dbg-print
        ; :x player.x
        ; :y player.y)

        (love.graphics.translate (trans-amt-x player.x) 0)
        (love.graphics.setColor 0.8 0.8 0.8)
        (love.graphics.setLineWidth 10)
        (draw-rectangle :line 0 250 1000 450)
        (draw-windows)
        (love.graphics.draw title 0 10)
        (player:draw)
        
        ;(dbg-world world)
        )
    
    (fn update [dt]
        (player:update dt world))

    {: draw
     : update})

{: make}