Automatic shooting

This commit is contained in:
2024-01-04 05:57:21 +01:00
parent d336fa4d15
commit 156e3ed43c
5 changed files with 51 additions and 35 deletions

View File

@@ -14,10 +14,12 @@ import (
)
const WalkSpeedMs = 300
const ReloadSpeedMs = 1000
var (
heroImage *ebiten.Image
walkTicker *time.Ticker
reloadTicker *time.Ticker
)
type Hero struct {
@@ -27,6 +29,7 @@ type Hero struct {
Health int // Health as a percentage (0-100)
X float64
Y float64
gunLoaded bool
}
type spriteFramePosition struct {
@@ -178,6 +181,7 @@ func init() {
}
heroImage = ebiten.NewImageFromImage(img)
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
reloadTicker = time.NewTicker(ReloadSpeedMs * time.Millisecond)
// go routine that runs on every tick and increases step
}
@@ -187,6 +191,7 @@ func NewHero() *Hero {
step: 0,
IsWalking: false,
Health: 100,
gunLoaded: true,
}
go func() {
@@ -200,6 +205,15 @@ func NewHero() *Hero {
}
}
}()
go func() {
for {
select {
case <-reloadTicker.C:
hero.gunLoaded = true
}
}
}()
return hero
}
@@ -224,6 +238,11 @@ func (h *Hero) Stop() {
h.IsWalking = false
}
func (h *Hero) StopShooting() {
h.gunLoaded = false
reloadTicker.Stop()
}
func (h *Hero) CollisionShape() collision.Polygon {
return collision.Polygon{
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) {
if fire {
func (h *Hero) Fire(offsetX float64, offsetY float64) (bullet *weapons.Handgun) {
if h.gunLoaded {
h.gunLoaded = false
bullet = weapons.NewHandgun()
bullet.X = h.X + (resources.HeroTileSize / 2) - offsetX
bullet.Y = h.Y + (resources.HeroTileSize / 2) - offsetY
bullet.OffsetX = offsetX
bullet.OffsetY = offsetY
bullet.X = h.X + (resources.HeroTileSize / 2)
bullet.Y = h.Y + (resources.HeroTileSize / 2)
bullet.OffsetXAtStart = offsetX
bullet.OffsetYAtStart = offsetY
bullet.Fire(h.direction)
return bullet
} else {

View File

@@ -2,7 +2,6 @@ package input
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"sort"
"strings"
)
@@ -52,7 +51,3 @@ func (k *Keyboard) DirectionFromKeys(keys []ebiten.Key) configuration.Direction
return configuration.PreviouslyHeld
}
}
func (k *Keyboard) ShouldFire(keys []ebiten.Key) bool {
return inpututil.IsKeyJustReleased(ebiten.KeySpace)
}

View File

@@ -63,7 +63,7 @@ func (g *Game) Update() error {
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
GameInstance.hero.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 {
GameInstance.world.AddEntity(bullet)
g.bullets = append(g.bullets, bullet)

View File

@@ -12,6 +12,7 @@ import (
var (
grassImage *ebiten.Image
subimage *ebiten.Image
)
type Terrain struct {
@@ -31,7 +32,7 @@ func (t *Terrain) DrawTerrain(screen *ebiten.Image) {
// ground
op.GeoM.Reset()
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)
}
grassImage = ebiten.NewImageFromImage(img)
subimage = grassImage.SubImage(image.Rect(32, 160, 32+64, 160+64)).(*ebiten.Image)
}
func NewTerrain() *Terrain {
@@ -54,16 +56,16 @@ func NewTerrain() *Terrain {
func (t *Terrain) Move() {
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) {
t.PositionY -= 1
t.PositionY -= 3
}
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) {
t.PositionX += 1
t.PositionX += 3
}
}

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"gitlab.com/kbr4/9heroja/collision"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/resources"
@@ -25,6 +24,8 @@ type Handgun struct {
IsFlying bool
X float64
Y float64
OffsetXAtStart float64
OffsetYAtStart float64
OffsetX float64
OffsetY float64
IsDisintegrated bool
@@ -41,16 +42,13 @@ func (h *Handgun) DrawHandgunBullet(screen *ebiten.Image) {
}
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.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)
screen.DrawImage(
handgunBulletImage,
op,
)
op.ColorScale.Scale(1, 1, 1, 1)
// draw circle
screen.DrawImage(handgunBulletImage, op)
}
}
@@ -92,10 +90,10 @@ func (h *Handgun) CollisionShape() collision.Polygon {
if !h.IsDisintegrated {
return collision.Polygon{
Points: []collision.Point{
{X: h.X + h.OffsetX, Y: h.Y + h.OffsetY},
{X: h.X + h.OffsetX + 4, Y: h.Y + h.OffsetY},
{X: h.X + h.OffsetX + 4, Y: h.Y + h.OffsetY + 4},
{X: h.X + h.OffsetX, Y: h.Y + h.OffsetY + 4},
{X: h.X + h.OffsetX - 3 - h.OffsetXAtStart, Y: h.Y + h.OffsetY - 3 - h.OffsetYAtStart},
{X: h.X + h.OffsetX + 7 - h.OffsetXAtStart, Y: h.Y + h.OffsetY - 3 - h.OffsetYAtStart},
{X: h.X + h.OffsetX + 7 - h.OffsetXAtStart, Y: h.Y + h.OffsetY + 7 - h.OffsetYAtStart},
{X: h.X + h.OffsetX - 3 - h.OffsetXAtStart, Y: h.Y + h.OffsetY + 7 - h.OffsetYAtStart},
},
}
} else {
@@ -119,16 +117,16 @@ func (h *Handgun) HandleCollisionEvent(other collision.Collidable) {
}
func (h *Handgun) Move() {
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) {
h.Y += 1
h.Y += 6
}
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) {
h.X -= 1
h.X -= 6
}
}