Skip to content

Commit

Permalink
Add animation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kensonjohnson committed Aug 12, 2024
1 parent aa845d3 commit 27d281a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
6 changes: 5 additions & 1 deletion system/action/monster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TakeMonsterAction(ecs *ecs.ECS) {

archetype.MonsterTag.Each(ecs.World, func(entry *donburi.Entry) {
position := component.Position.Get(entry)

sprite := component.Sprite.Get(entry)
monsterVision := component.Fov.Get(entry).VisibleTiles

// Check if the monster can see the player
Expand All @@ -37,8 +37,12 @@ func TakeMonsterAction(ecs *ecs.ECS) {
// Update the tile this monster is blocking
level.GetFromXY(position.X, position.Y).Blocked = false
nextTile.Blocked = true
sprite.OffestX = position.X - path[1].X
sprite.OffestY = position.Y - path[1].Y
position.X = path[1].X
position.Y = path[1].Y
sprite.Animating = true
sprite.SetProgress(0)
// Since the monster moved, update the field of view
monsterVision.Compute(level, position.X, position.Y, 8)
}
Expand Down
23 changes: 14 additions & 9 deletions system/action/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ import (
func TakePlayerAction(ecs *ecs.ECS) bool {
turnTaken := false

// Grab current level
levelEntry := archetype.LevelTag.MustFirst(ecs.World)
level := component.Level.Get(levelEntry)

// Grab player data
playerEntry := archetype.PlayerTag.MustFirst(ecs.World)
position := component.Position.Get(playerEntry)
vision := component.Fov.Get(playerEntry)

// Capture any keypresses we care about
moveX := 0
moveY := 0
Expand Down Expand Up @@ -55,6 +46,16 @@ func TakePlayerAction(ecs *ecs.ECS) bool {
return false
}

// Grab current level
levelEntry := archetype.LevelTag.MustFirst(ecs.World)
level := component.Level.Get(levelEntry)

// Grab player data
playerEntry := archetype.PlayerTag.MustFirst(ecs.World)
position := component.Position.Get(playerEntry)
sprite := component.Sprite.Get(playerEntry)
vision := component.Fov.Get(playerEntry)

// TODO: Update so diagonal movement consumes two turns
// Attempt to move
tile := level.GetFromXY(position.X+moveX, position.Y+moveY)
Expand All @@ -66,6 +67,10 @@ func TakePlayerAction(ecs *ecs.ECS) bool {
// Update the player's position
position.X += moveX
position.Y += moveY
sprite.OffestX = -moveX
sprite.OffestY = -moveY
sprite.Animating = true
sprite.SetProgress(0)
// Update the player's field of view
vision.VisibleTiles.Compute(level, position.X, position.Y, 8)
// Update any discoverable entities
Expand Down
7 changes: 6 additions & 1 deletion system/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func (r *render) Draw(ecs *ecs.ECS, screen *ebiten.Image) {

if playerVision.IsVisible(position.X, position.Y) {
camera.CamImageOptions.GeoM.Reset()
camera.CamImageOptions.GeoM.Translate(float64(position.X*config.TileWidth), float64(position.Y*config.TileHeight))
if sprite.Animating {
offsetX, offsetY := sprite.GetAnimationStep()
camera.CamImageOptions.GeoM.Translate(float64(position.X*config.TileWidth)+offsetX, float64(position.Y*config.TileHeight)+offsetY)
} else {
camera.CamImageOptions.GeoM.Translate(float64(position.X*config.TileWidth), float64(position.Y*config.TileHeight))
}
camera.MainCamera.Draw(sprite.Image, camera.CamImageOptions, screen)
}
})
Expand Down

0 comments on commit 27d281a

Please sign in to comment.