/
player.go
108 lines (88 loc) · 3.21 KB
/
player.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
package game_manager
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
type Player struct {
position Vector2D
velocity Vector2D
acceleration Vector2D
width int32
height int32
textureID string
currentRow int
currentFrame int32
}
//*****************************************************************************
// NewPlayer
//*****************************************************************************
func NewPlayer(x float32, y float32, width int32, height int32, textureID string) *Player {
return &Player{position: Vector2D{x, y}, velocity: Vector2D{0, 0}, width: width, height: height, textureID: textureID,
currentRow: 1, currentFrame: 1}
}
//*****************************************************************************
// Position
//*****************************************************************************
func (player *Player) Position() Vector2D {
return player.position
}
//*****************************************************************************
// Width
//*****************************************************************************
func (player *Player) Width() int32 {
return player.width
}
//*****************************************************************************
// Height
//*****************************************************************************
func (player *Player) Height() int32 {
return player.height
}
//*****************************************************************************
// draw
//*****************************************************************************
func (player *Player) draw(game *Game) {
var flip_or_not sdl.RendererFlip
if player.velocity.X > 0 {
flip_or_not = sdl.FLIP_HORIZONTAL
} else {
flip_or_not = sdl.FLIP_NONE
}
game.textureManager.drawFrame(player.textureID, player.position.X, player.position.Y, player.width, player.height, player.currentRow,
player.currentFrame, game.renderer, flip_or_not)
}
//*****************************************************************************
// update
//*****************************************************************************
func (player *Player) update(game *Game) {
player.handleInput(game)
player.currentFrame = int32(((sdl.GetTicks() / 100) % 6))
if game.inputHandler.isKeyDown(sdl.SCANCODE_RIGHT) {
player.velocity.X = 2
}
if game.inputHandler.isKeyDown(sdl.SCANCODE_LEFT) {
player.velocity.X = -2
}
if game.inputHandler.isKeyDown(sdl.SCANCODE_UP) {
player.velocity.Y = -2
}
if game.inputHandler.isKeyDown(sdl.SCANCODE_DOWN) {
player.velocity.Y = 2
}
player.velocity = player.velocity.Add(&player.acceleration)
player.position = player.position.Add(&player.velocity)
}
//*****************************************************************************
// handleInput
//*****************************************************************************
func (player *Player) handleInput(game *Game) {
target := game.inputHandler.getMousePosition()
player.velocity = target.Subtract(&player.position)
player.velocity = player.velocity.Divide(50)
}
//*****************************************************************************
// clean
//*****************************************************************************
func (player *Player) clean() {
fmt.Println("clean player")
}