summary refs log tree commit diff
path: root/lib/player.fnl
blob: d40e980f02bf2cb23677412400c08f14798038bf (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
(local entity (require :lib.entity))
(local vec (require :lib.vec))
(local cell (require :lib.cell))
(local cells (require :lib.cells))
(local bullet (require :lib.bullet))
(local music-state (require :lib.music-state))

(fn init [self pos]
  (setmetatable {entity.position pos
                 entity.velocity {:x 0 :y 0}
                 :target-spin 0
                 :shot-age 20
                 :direction 0}
                self))

(fn steer [self game id controls]
  (when (> self.shot-age 0)
    (set self.shot-age (- self.shot-age 1)))
  (set music-state.shot-age self.shot-age)
  ;; TODO: smooth turning
  (when controls.left
    (set self.target-spin (- self.target-spin 0.15)))
    ;; (set self.direction (% (- self.direction 0.1) (* math.pi 2)))
  (when controls.right
    (set self.target-spin (+ self.target-spin 0.15)))
  (set self.direction (% (+ self.direction (* self.target-spin 0.1)) (* math.pi 2)))
  (set self.target-spin (* self.target-spin 0.9))
  (set music-state.forward controls.up)
  (when controls.up
    (local v (entity.velocity self))
    (set v.x (+ v.x (* (math.cos self.direction) 0.02)))
    (set v.y (+ v.y (* (math.sin self.direction) 0.02)))
    (when (> (vec.mag v) 0.8)
      (tset self entity.velocity (vec.mul v (/ 0.8 (vec.mag v))))))
  (set music-state.speed (vec.mag (. self entity.velocity)))
  (when (and controls.shoot (= self.shot-age 0))
    (set self.shot-age 10)
    (local v (entity.velocity self))
    (table.insert
      game.entities
      (entity.init
        bullet
        (vec.clone (entity.position self))
        {:x (+ v.x (* (math.cos self.direction) 0.6))
         :y (+ v.y (* (math.sin self.direction) 0.6))}
        self.direction))))

(fn draw [self game id]
  (love.graphics.setColor 1 1 1)
  (love.graphics.rotate self.direction)
  (love.graphics.polygon :fill -0.6 -0.4 0.6 0 -0.6 0.4)
  (love.graphics.setLineWidth 0.1)
  ;; (love.graphics.setColor 1 1 1 0.4)
  ;; (love.graphics.arc :line :open 0 0 1.5 -0.3 0.3 13)
  ;; (love.graphics.line 1.4 0 1.6 0)
  (love.graphics.circle :fill 0.9 0 0.1))

(fn collide [self game id x y]
  (tset game.entities id nil)
  (tset game.grid x y (cell.init cells.boom)))

{entity.init init entity.steer steer entity.draw draw entity.collide collide}