-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_state.go
130 lines (104 loc) · 3.53 KB
/
play_state.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package game_manager
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
type PlayState struct {
playID string
gameObjects []GameObject
}
//*****************************************************************************
// NewPlayState
//*****************************************************************************
func NewPlayState() *PlayState {
return &PlayState{playID: "PLAY"}
}
//*****************************************************************************
// getStateID
//*****************************************************************************
func (ps *PlayState) getStateID() string {
return ps.playID
}
//*****************************************************************************
// update
//*****************************************************************************
func (ps *PlayState) update(game *Game) {
if game.inputHandler.isKeyDown(sdl.SCANCODE_ESCAPE) {
game.gameStateMachine.pushState(NewPauseState())
}
for _, gameObject := range ps.gameObjects {
gameObject.update(game)
}
if ps.checkCollision(ps.gameObjects[0], ps.gameObjects[1]) {
game.gameStateMachine.pushState(NewGameOverState())
}
}
//*****************************************************************************
// render
//*****************************************************************************
func (ps *PlayState) render(game *Game) {
for _, gameObject := range ps.gameObjects {
gameObject.draw(game)
}
}
//*****************************************************************************
// resume
//*****************************************************************************
func (ps *PlayState) resume(game *Game) {
}
//*****************************************************************************
// onEnter
//*****************************************************************************
func (ps *PlayState) onEnter(game *Game) bool {
if !game.textureManager.load("assets/helicopter.png", "helicopter", game.renderer) {
return false
}
if !game.textureManager.load("assets/helicopter2.png", "helicopter2", game.renderer) {
return false
}
ps.gameObjects = append(ps.gameObjects, NewPlayer(500, 100, 128, 55, "helicopter"))
ps.gameObjects = append(ps.gameObjects, NewEnemy(100, 100, 128, 55, "helicopter2"))
fmt.Println("entering PlayState")
return true
}
//*****************************************************************************
// onExit
//*****************************************************************************
func (ps *PlayState) onExit(game *Game) bool {
for _, gameObject := range ps.gameObjects {
gameObject.clean()
}
ps.gameObjects = ps.gameObjects[:0]
game.textureManager.clearFromTextureMap("helicopter")
fmt.Println("exiting PlayState")
return true
}
//*****************************************************************************
// checkCollision
//*****************************************************************************
func (ps *PlayState) checkCollision(p1 GameObject, p2 GameObject) bool {
var leftA, leftB, rightA, rightB, topA, topB, bottomA, bottomB float32
leftA = p1.Position().X
rightA = p1.Position().X + float32(p1.Width())
topA = p1.Position().Y
bottomA = p1.Position().Y + float32(p1.Height())
//Calculate the sides of rect B
leftB = p2.Position().X
rightB = p2.Position().X + float32(p2.Width())
topB = p2.Position().Y
bottomB = p2.Position().Y + float32(p2.Height())
//If any of the sides from A are outside of B
if bottomA <= topB {
return false
}
if topA >= bottomB {
return false
}
if rightA <= leftB {
return false
}
if leftA >= rightB {
return false
}
return true
}