-
Notifications
You must be signed in to change notification settings - Fork 1
/
inventory.rkt
155 lines (140 loc) · 6.15 KB
/
inventory.rkt
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
#lang racket/gui
(require sgl/gl
sgl/gl-vectors
racket/mpair
"drawtext.rkt")
(provide Inventory%)
;------------------------------------------------------------------------------
;Class: Inventory%
;Description: The class representing the player's inventory.
;------------------------------------------------------------------------------
(define Inventory%
(class object%
(super-new)
(init-field width height)
(field (grid (make-vector height))
(cursorx 0)
(cursory 0))
;------------------------------------------------------------------------------
;gridloop: a short looping method to initialize the inventory grid with unique rows. We must
; use this approach since initializing grid to (make-vector height (make-vector width #f)) creates
; a grid with the _SAME_ object in all the rows, and mutating one will mutate all the others.
;params:
; rownum - the number of rows passed.
;------------------------------------------------------------------------------
(define/private (gridloop rownum)
(when (< rownum height)
(vector-set! grid rownum (make-vector width #f))
(gridloop (+ rownum 1))))
(gridloop 0)
;------------------------------------------------------------------------------
;add-thing: adds an object to the inventory.
;params:
; thing - the thing to be added.
;------------------------------------------------------------------------------
(define/public (add-thing! thing world)
(set-field! place thing 'Inventory)
(display "in add-thing!")
(update-inventory world))
;------------------------------------------------------------------------------
;get-thing: gets an object from the inventory.
;params:
;------------------------------------------------------------------------------
(define/public (get-thing)
(vector-ref (vector-ref grid cursory) cursorx))
;------------------------------------------------------------------------------
;update-inventory: Updates the inventory grid with data from the inventory's list of things.
;------------------------------------------------------------------------------
(define/private (update-inventory world)
(let ((things '()))
(define (add-loop mlst)
(if (null? mlst)
(void)
(if (eqv? (get-field place (mcar mlst)) 'Inventory)
(begin (set! things (mcons (mcar mlst) things))
(display "Added thing to inventory")
(add-loop (mcdr mlst)))
(add-loop (mcdr mlst)))))
(add-loop (get-field things world))
(let ((number-of-things (mlength things))
(thingcounter 0))
(define (yloop rownum)
(when (< rownum height)
(define row (vector-ref grid rownum))
(define (xloop colnum)
(when (< colnum width)
(if (< thingcounter number-of-things)
(let ((thing (mlist-ref things thingcounter)))
(vector-set! row colnum thing)
(set! thingcounter (+ thingcounter 1)))
(begin
(vector-set! row colnum #f)))
(xloop (+ colnum 1))))
(xloop 0)
(vector-set! grid rownum row)
(yloop (+ rownum 1))))
(yloop 0))))
;------------------------------------------------------------------------------
;action: Reacts to player input and moves the current position in the grid.
;params:
; direction - the direction in which to move the cursor, or another command.v
;------------------------------------------------------------------------------
(define/public (action direction)
(case direction
((up) (unless (= cursory 0)
(set! cursory (- cursory 1))))
((down) (unless (= cursory (- height 1))
(set! cursory (+ cursory 1))))
((left) (unless (= cursorx 0)
(set! cursorx (- cursorx 1))))
((right) (unless (= cursorx (- width 1))
(set! cursorx (+ cursorx 1))))))
;------------------------------------------------------------------------------
;delete-thing!: Moves a thing from the inventory to Limbo and updates inventory.
;params:
; thing - the thing to be deleted.
;------------------------------------------------------------------------------
(define/public (delete-thing! thing world)
(set-field! place thing 'Limbo)
(update-inventory world))
;------------------------------------------------------------------------------
;draw: Draws the inventory on the screen.
;params:
; texture-list the global texture list passed on from the caller
;------------------------------------------------------------------------------
(define/public (draw texture-list)
(glDisable GL_TEXTURE_2D)
(define (yloop row)
(define (xloop col)
(when (< col width)
(let ((thing (vector-ref (vector-ref grid row) col)))
(glPushMatrix)
(glTranslatef (* col 100) (* row 50) 0)
(glColor4f 0 0 0 0)
(glBegin GL_TRIANGLE_STRIP)
(glVertex2i 0 0)
(glVertex2i 100 0)
(glVertex2i 0 50)
(glVertex2i 100 50)
(glEnd)
(if (and (= cursorx col) (= cursory row))
(glColor3f 0.7 0.7 0.65)
(glColor3f 0.5 0.5 0.45))
(glBegin GL_TRIANGLE_STRIP)
(glVertex2i 0 0)
(glVertex2i 99 0)
(glVertex2i 0 49)
(glVertex2i 99 49)
(glEnd)
(if (not (eq? thing #f))
(begin
(glColor3f 1 1 1)
(draw-text 10 10 0.5 (get-field inv-name thing) texture-list))
(void))
(glPopMatrix))
(xloop (+ col 1))))
(when (< row height)
(xloop 0)
(yloop (+ row 1))))
(yloop 0)
(glEnable GL_TEXTURE_2D))))