Skip to content

Commit 303d75f

Browse files
jsjs
js
authored and
js
committed
Misc work, going for a towns clone instead
1 parent a6de592 commit 303d75f

12 files changed

+109
-58
lines changed

Agents.hs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Agents where
2+
3+
import Graphics.UI.SDL as SDL
4+
import Graphics.UI.SDL.Image as SDLi
5+
6+
import Model
7+
8+
initAgents :: IO[Agent]
9+
initAgents = do
10+
agentSprite <- SDLi.load "image/agent.png"
11+
return [Agent "A" (Position 10 10) agentSprite, Agent "B" (Position 20 20) agentSprite]
12+

Draw.hs

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ module Draw where
33
import Model
44
import DrawWorldMap
55
import DrawFight
6+
import DrawMenu
67

7-
drawGamestate :: GameState -> IO ()
8-
drawGamestate gs
9-
| gameMode gs == Model.Walking = drawWalkingMode gs
10-
| gameMode gs == Model.Fight = drawFight gs
11-
| gameMode gs == Model.AfterFight = return ()
8+
drawGamestate :: GameState -> Mode -> IO ()
9+
drawGamestate gs Model.Walking = do
10+
drawWalkingMode gs
11+
drawMenu gs (hasMenu gs)
12+
13+
drawGamestate gs Model.AfterFight = do
14+
return ()
15+
16+
drawGamestate gs Model.Fight = do
17+
drawFight gs
18+
drawMenu gs (hasMenu gs)

DrawFight.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ drawFight gs = do
3434
blitAnimations (map curAnimation (enemies gs)) s (Position 0 0)
3535

3636
blitAnimations (animations gs) s (Position 0 0)
37-
drawMenu gs s
37+
--drawMenu gs s
3838
SDL.flip s

DrawMenu.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ drawLabels (x:xs) pos s fnt = do
1818
blitSurface title Nothing s (Just (Rect (floor $ xVal pos) (floor $ yVal pos) 200 200))
1919
drawLabels xs (Position ((xVal pos)) ((yVal pos) + 30)) s fnt
2020

21-
drawMenu :: GameState -> Surface -> IO ()
22-
drawMenu gs s = do
21+
drawMenu :: GameState -> Bool -> IO ()
22+
drawMenu gs True = do
23+
s <- getVideoSurface
2324
let menu' = menu gs
2425
let pos' = menuPos menu'
2526
let gx' = gx gs
@@ -28,3 +29,5 @@ drawMenu gs s = do
2829
drawLabels (labels menu') (Position (((xVal pos') +10.0)) (((yVal pos') + 10.0))) s (fnt gs)
2930

3031
return ()
32+
33+
drawMenu _ _ = return ()

DrawWorldMap.hs

+11
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ drawMap m s tileSurface cameraPos = do
4141
drawTiles coords tileData s tileSurface cameraPos
4242
return ()
4343

44+
drawAgents :: [Agent] -> Surface -> Position -> IO()
45+
drawAgents [] _ _ = return ()
46+
drawAgents (x:xs) s camera = do
47+
let xpos = xVal $ agentPos x
48+
let ypos = yVal $ agentPos x
49+
let x' = floor (xpos - (xVal camera))
50+
let y' = floor (ypos - (yVal camera))
51+
blitSurface (agentImage x) Nothing s (Just (Rect x' y' 32 32))
52+
drawAgents xs s camera
53+
4454
drawWalkingMode :: GameState -> IO ()
4555
drawWalkingMode gs = do
4656
s <- getVideoSurface
4757
drawMap (currentMap gs) s (tileSurface $ gx gs) (cameraPos gs)
4858
blitAnimations ((animation (player gs)) : animations gs) s (cameraPos gs)
59+
drawAgents (agents gs) s (cameraPos gs)
4960
SDL.flip s

LogicWalking.hs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module LogicWalking where
2+
3+
import Model
4+
import Data.Word
5+
import Animation
6+
import System.Random
7+
8+
setUpNextFight :: GameState -> Int -> GameState
9+
setUpNextFight gs t = gs {rng = rng', nextFight = nextFight'}
10+
where (val, rng') = next $ rng gs
11+
nextFight' = val `mod` 1000 + t + 2000
12+
13+
nextPlayerPos :: Player -> Word32 -> Position
14+
nextPlayerPos player dt
15+
| speed player == Slow && moveDirection player == Model.Right = Position (x0 + slowSpeed*(fromIntegral dt)) y0
16+
| speed player == Slow && moveDirection player == Model.Left = Position (x0 - slowSpeed*(fromIntegral dt)) y0
17+
| speed player == Slow && moveDirection player == Model.Up = Position x0 (y0 - slowSpeed*(fromIntegral dt))
18+
| speed player == Slow && moveDirection player == Model.Down = Position x0 (y0 + slowSpeed*(fromIntegral dt))
19+
| otherwise = playerPos player
20+
where x0 = xVal $ playerPos player
21+
y0 = yVal $ playerPos player
22+
slowSpeed = 0.2
23+
24+
updatePlayer :: Player -> Word32 -> Word32 -> Player
25+
updatePlayer player t dt = player { animation = animation', playerPos = playerPos' }
26+
where animation' = (head (updateAnimations [animation player] t)) { animPos = playerPos player}
27+
playerPos' = nextPlayerPos player dt
28+
29+
posDiff :: Float -> Float -> Float -> Float -> Float
30+
posDiff minval maxval a b
31+
| (b-a) < minval = b - minval
32+
| (b-a) > maxval = b - maxval
33+
| otherwise = a
34+
35+
updateCamera :: Position -> Position -> Float -> Float -> Position
36+
updateCamera cameraPos playerPos xdim ydim = Position (min (32*xdim-800) (max 0 xpos)) (min (32*ydim-600) (max 0 ypos))
37+
where xpos = posDiff 200 600 (xVal cameraPos) (xVal playerPos)
38+
ypos = posDiff 200 400 (yVal cameraPos) (yVal playerPos)
39+
40+
setupFight :: GameState -> Word32 -> GameState
41+
setupFight gs t = gs { enemies = enemies' , gameMode = Model.Fight} -- create enemies here
42+
where enemies' = genEnemies gs
43+
44+
genEnemies :: GameState -> [Enemy]
45+
genEnemies gs = [Enemy "Rat 1" Model.Rat 10 10 1 1 1 animation (Position 10 10)]
46+
where animation = Animation (ratSprite $ gx gs) 80 80 2 250 (t gs) 0 (Position 100 100) Nothing
47+
48+
checkForFight :: GameState -> Word32 -> GameState
49+
checkForFight gs t = gs
50+
-- | fromIntegral t > nextFight gs = setupFight gs t
51+
-- | otherwise = gs
52+

Logics.hs

+2-46
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,15 @@ module Logics where
33
import Graphics.UI.SDL as SDL
44

55
import Data.Word
6-
import Data.Tiled
76
import Data.Maybe
7+
import Data.Tiled
88

99
import Model
1010
import Animation
11+
import LogicWalking
1112

1213
import System.Random
1314

14-
setUpNextFight :: GameState -> Int -> GameState
15-
setUpNextFight gs t = gs {rng = rng', nextFight = nextFight'}
16-
where (val, rng') = next $ rng gs
17-
nextFight' = val `mod` 1000 + t + 2000
18-
19-
nextPlayerPos :: Player -> Word32 -> Position
20-
nextPlayerPos player dt
21-
| speed player == Slow && moveDirection player == Model.Right = Position (x0 + slowSpeed*(fromIntegral dt)) y0
22-
| speed player == Slow && moveDirection player == Model.Left = Position (x0 - slowSpeed*(fromIntegral dt)) y0
23-
| speed player == Slow && moveDirection player == Model.Up = Position x0 (y0 - slowSpeed*(fromIntegral dt))
24-
| speed player == Slow && moveDirection player == Model.Down = Position x0 (y0 + slowSpeed*(fromIntegral dt))
25-
| otherwise = playerPos player
26-
where x0 = xVal $ playerPos player
27-
y0 = yVal $ playerPos player
28-
slowSpeed = 0.2
29-
30-
updatePlayer :: Player -> Word32 -> Word32 -> Player
31-
updatePlayer player t dt = player { animation = animation', playerPos = playerPos' }
32-
where animation' = (head (updateAnimations [animation player] t)) { animPos = playerPos player}
33-
playerPos' = nextPlayerPos player dt
34-
35-
posDiff :: Float -> Float -> Float -> Float -> Float
36-
posDiff minval maxval a b
37-
| (b-a) < minval = b - minval
38-
| (b-a) > maxval = b - maxval
39-
| otherwise = a
40-
41-
updateCamera :: Position -> Position -> Float -> Float -> Position
42-
updateCamera cameraPos playerPos xdim ydim = Position (min (32*xdim-800) (max 0 xpos)) (min (32*ydim-600) (max 0 ypos))
43-
where xpos = posDiff 200 600 (xVal cameraPos) (xVal playerPos)
44-
ypos = posDiff 200 400 (yVal cameraPos) (yVal playerPos)
45-
46-
setupFight :: GameState -> Word32 -> GameState
47-
setupFight gs t = gs { enemies = enemies' , gameMode = Model.Fight} -- create enemies here
48-
where enemies' = genEnemies gs
49-
50-
genEnemies :: GameState -> [Enemy]
51-
genEnemies gs = [Enemy "Rat 1" Model.Rat 10 10 1 1 1 animation (Position 10 10)]
52-
where animation = Animation (ratSprite $ gx gs) 80 80 2 250 (t gs) 0 (Position 100 100) Nothing
53-
54-
checkForFight :: GameState -> Word32 -> GameState
55-
checkForFight gs t
56-
| fromIntegral t > nextFight gs = setupFight gs t
57-
| otherwise = gs
58-
5915
performFightActions :: GameState -> [String] -> Word32 -> IO(GameState)
6016
performFightActions gs [] _ = return (gs)
6117
performFightActions gs ("Attack":xs) t = do

Model.hs

+9-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ data GameState = GameState{
6565
nextFight :: Int,
6666
gx :: Graphics,
6767
menu :: Menu,
68+
hasMenu :: Bool,
6869
fnt :: Font,
6970
actions :: [String],
7071
enemies :: [Enemy],
71-
t :: Word32
72+
t :: Word32,
73+
agents :: [Agent]
7274
}
7375

7476
data Animation = Animation {
@@ -89,3 +91,9 @@ data Player = Player {
8991
playerPos :: Position,
9092
animation :: Animation
9193
}
94+
95+
data Agent = Agent {
96+
agentName :: String,
97+
agentPos :: Position,
98+
agentImage :: Surface
99+
}

image/Treasure.png

395 Bytes
Loading

image/agent.png

1.41 KB
Loading

image/tiles_tiny_sample.png

-2.4 KB
Loading

main.hs

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import Animation
1010
import Draw
1111
import Event
1212
import Logics
13-
--import MapLoader
13+
import LogicWalking
14+
import Agents
1415

1516
import System.Random
1617

@@ -45,7 +46,8 @@ main = do
4546
let player = Player Down Stop (Position 300 300) (Animation sheet 26 70 4 250 t0 0 (Position 0 0) Nothing)
4647
let gx = Graphics tileSurface fightbg menubg menumarker enemyfire explosion sheet hitSprite ratSprite
4748
let menu = Menu "Fight" 0 ["Attack", "Run"] (Position 0 340)
48-
let gs = (GameState True [] t0 player tiledMap (Position 32 32) Model.Walking rng 0 gx menu fnt [] [] t0)
49+
agents <- initAgents
50+
let gs = (GameState True [] t0 player tiledMap (Position 32 32) Model.Walking rng 0 gx menu False fnt [] [] t0 agents)
4951
let gs' = setUpNextFight gs ( fromIntegral (t0+1000) )
5052

5153
gameLoop gs' t0
@@ -58,7 +60,7 @@ gameLoop gs lastTick = do
5860

5961
gs' <- updateGamestate (gameMode gs) (handleEvents events gs) t (t - lastTick)
6062

61-
drawGamestate gs'
63+
drawGamestate gs' (gameMode gs')
6264

6365
if gameActive gs'
6466
then gameLoop gs'{t = t} t

0 commit comments

Comments
 (0)