aboutsummaryrefslogtreecommitdiff
path: root/src/macroknight/tiles.hy
blob: 81e72e57cb1db3193c35abd707577fb24b42ab0e (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
;;; Copyright (C) 2025 Gene Pasquet
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(import pygame)

(defclass TileSet []
  (defn __init__ [self image-file scaling tile-w tile-h [padding 0]]
    (setv self.tile-w tile-w)
    (setv self.tile-h tile-h)
    (setv self.scaling scaling)

    (setv self.sheet
          (let [surf (pygame.image.load image-file)
                map-width (* (.get_width surf) self.scaling)
                map-height (* (.get_height surf) self.scaling)]
            (if (!= self.scaling 1)
                (pygame.transform.scale surf #(map-width map-height))
                surf)))

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

(defclass MiniSprite [pygame.sprite.Sprite]
  (defn __init__ [self tile tile-size x y [goal False]]
    (.__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)))
    (setv self.goal goal)))

(defn draw-tile [target tileset tile-id #* args #** kwargs]
  (let [tile (get tileset.tiles tile-id)
        sprite (MiniSprite tile tileset.tile-w #* args #** kwargs)]
    (.blit target sprite.surf sprite.rect)
    sprite))