aboutsummaryrefslogtreecommitdiff
path: root/src/macroknight/game.hy
blob: 7227f7658175c9689ae68dcca09516fcb02c9ddd (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
(require hy)
(require hyrule *)
(import pygame
        pytmx.util_pygame [load_pygame]
        pprint [pprint])

(pygame.init)


(setv TILE_SCALING 1)
(setv TILE_SIZE (* TILE_SCALING 16))
(setv GRAVITY 5)

(defn neg [value]
  (* -1 value))

(defn invert [move]
  #((neg (get move 0))
     (neg (get move 1))))

(defn merge-moves [moves]
  (let [end-move #(0 0)]
    (for [move moves]
      (setv end-move
            #((+ (get end-move 0) (get move 0))
               (+ (get end-move 1) (get move 1)))))
    end-move))

(defclass TileSet []
  (defn __init__ [self image tile-w tile-h [padding 0]]

    (setv self.sheet
          (let [surf (pygame.image.load "assets/monochrome-transparent.png")]
            (if (!= TILE_SCALING 1)
                (pygame.transform.scale
                  surf
                  #((* (.get_width surf) TILE_SCALING)
                     (* (.get_height surf) TILE_SCALING)))
                surf)))

    (setv self.tiles
          (lfor y (range 0 (.get_height self.sheet) (+ tile-h (* padding TILE_SCALING)))
                x (range 0 (.get_width self.sheet) (+ tile-w (* padding TILE_SCALING)))
                (let [tile (pygame.Surface #(tile-w tile-h))]
                  (.blit tile self.sheet #(0 0) #(x y tile-w tile-h))
                  tile)))))


;; Define systems here
(defn apply-gravity [entity entities]
  (when (not entity.fixed)
    (.move entity #(0 GRAVITY))))

(defn apply-collisions [entity entities]
  (for [ent entities]
    (when (and (!= ent.id entity.id)
               (.colliderect entity.rect ent.rect)
               (not entity.fixed))
      (let [collision-rect (.clip entity.rect ent.rect)
            move-x (get entity.total-move 0)
            move-y (get entity.total-move 1)]
        (when (!= move-x 0)
          (.move entity #((* (if (> move-x 0) -1 1) collision-rect.width) 0)))
        (when (!= move-y 0)
          (.move entity #(0 (* (if (> move-y 0) -1 1) collision-rect.height)))))
      (.ground entity))))

;; Define entities here
(defclass LevelTile []
  (setv fixed True)

  (defn __init__ [self id x y surf]
    (setv self.id id)
    (setv self.surf
          (if (!= TILE_SCALING 1)
              (pygame.transform.scale
                surf
                #((* (.get_width surf) TILE_SCALING)
                   (* (.get_height surf) TILE_SCALING)))
              surf))

    (setv self.rect (.get_rect surf :left (* x TILE_SIZE) :top (* y TILE_SIZE)))))

(defclass MiniSprite [pygame.sprite.Sprite]
  (defn __init__ [self tile x y]
    (.__init__ (super))
    (setv self.surf (pygame.Surface #(TILE_SIZE TILE_SIZE)))
    (.blit self.surf tile #(0 0))
    (setv self.rect (.get_rect self.surf :left (* x TILE_SIZE) :top (* y TILE_SIZE)))))

(defclass Player [pygame.sprite.Sprite]
  (setv fixed False)
  (setv SPEED 3)
  (setv JUMP_IMPULSE 10)
  (setv MAX_JUMPING 100)

  (defn __init__ [self tile initial-x initial-y]
    (.__init__ (super))
    (setv self.id 1)
    (setv self.surf (pygame.Surface #(TILE_SIZE TILE_SIZE)))
    (setv self.jumping False)
    (setv self.jump_move 0)
    (.blit self.surf tile #(0 0))
    (setv self.rect (.get_rect self.surf :left (* 5 TILE_SIZE))))

  (defn move [self move]
    (.append self.moves move)
    (.move_ip self.rect (get move 0) (get move 1)))

  (defn [property] total-move [self]
    (merge-moves self.moves))

  (defn jump [self]
    (setv self.jumping True)
    (when (< self.jump_move self.MAX_JUMPING)
      (setv self.jump_move (+ self.jump_move self.JUMP_IMPULSE))
      (.move self #(0 (neg self.JUMP_IMPULSE)))))

  (defn ground [self]
    (setv self.jump_move 0))

  (defn flush [self]
    (setv self.moves [])))

(setv screen (pygame.display.set_mode #((* TILE_SCALING 640) (* TILE_SCALING 480))))
(setv clock (pygame.time.Clock))
(setv running True)

(setv tiles (TileSet "assets/monochrome-transparent.png" TILE_SIZE TILE_SIZE 1))

(setv player (Player (get tiles.tiles 28) 0 0))
(setv sprites-group [])
(.append sprites-group player)
(setv level (load_pygame "assets/level-1.tmx"))
(setv macro-input-mode False)
(setv macro-commands [None None None])

(for [tiledef (enumerate (.tiles (get level.layers 0)))]
  (.append sprites-group (LevelTile (get tiledef 0) #* (get tiledef 1))))

(setv ongoing_inputs [])

(defn draw-tile [target tile-id x y]
  (let [tile (MiniSprite (get tiles.tiles tile-id) x y)]
    (.blit target tile.surf tile.rect)))

(while running
  (for [event (pygame.event.get)]
    (case event.type
          pygame.QUIT (setv running False)
          pygame.KEYDOWN (if (= event.key pygame.K_ESCAPE)
                             (setv running False)
                             (if macro-input-mode
                                 (when (in event.key [pygame.K_a pygame.K_w pygame.K_a pygame.K_s pygame.K_d])
                                   (print event.key)
                                   (setv (get macro-commands (.index macro-commands None)) event.key))
                                 (case event.key
                                       pygame.K_SPACE (setv macro-input-mode True)
                                       else (.append ongoing_inputs event.key))))
          pygame.KEYUP (when (in event.key ongoing_inputs)
                         (.remove ongoing_inputs event.key))))

  (.fill screen "#000000")

  (if macro-input-mode
      ;; If the commands list is full
      (if (get macro-commands -1)
          ;; Process commands
          (do
            (pprint (lfor command macro-commands :if command command))
            (let [#(command-id command) (get (lfor command (enumerate macro-commands) :if (get command 1) command) 0)]
              (case command
                    pygame.K_a (.move player #((neg TILE_SIZE) 0))
                    pygame.K_s (.move player #(0 1))
                    pygame.K_w (.move player #(0 (neg player.MAX_JUMPING)))
                    pygame.K_d (.move player #(TILE_SIZE 0)))
              (if (= command-id (- (len macro-commands) 1))
                  (do  (setv macro-commands [None None None])
                       (setv macro-input-mode False))
                  (setv (get macro-commands command-id) None))))

          (do (draw-tile screen 774 3 1)
              (for [#(num command) (enumerate macro-commands)]
                (let [x-pos (+ 4 num)]
                  (case command
                        pygame.K_w (draw-tile screen 1057 x-pos 1)
                        pygame.K_d (draw-tile screen 1058 x-pos 1)
                        pygame.K_s (draw-tile screen 1059 x-pos 1)
                        pygame.K_a (draw-tile screen 1060 x-pos 1))))))

      (do (for [inp ongoing_inputs]
            (case inp
                  pygame.K_a (.move player #((neg player.SPEED) 0))
                  pygame.K_s (.move player #(0 1))
                  pygame.K_w (.jump player)
                  pygame.K_d (.move player #(player.SPEED 0))))

          (when (any ongoing_inputs)
            (for [sprite sprites-group]
              (apply-collisions sprite sprites-group)))

          (.flush player)

          ;; Apply systems
          (for [sprite sprites-group]
            (apply-gravity sprite sprites-group)
            (apply-collisions sprite sprites-group))

          (.flush player)))


  (for [sprite sprites-group]
    (.blit screen sprite.surf sprite.rect))

  (pygame.display.flip)
  (.tick clock 60)
  )

(pygame.quit)