dev #25
					 12 changed files with 158 additions and 9 deletions
				
			
		
							
								
								
									
										
											BIN
										
									
								
								assets/entities/player.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								assets/entities/player.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 1.5 KiB | 
							
								
								
									
										
											BIN
										
									
								
								assets/unkown.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								assets/unkown.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 870 B | 
|  | @ -7,7 +7,8 @@ data Game = Game { | ||||||
|     -- Current state of the game |     -- Current state of the game | ||||||
|     state   :: State, |     state   :: State, | ||||||
|     playing :: Level, |     playing :: Level, | ||||||
|     levels  :: [Level] |     levels  :: [Level], | ||||||
|  |     player  :: Player | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ------------------------------- Level -------------------------------- | ------------------------------- Level -------------------------------- | ||||||
|  | @ -30,9 +31,13 @@ data Physical = Void | ||||||
| 
 | 
 | ||||||
| ------------------------------- Player ------------------------------- | ------------------------------- Player ------------------------------- | ||||||
| 
 | 
 | ||||||
|  | type X = Int | ||||||
|  | type Y = Int | ||||||
|  | 
 | ||||||
| data Player = Player { | data Player = Player { | ||||||
|     playerHp  :: Maybe Int, |     playerHp  :: Maybe Int, | ||||||
|     inventory :: [Item] |     inventory :: [Item], | ||||||
|  |     coord     :: (X, Y) | ||||||
| } deriving (Eq, Show) | } deriving (Eq, Show) | ||||||
| 
 | 
 | ||||||
| instance Living Player where | instance Living Player where | ||||||
|  |  | ||||||
|  | @ -18,9 +18,10 @@ defaultEntity = Entity { | ||||||
| -- Initialize the game | -- Initialize the game | ||||||
| initGame :: Game | initGame :: Game | ||||||
| initGame = Game {  | initGame = Game {  | ||||||
|     state = defaultState, |     state   = defaultState, | ||||||
|     playing = defaultLevel, |     playing = defaultLevel, | ||||||
|     levels = [defaultLevel] |     levels  = [defaultLevel], | ||||||
|  |     player  = defaultPlayer | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| defaultItem :: Item | defaultItem :: Item | ||||||
|  | @ -51,8 +52,9 @@ defaultLevel = Level { | ||||||
| 
 | 
 | ||||||
| defaultPlayer :: Player | defaultPlayer :: Player | ||||||
| defaultPlayer = Player { | defaultPlayer = Player { | ||||||
|     playerHp = Prelude.Nothing, -- Compares to infinity |     playerHp  = Prelude.Nothing, -- Compares to infinity | ||||||
|     inventory = [] |     inventory = [], | ||||||
|  |     coord     = (0, 0) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| -- Default state of the game, Menu | -- Default state of the game, Menu | ||||||
|  |  | ||||||
|  | @ -7,6 +7,7 @@ module RPGEngine.Input | ||||||
| import RPGEngine.Data | import RPGEngine.Data | ||||||
| import RPGEngine.Data.State | import RPGEngine.Data.State | ||||||
| import RPGEngine.Input.Core | import RPGEngine.Input.Core | ||||||
|  | import RPGEngine.Input.Player | ||||||
| 
 | 
 | ||||||
| import Graphics.Gloss.Interface.IO.Game | import Graphics.Gloss.Interface.IO.Game | ||||||
| 
 | 
 | ||||||
|  | @ -22,7 +23,19 @@ handleAllInput ev g                         = handleAnyKey setNextState ev g | ||||||
| -- Input for 'Playing' state | -- Input for 'Playing' state | ||||||
| handlePlayInputs :: InputHandler Game | handlePlayInputs :: InputHandler Game | ||||||
| handlePlayInputs = composeInputHandlers [ | handlePlayInputs = composeInputHandlers [ | ||||||
|     handleKey (Char 'p') (\game -> game{ state = Pause }) |     -- Pause the game | ||||||
|  |     handleKey (Char 'p') (\game -> game{ state = Pause }), | ||||||
|  | 
 | ||||||
|  |     -- Player movement | ||||||
|  |     handleKey (SpecialKey KeyUp)    $ movePlayer North, | ||||||
|  |     handleKey (SpecialKey KeyRight) $ movePlayer East, | ||||||
|  |     handleKey (SpecialKey KeyDown)  $ movePlayer South, | ||||||
|  |     handleKey (SpecialKey KeyLeft)  $ movePlayer West, | ||||||
|  | 
 | ||||||
|  |     handleKey (Char 'w') $ movePlayer North, | ||||||
|  |     handleKey (Char 'd') $ movePlayer East, | ||||||
|  |     handleKey (Char 's') $ movePlayer South, | ||||||
|  |     handleKey (Char 'a') $ movePlayer West | ||||||
|     ] |     ] | ||||||
| 
 | 
 | ||||||
| -- Go to the next stage of the Game | -- Go to the next stage of the Game | ||||||
|  |  | ||||||
							
								
								
									
										19
									
								
								lib/RPGEngine/Input/Player.hs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								lib/RPGEngine/Input/Player.hs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,19 @@ | ||||||
|  | module RPGEngine.Input.Player  | ||||||
|  | ( movePlayer | ||||||
|  | ) where | ||||||
|  | 
 | ||||||
|  | import RPGEngine.Data (Game(..), Direction(..), Player(..), X, Y) | ||||||
|  | 
 | ||||||
|  | movePlayer :: Direction -> Game -> Game | ||||||
|  | movePlayer dir g@Game{ player = p@Player{ coord = (x, y) }} = newGame | ||||||
|  |     where newGame   = g{ player = newPlayer } | ||||||
|  |           newPlayer = p{ coord  = newCoord  } | ||||||
|  |           newCoord  = (x + xD, y + yD) | ||||||
|  |           (xD, yD)  = diffs dir | ||||||
|  | 
 | ||||||
|  | diffs :: Direction -> (X, Y) | ||||||
|  | diffs North  = (0, 1) | ||||||
|  | diffs East   = (1, 0) | ||||||
|  | diffs South  = (0, -1) | ||||||
|  | diffs West   = (-1, 0) | ||||||
|  | diffs Center = (0, 0) | ||||||
|  | @ -8,7 +8,15 @@ module RPGEngine.Render | ||||||
| ) where | ) where | ||||||
| 
 | 
 | ||||||
| import RPGEngine.Data | import RPGEngine.Data | ||||||
|  |     ( State(..), | ||||||
|  |       Game(..) ) | ||||||
|  | import RPGEngine.Render.Level  | ||||||
|  |     ( renderLevel ) | ||||||
|  | 
 | ||||||
| import Graphics.Gloss | import Graphics.Gloss | ||||||
|  |     ( white, pictures, text, Display(InWindow), Color, Picture ) | ||||||
|  | import RPGEngine.Render.Player (renderPlayer) | ||||||
|  | import RPGEngine.Render.GUI (renderGUI) | ||||||
| 
 | 
 | ||||||
| ----------------------------- Constants ------------------------------ | ----------------------------- Constants ------------------------------ | ||||||
| 
 | 
 | ||||||
|  | @ -36,9 +44,12 @@ render g@Game{ state = Lose    } = renderLose g | ||||||
| renderMenu :: Game -> Picture | renderMenu :: Game -> Picture | ||||||
| renderMenu _ = text "[Press any key to start]" | renderMenu _ = text "[Press any key to start]" | ||||||
| 
 | 
 | ||||||
| -- TODO |  | ||||||
| renderPlaying :: Game -> Picture | renderPlaying :: Game -> Picture | ||||||
| renderPlaying _ = text "Playing" | renderPlaying g@Game{ playing = lvl, player = player } = pictures [ | ||||||
|  |     renderLevel lvl, | ||||||
|  |     renderPlayer player, | ||||||
|  |     renderGUI g | ||||||
|  |     ] | ||||||
| 
 | 
 | ||||||
| -- TODO | -- TODO | ||||||
| renderPause :: Game -> Picture | renderPause :: Game -> Picture | ||||||
|  |  | ||||||
							
								
								
									
										63
									
								
								lib/RPGEngine/Render/Core.hs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								lib/RPGEngine/Render/Core.hs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,63 @@ | ||||||
|  | module RPGEngine.Render.Core where | ||||||
|  | 
 | ||||||
|  | import Graphics.Gloss ( Picture, translate ) | ||||||
|  | import GHC.IO (unsafePerformIO) | ||||||
|  | import Graphics.Gloss.Juicy (loadJuicyPNG) | ||||||
|  | import Data.Maybe (fromJust) | ||||||
|  | import Graphics.Gloss.Data.Picture (scale) | ||||||
|  | 
 | ||||||
|  | ----------------------------- Constants ------------------------------ | ||||||
|  | 
 | ||||||
|  | -- Default scale | ||||||
|  | zoom :: Float | ||||||
|  | zoom = 5.0 | ||||||
|  | 
 | ||||||
|  | -- Resolution of the texture | ||||||
|  | resolution :: Float | ||||||
|  | resolution = 16 | ||||||
|  | 
 | ||||||
|  | assetsFolder :: FilePath | ||||||
|  | assetsFolder = "assets/" | ||||||
|  | 
 | ||||||
|  | unknownImage :: FilePath | ||||||
|  | unknownImage = "unkown.png" | ||||||
|  | 
 | ||||||
|  | allEntities :: [(String, FilePath)] | ||||||
|  | allEntities = [ | ||||||
|  |     ("player", "player.png"), | ||||||
|  |     ("door", "door.png") | ||||||
|  |     ] | ||||||
|  | 
 | ||||||
|  | allItems :: [(String, FilePath)] | ||||||
|  | allItems = [ | ||||||
|  |     ("dagger", "dagger.png"), | ||||||
|  |     ("key",    "key.png"   ) | ||||||
|  |     ] | ||||||
|  | 
 | ||||||
|  | -- Map of all renders | ||||||
|  | library :: [(String, Picture)] | ||||||
|  | library = unknown:entities ++ environment ++ gui ++ items | ||||||
|  |     where unknown     = ("unkown", renderPNG (assetsFolder ++ unknownImage)) | ||||||
|  |           entities    = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "entities/" ++ s))) allEntities | ||||||
|  |           environment = [] | ||||||
|  |           gui         = [] | ||||||
|  |           items       = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "items/" ++ s))) allItems | ||||||
|  | 
 | ||||||
|  | ---------------------------------------------------------------------- | ||||||
|  | 
 | ||||||
|  | -- Turn a path to a .png file into a Picture. | ||||||
|  | renderPNG :: FilePath -> Picture | ||||||
|  | renderPNG path = scale zoom zoom $ fromJust $ unsafePerformIO $ loadJuicyPNG path | ||||||
|  | 
 | ||||||
|  | -- Retrieve an image from the library. If the library does not contain | ||||||
|  | -- the requested image, a default is returned. | ||||||
|  | getRender :: String -> Picture | ||||||
|  | getRender id = get filtered | ||||||
|  |     where filtered         = filter ((== id) . fst) library | ||||||
|  |           get []           = snd $ head library | ||||||
|  |           get ((_, res):_) = res | ||||||
|  | 
 | ||||||
|  | setRenderPos :: Int -> Int -> Picture -> Picture | ||||||
|  | setRenderPos x y = translate floatX floatY | ||||||
|  |     where floatX = fromIntegral x * zoom * resolution | ||||||
|  |           floatY = fromIntegral y * zoom * resolution | ||||||
							
								
								
									
										10
									
								
								lib/RPGEngine/Render/GUI.hs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								lib/RPGEngine/Render/GUI.hs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,10 @@ | ||||||
|  | module RPGEngine.Render.GUI  | ||||||
|  | ( renderGUI | ||||||
|  | ) where | ||||||
|  | 
 | ||||||
|  | import RPGEngine.Data (Game) | ||||||
|  | import Graphics.Gloss (Picture, blank) | ||||||
|  | 
 | ||||||
|  | -- TODO | ||||||
|  | renderGUI :: Game -> Picture | ||||||
|  | renderGUI _ = blank | ||||||
							
								
								
									
										10
									
								
								lib/RPGEngine/Render/Level.hs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								lib/RPGEngine/Render/Level.hs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,10 @@ | ||||||
|  | module RPGEngine.Render.Level  | ||||||
|  | ( renderLevel | ||||||
|  | ) where | ||||||
|  | 
 | ||||||
|  | import Graphics.Gloss | ||||||
|  | import RPGEngine.Data | ||||||
|  | 
 | ||||||
|  | -- TODO | ||||||
|  | renderLevel :: Level -> Picture | ||||||
|  | renderLevel _ = text "Level" | ||||||
							
								
								
									
										11
									
								
								lib/RPGEngine/Render/Player.hs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								lib/RPGEngine/Render/Player.hs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,11 @@ | ||||||
|  | module RPGEngine.Render.Player  | ||||||
|  | ( renderPlayer | ||||||
|  | ) where | ||||||
|  | 
 | ||||||
|  | import RPGEngine.Data (Player(..)) | ||||||
|  | 
 | ||||||
|  | import Graphics.Gloss (Picture, text) | ||||||
|  | import RPGEngine.Render.Core (getRender, setRenderPos) | ||||||
|  | 
 | ||||||
|  | renderPlayer :: Player -> Picture | ||||||
|  | renderPlayer Player{ coord = (x, y) } = setRenderPos x y $ getRender "player" | ||||||
|  | @ -19,6 +19,7 @@ library | ||||||
| 
 | 
 | ||||||
|     RPGEngine.Input |     RPGEngine.Input | ||||||
|     RPGEngine.Input.Core |     RPGEngine.Input.Core | ||||||
|  |     RPGEngine.Input.Player | ||||||
| 
 | 
 | ||||||
|     RPGEngine.Parse |     RPGEngine.Parse | ||||||
|     RPGEngine.Parse.Core |     RPGEngine.Parse.Core | ||||||
|  | @ -26,6 +27,10 @@ library | ||||||
|     RPGEngine.Parse.StructElement |     RPGEngine.Parse.StructElement | ||||||
| 
 | 
 | ||||||
|     RPGEngine.Render |     RPGEngine.Render | ||||||
|  |     RPGEngine.Render.Core | ||||||
|  |     RPGEngine.Render.GUI | ||||||
|  |     RPGEngine.Render.Level | ||||||
|  |     RPGEngine.Render.Player | ||||||
| 
 | 
 | ||||||
| executable rpg-engine | executable rpg-engine | ||||||
|   main-is: Main.hs |   main-is: Main.hs | ||||||
|  |  | ||||||
		Reference in a new issue