Collision detection system
This commit is contained in:
59
hero/hero.go
59
hero/hero.go
@@ -3,6 +3,7 @@ package hero
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"gitlab.com/kbr4/9heroja/collision"
|
||||
"gitlab.com/kbr4/9heroja/configuration"
|
||||
"gitlab.com/kbr4/9heroja/resources"
|
||||
"image"
|
||||
@@ -23,9 +24,11 @@ type Hero struct {
|
||||
direction configuration.Direction
|
||||
IsWalking bool
|
||||
Health int // Health as a percentage (0-100)
|
||||
X float64
|
||||
Y float64
|
||||
}
|
||||
|
||||
type spritePosition struct {
|
||||
type spriteFramePosition struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
@@ -34,8 +37,8 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
|
||||
// set movement positions to be hashmap of sprite positions for each direction
|
||||
|
||||
movementPositions := map[configuration.Direction][]spritePosition{}
|
||||
movementPositions[configuration.North] = []spritePosition{
|
||||
movementPositions := map[configuration.Direction][]spriteFramePosition{}
|
||||
movementPositions[configuration.North] = []spriteFramePosition{
|
||||
{0, 166},
|
||||
{33, 166},
|
||||
{66, 166},
|
||||
@@ -45,7 +48,7 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
{66, 166},
|
||||
{33, 166},
|
||||
}
|
||||
movementPositions[configuration.NorthEast] = []spritePosition{
|
||||
movementPositions[configuration.NorthEast] = []spriteFramePosition{
|
||||
{168, 0},
|
||||
{201, 0},
|
||||
{0, 33},
|
||||
@@ -56,7 +59,7 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
{201, 0},
|
||||
}
|
||||
|
||||
movementPositions[configuration.East] = []spritePosition{
|
||||
movementPositions[configuration.East] = []spriteFramePosition{
|
||||
{132, 99},
|
||||
{165, 99},
|
||||
{198, 99},
|
||||
@@ -67,7 +70,7 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
{165, 99},
|
||||
}
|
||||
|
||||
movementPositions[configuration.SouthEast] = []spritePosition{
|
||||
movementPositions[configuration.SouthEast] = []spriteFramePosition{
|
||||
{33, 66},
|
||||
{66, 66},
|
||||
{99, 66},
|
||||
@@ -78,7 +81,7 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
{66, 66},
|
||||
}
|
||||
|
||||
movementPositions[configuration.South] = []spritePosition{
|
||||
movementPositions[configuration.South] = []spriteFramePosition{
|
||||
{66, 132},
|
||||
{0, 99},
|
||||
{33, 99},
|
||||
@@ -89,7 +92,7 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
{0, 99},
|
||||
}
|
||||
|
||||
movementPositions[configuration.SouthWest] = []spritePosition{
|
||||
movementPositions[configuration.SouthWest] = []spriteFramePosition{
|
||||
{99, 33},
|
||||
{33, 0},
|
||||
{66, 0},
|
||||
@@ -100,7 +103,7 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
{33, 0},
|
||||
}
|
||||
|
||||
movementPositions[configuration.West] = []spritePosition{
|
||||
movementPositions[configuration.West] = []spriteFramePosition{
|
||||
{198, 66},
|
||||
{99, 132},
|
||||
{132, 132},
|
||||
@@ -111,7 +114,7 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
{99, 132},
|
||||
}
|
||||
|
||||
movementPositions[configuration.NorthWest] = []spritePosition{
|
||||
movementPositions[configuration.NorthWest] = []spriteFramePosition{
|
||||
{0, 0},
|
||||
{132, 33},
|
||||
{165, 33},
|
||||
@@ -129,7 +132,9 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
|
||||
// ground
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(float64(screenWidth/2-16), float64(screenHeight/2-16))
|
||||
h.X = float64(screenWidth/2 - 16)
|
||||
h.Y = float64(screenHeight/2 - 16)
|
||||
op.GeoM.Translate(h.X, h.Y)
|
||||
//op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
|
||||
// float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
|
||||
|
||||
@@ -138,8 +143,8 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
||||
// Drawing the dynamic health bar below the hero
|
||||
maxHealthBarWidth := resources.HeroTileSize // Maximum width of the health bar (same as the character width)
|
||||
healthBarHeight := 5 // Height of the health bar
|
||||
healthBarX := float64(screenWidth/2 - 16) // Align with the hero
|
||||
healthBarY := float64(screenHeight/2 + 20) // Position below the hero
|
||||
healthBarX := float64(h.X) // Align with the hero
|
||||
healthBarY := float64(h.Y + 37) // Position below the hero
|
||||
|
||||
// Calculate the current width of the health bar based on the hero's health
|
||||
currentHealthBarWidth := int(float64(maxHealthBarWidth) * (float64(h.Health) / 100.0))
|
||||
@@ -180,7 +185,7 @@ func NewHero() *Hero {
|
||||
hero := &Hero{
|
||||
step: 0,
|
||||
IsWalking: false,
|
||||
Health: 33,
|
||||
Health: 100,
|
||||
}
|
||||
|
||||
go func() {
|
||||
@@ -217,3 +222,29 @@ func (h *Hero) Stop() {
|
||||
walkTicker.Stop()
|
||||
h.IsWalking = false
|
||||
}
|
||||
|
||||
func (h *Hero) CollisionShape() collision.Polygon {
|
||||
return collision.Polygon{
|
||||
Points: []collision.Point{
|
||||
{X: h.X, Y: h.Y},
|
||||
{X: h.X + 33, Y: h.Y},
|
||||
{X: h.X + 33, Y: h.Y + 33},
|
||||
{X: h.X, Y: h.Y + 33},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hero) CollisionObjectType() collision.ObjectType {
|
||||
return collision.Hero
|
||||
}
|
||||
|
||||
func (h *Hero) HandleCollisionEvent(other collision.Collidable) {
|
||||
coll := other.CollisionObjectType()
|
||||
switch coll {
|
||||
case collision.Zombie:
|
||||
h.Health -= 25
|
||||
if h.Health <= 0 {
|
||||
h.Health = 10
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user