0
私は2つの「船」でゲームを作りたいと思っています。ELM:ゲーム要素が互いに衝突するのを防ぐ/
船は、上下左右に移動できます。一方の船は矢印で制御され、もう一方の船はWASDによって制御されます。
しかし、私は船が互いの上に配置されないようにしたいと思います。
私の例では、青い船は赤い船の上に乗ってはいけません。赤い船が右に動いて青い船に当たったら、船は動かないだろうと私は期待しています。
何か助けていただければ幸いです。
module Game (..) where
import Graphics.Element exposing (..)
import Graphics.Collage exposing (..)
import Color exposing (red, blue, gray, green)
import Keyboard
import Window
-- ALIAS
type alias Model =
{ color : Color.Color
, isFiring : Bool
, name : String
, y : Int
, x : Int
}
-- MODEL
initialShip : String -> Model
initialShip name =
let
color =
if name == "ship1" then
red
else
blue
in
{ color = color
, isFiring = False
, name = name
, y = 0
, x = 0
}
-- POSITIONS
moveLeft : Model -> Model
moveLeft model =
{ model | x = model.x - 1 }
moveRight : Model -> Model
moveRight model =
{ model | x = model.x + 1 }
moveDown : Model -> Model
moveDown model =
{ model | y = model.y - 1 }
moveUp : Model -> Model
moveUp model =
{ model | y = model.y + 1 }
-- ACTIONS
type Action
= NoOp
| Left
| Right
| Down
| Up
-- UPDATE
update : Action -> Model -> Model
update action model =
case action of
NoOp ->
model
Left ->
moveLeft model
Right ->
moveRight model
Down ->
moveDown model
Up ->
moveUp model
-- View
drawGame : Float -> Float -> Form
drawGame w h =
rect w h
|> filled gray
drawShip : Float -> Model -> Form
drawShip gameHeight ship =
let
shipColor =
if ship.isFiring then green else ship.color
initialPosition =
if ship.name == "ship1" then
(toFloat (ship.x - 50))
else
(toFloat (ship.x + 50))
in
ngon 3 30
|> filled shipColor
|> rotate (degrees 90)
|> move (initialPosition, (toFloat ship.y + 50))
view : (Int, Int) -> Model -> Model -> Element
view (w, h) ship1 ship2 =
let
(w', h') = (toFloat w, toFloat h)
in
collage w h
[ drawGame w' h'
, drawShip h' ship1
, drawShip h' ship2
]
-- SIGNALS
direction : Signal { x : Int, y : Int } -> Signal Action
direction input =
let
position =
Signal.map (\{ x, y } -> { x = x, y = y }) input
delta =
Time.fps 120
toAction { x, y } =
if x < 0 then
Left
else if x > 0 then
Right
else if y < 0 then
Down
else if y > 0 then
Up
else
NoOp
actions =
Signal.map toAction position
in
Signal.sampleOn delta actions
ship1 : Signal Model
ship1 =
Signal.foldp update (initialShip "ship1") (direction Keyboard.wasd)
ship2 : Signal Model
ship2 =
Signal.foldp update (initialShip "ship2") (direction Keyboard.arrows)
-- MAIN
main : Signal Element
main =
Signal.map3 view Window.dimensions ship1 ship2