-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.lisp
220 lines (198 loc) · 8.36 KB
/
map.lisp
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
220
(defpackage :map
(:use :cl)
(:shadow :map :load)
(:nicknames :m)
(:export :tile
:solid-tile
:portal
:map
:exit
:width :height
:portals
:get-tile
:tile-at
:draw
:portal-exit-map
:delete-portal
:draw-portals
:draw-portals-with
:default-map
:default-tiles
:load
:save
:relink-portals))
(in-package :map)
(defvar *tile-width* 64)
(defvar *tile-height* 64)
(defclass tile (s:image)
())
(defclass solid-tile (tile)
())
(defclass portal (u:rectangle)
((exit :initarg :exit :accessor exit))
(:default-initargs
:width 64
:height 128))
(defclass map ()
((tiles :accessor tiles)
(background :initform nil :initarg :background :accessor background)
(portals :initform nil :accessor portals :documentation "A portal links maps together.")
(things :accessor things)))
(defmethod width ((map map))
(array-dimension (tiles map) 1))
(defmethod height ((map map))
(array-dimension (tiles map) 0))
(defmethod draw ((map map) ofs-x ofs-y width height)
(labels ((bound (n dim)
(max 0 (min n (array-dimension (tiles map) dim)))))
(let ((start-x (floor ofs-x *tile-width*))
(start-y (floor ofs-y *tile-height*))
(ofsx (- (mod ofs-x *tile-width*)))
(ofsy (- (mod ofs-y *tile-width*)))
(w (truncate width *tile-width*))
(h (truncate height *tile-height*)))
(when (background map)
(sdl:draw-surface (background map)))
(loop for tile-y from start-y below (bound (+ start-y h 1) 0)
for y from ofsy by *tile-height* do
(loop for tile-x from start-x below (bound (+ start-x w 1) 1)
for x from ofsx by *tile-width* do
(when (and (>= tile-x 0) (>= tile-y 0)
(aref (tiles map) tile-y tile-x))
(s:draw (aref (tiles map) tile-y tile-x) (u:pt x y))))))))
(defun portal-exit-map (portal maps)
(loop for m across maps when (and m (find (exit portal) (portals m))) return m))
(defun delete-portal (portal maps)
(loop for m across maps
when m do
(when (find portal (portals m))
(setf (portals m) (delete portal (portals m))))
(loop for i in (portals m)
when (eq (exit i) portal)
do (setf (exit i) nil))))
(defun draw-portals-with (portals animation ofs-x ofs-y width height)
(loop for i in portals
with view = (u:rect ofs-x ofs-y width height)
when (u:collide i view) do
(s:draw (s::current-image animation 0) (u:sub i (u:pt ofs-x ofs-y)))))
(defun draw-portals (portals current maps font ofs-x ofs-y width height)
(loop for i in portals
for map = (loop for m across maps when (and m (find (exit i) (portals m))) return m)
for c from 0
for color = (if (eq i current)
(u:color 255 0 0)
(u:gray 255))
with view = (u:rect ofs-x ofs-y width height)
when (u:collide i view) do
(sdl-gfx:draw-rectangle-* (- (u:x i) ofs-x)
(- (u:y i) ofs-y)
(u:width i)
(u:height i)
:color color)
(let* ((txt (format nil "~d: ~d|~d"
c (position map maps)
(when map (position (exit i) (portals map)))))
(w (sdl:get-font-size txt :size :w :font font))
(h (sdl:get-font-height :font font)))
(sdl:draw-string-blended-* txt
(+ (- (u:x i) ofs-x)
(u:half (- (u:width i) w)))
(+ (- (u:y i) ofs-y)
(u:half (- (u:height i) h)))
:font font
:color color))))
(defmethod u:dump-object append ((object portal) &optional extra)
(let ((m (portal-exit-map object extra)))
(when m
(list :exit (list (position m extra) (position (exit object) (portals m)))))))
(defun default-tiles ()
(labels ((tile (file solid)
(make-instance (if solid 'solid-tile 'tile) :surface (sdl-image:load-and-convert-image file))))
(list
(tile (u:resource "block" "png") t)
(tile (u:resource "pillar" "png") nil)
(tile (u:resource "water" "png") nil)
(tile (u:resource "blue bricks" "png") nil)
(tile (u:resource "bb-left" "png") nil)
(tile (u:resource "bb-right" "png") nil)
;;(tile (u:resource "bb-bottom" "png") nil)
(tile (u:resource "spikes" "png") nil)
(tile (u:resource "stairs-left" "png") nil)
(tile (u:resource "stairs-right" "png") nil))))
(defun default-map (width height background tiles)
(let ((map (make-instance 'map
:background background)))
(setf (tiles map) (make-array (list height width) :initial-element nil))
(loop for h below height do
(loop for w below width do
(setf (aref (tiles map) h w)
(if (or (= h 0)
(= w 0)
(= w (1- width))
(= h (1- height)))
(first tiles)
nil))))
(setf (portals map) (list (make-instance 'portal :exit nil
:x 0 :y 0 :height (* height *tile-height*))
(make-instance 'portal :exit nil
:x (- (* width *tile-width*) *tile-width*)
:y 0
:height (* height *tile-height*))
(make-instance 'portal :exit nil
:x *tile-width* :y 0
:height *tile-height*
:width (- (* width *tile-width*)
(* 2 *tile-width*)))
(make-instance 'portal :exit nil
:x *tile-width* :y (- (* height *tile-height*)
*tile-height*)
:height *tile-height*
:width (- (* width *tile-width*)
(* 2 *tile-width*)))))
map))
(defun get-tile (map x y)
(when (and (<= 0 x (1- (array-dimension (tiles map) 1)))
(<= 0 y (1- (array-dimension (tiles map) 0))))
(aref (tiles map) y x)))
(defun tile-at (map x y)
(let ((x (truncate x *tile-width*))
(y (truncate y *tile-height*)))
(when (and (<= 0 x (1- (array-dimension (tiles map) 1)))
(<= 0 y (1- (array-dimension (tiles map) 0))))
(aref (tiles map) y x))))
(defun (setf tile-at) (tile map x y)
(let ((x (truncate x *tile-width*))
(y (truncate y *tile-height*)))
(when (and (<= 0 x (1- (array-dimension (tiles map) 1)))
(<= 0 y (1- (array-dimension (tiles map) 0))))
(setf (aref (tiles map) y x) tile))))
(defmethod save ((map map) maps stream tiles)
(terpri stream)
(write-line ";; map" stream)
(write (loop for y below (height map)
collect (loop for x below (width map)
collect (position (get-tile map x y) tiles)))
:stream stream
:lines nil :level nil :length nil)
(terpri stream)
(write-line ";; portals" stream)
(write (mapcar (lambda (p) (u:dump-object p maps)) (portals map))
:stream stream
:lines nil :level nil :length nil))
(defmethod load ((map map) stream tiles)
(let* ((list (read stream))
(portals (read stream))
(height (length list))
(width (length (first list)))
(array (make-array (list height width) :initial-element nil)))
(loop for y below height
for i in list do
(loop for x below width
for j in i do
(setf (aref array y x) (and j (elt tiles j)))))
(setf (tiles map) array
(portals map) (loop for i in portals
collect (u:load-object i)))))
(defun relink-portals (map maps)
(dolist (i (portals map))
(setf (exit i) (elt (portals (elt maps (first (exit i)))) (second (exit i))))))