summaryrefslogtreecommitdiff
path: root/src/player.fnl
blob: 702d466d8eac19159c0b02c06d05d53cfe3b8bfc (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
(local SPEED 170)
(local SPRINT-SPEED 280)

(local C {})
(set C.__index C)

(fn C.update [self dt world]
    (let [speed (if (love.keyboard.isScancodeDown :lshift :rshift) 
                    SPRINT-SPEED SPEED)
          up    (if (love.keyboard.isScancodeDown :w :up) 1 0)
          down  (if (love.keyboard.isScancodeDown :s :down) 1 0)
          left  (if (love.keyboard.isScancodeDown :a :left) 1 0)
          right (if (love.keyboard.isScancodeDown :d :right) 1 0)
          vy (- down up)
          vx (- right left)
          sf (math.sqrt (+ (* vx vx) (* vy vy)))]
          ; world (if ?world ?world
          ;           {:move (fn move [_1 _2 tx ty] (values tx ty))})]
         (when (not= sf 0)
               (if (> vx 0) (set self.left false)
                   (< vx 0) (set self.left true))
               (let [vx (/ vx sf)
                     vy (/ vy sf)
                     tx (+ self.x (* vx dt speed))
                     ty (+ self.y (* vy dt speed))
                     (ax ay) (if world
                                 (world:move :player tx ty)
                                 (values tx ty))]
                    (set self.x ax)
                    (set self.y ay)))))

(fn C.draw [self]
    (love.graphics.push)
    (love.graphics.translate self.x self.y)
    (love.graphics.translate 15 15)
    (if self.left
        (love.graphics.scale -1 1))
    (love.graphics.scale 0.1)
    
    (love.graphics.setColor 0.91 0.62 0)
    (love.graphics.circle :fill 0 0 150)
    (love.graphics.setColor 1 1 1)
    (love.graphics.setLineWidth 4)
    (love.graphics.circle :line 0 0 150)

    
    (love.graphics.setColor 0 0 0)
    (love.graphics.circle :fill 30  -80 20)
    (love.graphics.circle :fill 100 -80 20)
    
    (love.graphics.setLineWidth 20)
    (love.graphics.line 40  50   0   70)
    (love.graphics.line 120 50   150 70)
    
    
    (love.graphics.pop))


    

(fn make [?x ?y] 
    (let [x (or ?x 170)
          y (or ?y 150)
          s {: x : y :left false}]
         (setmetatable s C)
         s))
         
{: make}