1
1
using CSFML
2
2
using CSFML. LibCSFML
3
3
4
+ include (" Utils.jl" )
4
5
include (" Voxel.jl" )
5
6
using . Voxel: newVoxel
7
+ using . Utils: applyBetween
6
8
7
9
# enums of particle types, sand, stone, water, etc
8
10
const AIR = 0
@@ -14,6 +16,7 @@ selectedBlock = SAND
14
16
windowWidth = 640
15
17
windowHeight = 480
16
18
leftPressed = false
19
+ prevPosition = nothing ;
17
20
18
21
# TODO figure out how I want to handle resizing and map size in general
19
22
@@ -68,24 +71,38 @@ function handleResize()
68
71
return true
69
72
end
70
73
71
- function updateClickedPixel ()
74
+ function updateClickedPixel (prevPosition )
72
75
mousePos = sfMouse_getPositionRenderWindow (window)
73
76
if mousePos. x < 0 || mousePos. x >= windowWidth || mousePos. y < 0 || mousePos. y >= windowHeight
74
77
return
75
78
end
76
79
77
- # set boards voxel to new color
78
- board[mousePos. y+ 1 ][mousePos. x+ 1 ] = newVoxel (selectedBlock)
79
- if (selectedBlock == SAND)
80
- moveableMap[[mousePos. y + 1 , mousePos. x + 1 ]] = SAND
80
+ if prevPosition === nothing
81
+ global prevPosition = mousePos
82
+ else
83
+
84
+ end
85
+
86
+ function setPixel (point)
87
+ x = point[1 ]
88
+ y = point[2 ]
89
+ board[y+ 1 ][x+ 1 ] = newVoxel (selectedBlock)
90
+ if (selectedBlock == SAND)
91
+ moveableMap[[y + 1 , x + 1 ]] = SAND
92
+ end
81
93
end
94
+
95
+ applyBetween ([prevPosition. x, prevPosition. y], [mousePos. x, mousePos. y], setPixel)
96
+
97
+ global prevPosition = mousePos
82
98
end
83
99
84
100
function physicsTick (board)
85
101
# iterate over moveableMap and move particles down if air below them
86
102
for (i, j) in keys (moveableMap)
87
103
if i < windowHeight
88
104
down1 = board[i+ 1 ][j]
105
+ # TODO check against swappable blocks instead of air
89
106
if down1. type == AIR
90
107
board[i+ 1 ][j] = board[i][j]
91
108
board[i][j] = down1
@@ -110,16 +127,17 @@ function mouseHandler(event::sfEvent)
110
127
isLeft = sfMouseLeft == event. mouseButton. button
111
128
# rightPress = sfMouseRight == event.mouseButton.button
112
129
if event. type == sfEvtMouseButtonPressed && isLeft
113
- updateClickedPixel ()
130
+ updateClickedPixel (prevPosition )
114
131
global leftPressed = true
115
132
end
116
133
117
134
if event. type == sfEvtMouseMoved && leftPressed
118
- updateClickedPixel ()
135
+ updateClickedPixel (prevPosition )
119
136
end
120
137
121
138
if event. type == sfEvtMouseButtonReleased && isLeft
122
139
global leftPressed = false
140
+ global prevPosition = nothing
123
141
end
124
142
end
125
143
0 commit comments