-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.hs
87 lines (69 loc) · 2.16 KB
/
Bullet.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module Bullet
( Bullet (..),
BulletState (..),
BulletId,
Health,
bulletDamage,
staticBulletImages,
bulletSpeed,
bulletRadius,
createBullet,
drawBullet,
moveBullet,
isBulletWithinBounds,
bulletToCircle,
setBulletHit,
getBulletId,
)
where
import Circle
import Codec.Picture.Types ( PixelRGBA8(PixelRGBA8) )
import SDL ( Rectangle )
import Space
import Visual ( ImageId, VectorImage )
type BulletId = Int
data Bullet = Bullet Circle Velocity2D BulletState BulletId deriving (Show, Eq)
data BulletState = HasHitPlayer | HasNotHitPlayer deriving (Show, Eq)
type Health = Float
bulletDamage :: Health
bulletDamage = 0.15
bulletSpeed :: Speed
bulletSpeed = 1.26
bulletRadius :: Radius
bulletRadius = 10
hasNotHitPlayerImageId = "hasNotHitPlayerBullet"
hasHitPlayerImageId = "hasHitPlayerBullet"
createBullet :: Position2D -> Angle2D -> BulletId -> Bullet
createBullet position direction =
Bullet
(Circle position bulletRadius)
(toVelocity direction bulletSpeed)
HasNotHitPlayer
staticBulletImages :: [(ImageId, VectorImage)]
staticBulletImages =
[ (imageId, toSolidCircleImage color bulletRadius)
| (imageId, color) <-
[ (hasNotHitPlayerImageId, PixelRGBA8 0xff 0xe6 0x80 255),
(hasHitPlayerImageId, PixelRGBA8 0xd3 0x5f 0x5f 255)
]
]
drawBullet :: Bullet -> (Rectangle Float, Either VectorImage ImageId)
drawBullet (Bullet circle _ state _) =
( toTextureArea circle,
case state of
HasNotHitPlayer -> Right hasNotHitPlayerImageId
HasHitPlayer -> Right hasHitPlayerImageId
)
moveBullet :: DeltaTime -> Bullet -> Bullet
moveBullet dt (Bullet circle velocity state bulletId) =
Bullet (moveCircle velocity dt circle) velocity state bulletId
isBulletWithinBounds :: Bounds2D -> Bullet -> Bool
isBulletWithinBounds bounds (Bullet circle _ _ _) =
isCircleWithinBounds bounds circle
bulletToCircle :: Bullet -> Circle
bulletToCircle (Bullet circle _ _ _) = circle
setBulletHit :: Bullet -> Bullet
setBulletHit (Bullet circle velocity _ bulletId) =
Bullet circle velocity HasHitPlayer bulletId
getBulletId :: Bullet -> BulletId
getBulletId (Bullet _ _ _ bulletId) = bulletId