-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
controller.go
147 lines (131 loc) · 3.05 KB
/
controller.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
// controller handle input from devices
type controller struct {
quit bool
entity entity
moveLeftKey pixelgl.Button
moveRightKey pixelgl.Button
moveClimbKey pixelgl.Button
moveJumpKey pixelgl.Button
lightDt float64
}
// Set which entity to control
func (c *controller) setActiveEntity(e entity) {
switch item := e.(type) {
case *mob:
c.entity = item
}
}
// Initialize controls
func (c *controller) create() {
c.quit = false
}
// Handle input for both mouse and keyboard
func (c *controller) update(dt float64) {
if global.gWin.Pressed(pixelgl.KeyQ) {
c.quit = true
}
// If main menu is visible, just stear the menu and not char.
if global.gActiveMenu != nil {
if global.gWin.JustPressed(pixelgl.KeyUp) {
global.gActiveMenu.moveUp()
}
if global.gWin.JustPressed(pixelgl.KeyDown) {
global.gActiveMenu.moveDown()
}
if global.gWin.JustPressed(pixelgl.KeyEnter) {
global.gActiveMenu.selectItem()
}
if global.gWin.JustPressed(pixelgl.KeyLeft) {
global.gActiveMenu.prevItem()
}
if global.gWin.JustPressed(pixelgl.KeyRight) {
global.gActiveMenu.nextItem()
}
if global.gWin.JustPressed(pixelgl.KeyEscape) {
global.gActiveMenu = nil
}
return
}
// Global not bound to entity
if global.gWin.Pressed(pixelgl.KeyM) {
// PrintMemoryUsage()
global.gActiveMenu = global.gMainMenu
}
if global.gWin.Pressed(pixelgl.KeyP) {
global.gCamera.setFollow(nil)
}
if global.gWin.Pressed(pixelgl.KeyG) {
global.gWorld.gravity += 0.1
}
if global.gWin.Pressed(pixelgl.KeyH) {
global.gWorld.gravity -= 0.1
}
// Controllers for entity
if c.entity == nil {
return
}
move := pixel.Vec{X: 0, Y: 0}
// TEST
if global.gWin.Pressed(pixelgl.KeyK) {
//global.gWorld.Explode(global.gPlayer.bounds.X, global.gPlayer.bounds.Y+20, 20)
}
c.lightDt += dt
if global.gWin.Pressed(pixelgl.KeyL) {
if c.lightDt > 1 {
l := &light{}
pos := global.gPlayer.getPosition()
l.create(pos.X, pos.Y, 300, 360, 100, pixel.RGBA{R: 0.8, G: 0.6, B: 0, A: 0.3}, true, 1)
c.lightDt = 0
}
}
// Go into door
if global.gWin.Pressed(pixelgl.KeyA) {
if m, ok := c.entity.(*mob); ok {
m.action()
}
}
// Test pickup
if global.gWin.Pressed(pixelgl.KeyB) {
if m, ok := c.entity.(*mob); ok {
m.pickup()
}
}
// Throw object
if global.gWin.Pressed(pixelgl.KeyV) {
if m, ok := c.entity.(*mob); ok {
m.throw()
}
}
if global.gWin.Pressed(pixelgl.KeyS) {
global.gCamera.zoom -= 0.05
}
if global.gWin.Pressed(pixelgl.KeyW) {
global.gCamera.zoom += 0.05
}
if global.gWin.Pressed(pixelgl.KeyLeft) {
move.X = -dt
}
if global.gWin.Pressed(pixelgl.KeyRight) {
move.X = dt
}
if global.gWin.Pressed(pixelgl.KeyUp) {
move.Y = dt
}
if global.gWin.Pressed(pixelgl.KeyDown) {
move.Y = -dt
}
if m, ok := c.entity.(*mob); ok {
m.move(move.X, move.Y)
}
// Handle mouse
if global.gWin.Pressed(pixelgl.MouseButtonLeft) || global.gWin.Pressed(pixelgl.KeyLeftShift) {
if m, ok := c.entity.(*mob); ok {
m.shoot()
}
}
}