Skip to content

Commit 1b4ffd4

Browse files
author
Patrick Sullivan
committed
resize camera with window; track key presses
1 parent b2b99fc commit 1b4ffd4

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

elm.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"Zinggi/elm-game-resources": "2.0.1",
1111
"elm/browser": "1.0.2",
1212
"elm/core": "1.0.5",
13-
"elm/html": "1.0.0"
13+
"elm/html": "1.0.0",
14+
"ohanhi/keyboard": "2.0.0"
1415
},
1516
"indirect": {
1617
"avh4/elm-color": "1.0.0",

src/Main.elm

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Game.TwoD.Render as Render exposing (Renderable)
99
import Html exposing (Html, a, div, input, text)
1010
import Html.Attributes exposing (class, placeholder, value)
1111
import Html.Events exposing (onClick, onInput)
12+
import Keyboard
13+
import Keyboard.Arrows
1214

1315

1416
main : Program ( Int, Int ) State Msg
@@ -22,13 +24,15 @@ main =
2224
Sub.batch
2325
[ Browser.Events.onResize ResizeWindow
2426
, Browser.Events.onAnimationFrameDelta Tick
27+
, Sub.map PressKeys Keyboard.subscriptions
2528
]
2629
}
2730

2831

2932
type alias State =
3033
{ time : Float -- time in ms
3134
, windowSize : ( Int, Int )
35+
, keys : List Keyboard.Key -- keys currently pressed
3236
, resources : Resources
3337
, serverUrl : String
3438
, loadable : Loadable GameState
@@ -66,6 +70,7 @@ init : ( Int, Int ) -> ( State, Cmd Msg )
6670
init windowSize =
6771
( { time = 0
6872
, windowSize = windowSize
73+
, keys = []
6974
, resources = Resources.init
7075
, serverUrl = ""
7176
, loadable = NotStarted
@@ -83,26 +88,28 @@ init windowSize =
8388

8489

8590
type Msg
86-
= Tick Float
87-
| ResizeWindow Int Int
88-
| LoadResources Resources.Msg
91+
= ChangeServerUrl String
8992
| Connect
90-
| ChangeServerUrl String
93+
| LoadResources Resources.Msg
94+
| PressKeys Keyboard.Msg
95+
| ResizeWindow Int Int
96+
| Tick Float
9197

9298

9399
update : Msg -> State -> ( State, Cmd Msg )
94100
update msg state =
95101
case msg of
96-
Tick dt ->
102+
ChangeServerUrl url ->
97103
{ state
98-
| time = state.time + dt
104+
| serverUrl = url
99105
}
100106
|> withNoCmd
101107

102-
ResizeWindow x y ->
108+
Connect ->
103109
{ state
104-
| windowSize = ( x, y )
110+
| loadable = Loaded testGameState
105111
}
112+
|> Debug.log "State"
106113
|> withNoCmd
107114

108115
LoadResources rMsg ->
@@ -111,16 +118,21 @@ update msg state =
111118
}
112119
|> withNoCmd
113120

114-
Connect ->
121+
PressKeys kMsg ->
115122
{ state
116-
| loadable = Loaded testGameState
123+
| keys = Keyboard.update kMsg state.keys
117124
}
118-
|> Debug.log "State"
119125
|> withNoCmd
120126

121-
ChangeServerUrl url ->
127+
ResizeWindow x y ->
122128
{ state
123-
| serverUrl = url
129+
| windowSize = ( x, y )
130+
}
131+
|> withNoCmd
132+
133+
Tick dt ->
134+
{ state
135+
| time = state.time + dt
124136
}
125137
|> withNoCmd
126138

@@ -184,11 +196,11 @@ gameView time windowSize camera resources cells agents =
184196
, size = windowSize
185197
, camera = camera
186198
}
187-
(render resources camera cells agents)
199+
(render resources cells agents)
188200

189201

190-
render : Resources -> Camera -> List Cell -> List Agent -> List Renderable
191-
render resources camera cells _ =
202+
render : Resources -> List Cell -> List Agent -> List Renderable
203+
render resources _ _ =
192204
[ Render.spriteWithOptions
193205
{ position = ( 0, 0, 0 )
194206
, size = ( 10, 5 )
@@ -210,7 +222,7 @@ testGameState =
210222
, mapHeight = 50
211223
, cells = testCells
212224
, agents = testAgents
213-
, camera = Camera.fixedWidth 8 ( 0, 0 )
225+
, camera = Camera.custom (\( w, h ) -> ( w / 100, h / 100 )) ( 0, 0 )
214226
}
215227

216228

0 commit comments

Comments
 (0)