-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
110 lines (97 loc) · 2.36 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
109
110
package main
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
/*
Player is the ship a player uses to fight aliens!
*/
type Player struct {
assetManager *AssetManager
window *pixelgl.Window
sprite *pixel.Sprite
pos pixel.Vec
width float64
height float64
leftEdge float64
rightEdge float64
dead bool
}
/*
NewPlayer creates a new player struct. Is is initialized positioned
at the bottom middle of the window
*/
func NewPlayer(window *pixelgl.Window, assetManager *AssetManager) *Player {
sprite := assetManager.GetShipSprite()
pos := window.Bounds().Center()
pos.Y = 16
return &Player{
assetManager: assetManager,
window: window,
sprite: sprite,
pos: pos,
width: sprite.Frame().W(),
height: sprite.Frame().H(),
leftEdge: sprite.Frame().W() / 2,
rightEdge: window.Bounds().W() - (sprite.Frame().W() / 2),
dead: false,
}
}
/*
Draw renders this ship onto the window
*/
func (p *Player) Draw() {
if !p.dead {
p.sprite.Draw(p.assetManager.Batch, pixel.IM.Moved(p.pos))
// p.sprite.Draw(p.window, pixel.IM.Moved(p.pos))
}
}
/*
GetHeight returns the ship's height
*/
func (p *Player) GetHeight() float64 {
return p.height
}
/*
GetPosition retrieves the player's position vector
*/
func (p *Player) GetPosition() pixel.Vec {
return p.pos
}
func (p *Player) GetRect() pixel.Rect {
return pixel.R(p.pos.X-(p.width/2), p.pos.Y+(p.height/2), p.pos.X+(p.width/2), p.pos.Y-(p.height/2))
}
/*
IsLeftEdge returns true if the player is on the left edge of the window
*/
func (p *Player) IsLeftEdge() bool {
return p.pos.X <= p.leftEdge
}
/*
IsRightEdge returns true if the player is on the right edge of the window
*/
func (p *Player) IsRightEdge() bool {
return p.pos.X >= p.rightEdge
}
/*
IsShooting returns true if the player is pressing Space
*/
func (p *Player) IsShooting() bool {
return p.window.Pressed(pixelgl.KeySpace)
}
/*
MoveLeft moves the player left, repspecting the left edge boundary
*/
func (p *Player) MoveLeft(dt float64) {
move := -300.0
x := p.pos.X + (move * dt)
p.pos.X = pixel.Clamp(x, p.leftEdge, p.rightEdge)
}
/*
MoveRight moves the player right, respecting the right edge boundary
*/
func (p *Player) MoveRight(dt float64) {
move := 300.0
x := p.pos.X + (move * dt)
p.pos.X = pixel.Clamp(x, p.leftEdge, p.rightEdge)
}