Automatic shooting
This commit is contained in:
37
hero/hero.go
37
hero/hero.go
@@ -14,10 +14,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const WalkSpeedMs = 300
|
const WalkSpeedMs = 300
|
||||||
|
const ReloadSpeedMs = 1000
|
||||||
|
|
||||||
var (
|
var (
|
||||||
heroImage *ebiten.Image
|
heroImage *ebiten.Image
|
||||||
walkTicker *time.Ticker
|
walkTicker *time.Ticker
|
||||||
|
reloadTicker *time.Ticker
|
||||||
)
|
)
|
||||||
|
|
||||||
type Hero struct {
|
type Hero struct {
|
||||||
@@ -27,6 +29,7 @@ type Hero struct {
|
|||||||
Health int // Health as a percentage (0-100)
|
Health int // Health as a percentage (0-100)
|
||||||
X float64
|
X float64
|
||||||
Y float64
|
Y float64
|
||||||
|
gunLoaded bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type spriteFramePosition struct {
|
type spriteFramePosition struct {
|
||||||
@@ -178,6 +181,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
heroImage = ebiten.NewImageFromImage(img)
|
heroImage = ebiten.NewImageFromImage(img)
|
||||||
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
|
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
|
||||||
|
reloadTicker = time.NewTicker(ReloadSpeedMs * time.Millisecond)
|
||||||
// go routine that runs on every tick and increases step
|
// go routine that runs on every tick and increases step
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -187,6 +191,7 @@ func NewHero() *Hero {
|
|||||||
step: 0,
|
step: 0,
|
||||||
IsWalking: false,
|
IsWalking: false,
|
||||||
Health: 100,
|
Health: 100,
|
||||||
|
gunLoaded: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -200,6 +205,15 @@ func NewHero() *Hero {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-reloadTicker.C:
|
||||||
|
hero.gunLoaded = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
return hero
|
return hero
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,6 +238,11 @@ func (h *Hero) Stop() {
|
|||||||
h.IsWalking = false
|
h.IsWalking = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Hero) StopShooting() {
|
||||||
|
h.gunLoaded = false
|
||||||
|
reloadTicker.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Hero) CollisionShape() collision.Polygon {
|
func (h *Hero) CollisionShape() collision.Polygon {
|
||||||
return collision.Polygon{
|
return collision.Polygon{
|
||||||
Points: []collision.Point{
|
Points: []collision.Point{
|
||||||
@@ -250,13 +269,15 @@ func (h *Hero) HandleCollisionEvent(other collision.Collidable) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hero) Fire(fire bool, offsetX float64, offsetY float64) (bullet *weapons.Handgun) {
|
func (h *Hero) Fire(offsetX float64, offsetY float64) (bullet *weapons.Handgun) {
|
||||||
if fire {
|
if h.gunLoaded {
|
||||||
|
h.gunLoaded = false
|
||||||
bullet = weapons.NewHandgun()
|
bullet = weapons.NewHandgun()
|
||||||
bullet.X = h.X + (resources.HeroTileSize / 2) - offsetX
|
bullet.X = h.X + (resources.HeroTileSize / 2)
|
||||||
bullet.Y = h.Y + (resources.HeroTileSize / 2) - offsetY
|
bullet.Y = h.Y + (resources.HeroTileSize / 2)
|
||||||
bullet.OffsetX = offsetX
|
bullet.OffsetXAtStart = offsetX
|
||||||
bullet.OffsetY = offsetY
|
bullet.OffsetYAtStart = offsetY
|
||||||
|
|
||||||
bullet.Fire(h.direction)
|
bullet.Fire(h.direction)
|
||||||
return bullet
|
return bullet
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package input
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -52,7 +51,3 @@ func (k *Keyboard) DirectionFromKeys(keys []ebiten.Key) configuration.Direction
|
|||||||
return configuration.PreviouslyHeld
|
return configuration.PreviouslyHeld
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) ShouldFire(keys []ebiten.Key) bool {
|
|
||||||
return inpututil.IsKeyJustReleased(ebiten.KeySpace)
|
|
||||||
}
|
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -63,7 +63,7 @@ func (g *Game) Update() error {
|
|||||||
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
|
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
|
||||||
GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
||||||
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
||||||
bullet := GameInstance.hero.Fire(GameInstance.control.ShouldFire(g.keys), g.terrain.PositionX, g.terrain.PositionY)
|
bullet := GameInstance.hero.Fire(g.terrain.PositionX, g.terrain.PositionY)
|
||||||
if bullet != nil {
|
if bullet != nil {
|
||||||
GameInstance.world.AddEntity(bullet)
|
GameInstance.world.AddEntity(bullet)
|
||||||
g.bullets = append(g.bullets, bullet)
|
g.bullets = append(g.bullets, bullet)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
grassImage *ebiten.Image
|
grassImage *ebiten.Image
|
||||||
|
subimage *ebiten.Image
|
||||||
)
|
)
|
||||||
|
|
||||||
type Terrain struct {
|
type Terrain struct {
|
||||||
@@ -31,7 +32,7 @@ func (t *Terrain) DrawTerrain(screen *ebiten.Image) {
|
|||||||
// ground
|
// ground
|
||||||
op.GeoM.Reset()
|
op.GeoM.Reset()
|
||||||
op.GeoM.Translate(float64(i*64+offsetX), float64(j*64+offsetY))
|
op.GeoM.Translate(float64(i*64+offsetX), float64(j*64+offsetY))
|
||||||
screen.DrawImage(grassImage.SubImage(image.Rect(32, 160, 32+64, 160+64)).(*ebiten.Image), op)
|
screen.DrawImage(subimage, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -43,6 +44,7 @@ func init() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
grassImage = ebiten.NewImageFromImage(img)
|
grassImage = ebiten.NewImageFromImage(img)
|
||||||
|
subimage = grassImage.SubImage(image.Rect(32, 160, 32+64, 160+64)).(*ebiten.Image)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTerrain() *Terrain {
|
func NewTerrain() *Terrain {
|
||||||
@@ -54,16 +56,16 @@ func NewTerrain() *Terrain {
|
|||||||
|
|
||||||
func (t *Terrain) Move() {
|
func (t *Terrain) Move() {
|
||||||
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, t.direction) {
|
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, t.direction) {
|
||||||
t.PositionY += 1
|
t.PositionY += 3
|
||||||
}
|
}
|
||||||
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, t.direction) {
|
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, t.direction) {
|
||||||
t.PositionY -= 1
|
t.PositionY -= 3
|
||||||
}
|
}
|
||||||
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, t.direction) {
|
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, t.direction) {
|
||||||
t.PositionX -= 1
|
t.PositionX -= 3
|
||||||
}
|
}
|
||||||
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, t.direction) {
|
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, t.direction) {
|
||||||
t.PositionX += 1
|
t.PositionX += 3
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
||||||
"gitlab.com/kbr4/9heroja/collision"
|
"gitlab.com/kbr4/9heroja/collision"
|
||||||
"gitlab.com/kbr4/9heroja/configuration"
|
"gitlab.com/kbr4/9heroja/configuration"
|
||||||
"gitlab.com/kbr4/9heroja/resources"
|
"gitlab.com/kbr4/9heroja/resources"
|
||||||
@@ -25,6 +24,8 @@ type Handgun struct {
|
|||||||
IsFlying bool
|
IsFlying bool
|
||||||
X float64
|
X float64
|
||||||
Y float64
|
Y float64
|
||||||
|
OffsetXAtStart float64
|
||||||
|
OffsetYAtStart float64
|
||||||
OffsetX float64
|
OffsetX float64
|
||||||
OffsetY float64
|
OffsetY float64
|
||||||
IsDisintegrated bool
|
IsDisintegrated bool
|
||||||
@@ -41,16 +42,13 @@ func (h *Handgun) DrawHandgunBullet(screen *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !h.IsDisintegrated && h.IsFlying {
|
if !h.IsDisintegrated && h.IsFlying {
|
||||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("\n\n\n\n\n\n\ndx: %f y:%f isint: %t, flying: %t", h.X, h.Y, h.IsDisintegrated, h.IsFlying))
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Reset()
|
op.GeoM.Reset()
|
||||||
op.GeoM.Translate(h.X+h.OffsetX, h.Y+h.OffsetY)
|
op.GeoM.Translate(h.X+h.OffsetX-h.OffsetXAtStart, h.Y+h.OffsetY-h.OffsetYAtStart)
|
||||||
op.GeoM.Scale(1, 1)
|
op.GeoM.Scale(1, 1)
|
||||||
|
op.ColorScale.Scale(1, 1, 1, 1)
|
||||||
screen.DrawImage(
|
// draw circle
|
||||||
handgunBulletImage,
|
screen.DrawImage(handgunBulletImage, op)
|
||||||
op,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,10 +90,10 @@ func (h *Handgun) CollisionShape() collision.Polygon {
|
|||||||
if !h.IsDisintegrated {
|
if !h.IsDisintegrated {
|
||||||
return collision.Polygon{
|
return collision.Polygon{
|
||||||
Points: []collision.Point{
|
Points: []collision.Point{
|
||||||
{X: h.X + h.OffsetX, Y: h.Y + h.OffsetY},
|
{X: h.X + h.OffsetX - 3 - h.OffsetXAtStart, Y: h.Y + h.OffsetY - 3 - h.OffsetYAtStart},
|
||||||
{X: h.X + h.OffsetX + 4, Y: h.Y + h.OffsetY},
|
{X: h.X + h.OffsetX + 7 - h.OffsetXAtStart, Y: h.Y + h.OffsetY - 3 - h.OffsetYAtStart},
|
||||||
{X: h.X + h.OffsetX + 4, Y: h.Y + h.OffsetY + 4},
|
{X: h.X + h.OffsetX + 7 - h.OffsetXAtStart, Y: h.Y + h.OffsetY + 7 - h.OffsetYAtStart},
|
||||||
{X: h.X + h.OffsetX, Y: h.Y + h.OffsetY + 4},
|
{X: h.X + h.OffsetX - 3 - h.OffsetXAtStart, Y: h.Y + h.OffsetY + 7 - h.OffsetYAtStart},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -119,16 +117,16 @@ func (h *Handgun) HandleCollisionEvent(other collision.Collidable) {
|
|||||||
}
|
}
|
||||||
func (h *Handgun) Move() {
|
func (h *Handgun) Move() {
|
||||||
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, h.direction) {
|
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, h.direction) {
|
||||||
h.Y -= 1
|
h.Y -= 6
|
||||||
}
|
}
|
||||||
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, h.direction) {
|
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, h.direction) {
|
||||||
h.Y += 1
|
h.Y += 6
|
||||||
}
|
}
|
||||||
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, h.direction) {
|
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, h.direction) {
|
||||||
h.X += 1
|
h.X += 6
|
||||||
}
|
}
|
||||||
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, h.direction) {
|
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, h.direction) {
|
||||||
h.X -= 1
|
h.X -= 6
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user