Skip to content

Commit

Permalink
Add naive missile turret
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Nov 12, 2022
1 parent bcc14a7 commit 109f47f
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 43 deletions.
52 changes: 51 additions & 1 deletion archetype/bullet.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package archetype

import (
"time"

"github.com/yohamta/donburi"
"github.com/yohamta/donburi/features/math"
"github.com/yohamta/donburi/features/transform"
"github.com/yohamta/donburi/filter"
"github.com/yohamta/donburi/query"

"github.com/m110/airplanes/assets"
"github.com/m110/airplanes/component"
"github.com/m110/airplanes/engine"
)

const (
playerBulletSpeed = 10
enemyBulletSpeed = 4
enemyMissileSpeed = 2
)

func NewPlayerBullet(w donburi.World, player *component.PlayerData, position math.Vec2) {
Expand Down Expand Up @@ -113,7 +119,7 @@ func NewEnemyBullet(w donburi.World, position math.Vec2, rotation float64) {
),
)

image := assets.Rocket
image := assets.Bullet

t := transform.Transform.Get(bullet)
t.LocalPosition = position
Expand All @@ -138,3 +144,47 @@ func NewEnemyBullet(w donburi.World, position math.Vec2, rotation float64) {
Layer: component.CollisionLayerEnemyBullets,
})
}

func NewEnemyMissile(w donburi.World, position math.Vec2, rotation float64) {
missile := w.Entry(
w.Create(
component.Velocity,
transform.Transform,
component.Sprite,
component.Despawnable,
component.Collider,
component.Follower,
),
)

image := assets.Missile

t := transform.Transform.Get(missile)
t.LocalPosition = position
t.LocalRotation = rotation

component.Velocity.SetValue(missile, component.VelocityData{
Velocity: transform.Right(missile).MulScalar(enemyMissileSpeed),
})

component.Sprite.SetValue(missile, component.SpriteData{
Image: image,
Layer: component.SpriteLayerAirUnits,
Pivot: component.SpritePivotCenter,
OriginalRotation: -90,
})

width, height := image.Size()

component.Collider.SetValue(missile, component.ColliderData{
Width: float64(width),
Height: float64(height),
Layer: component.CollisionLayerEnemyBullets,
})

component.Follower.SetValue(missile, component.FollowerData{
Target: component.ClosestTarget(w, missile, query.NewQuery(filter.Contains(component.PlayerAirplane))),
FollowingSpeed: enemyMissileSpeed,
FollowingTimer: engine.NewTimer(3 * time.Second),
})
}
145 changes: 132 additions & 13 deletions archetype/enemy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ func NewEnemyAirplane(
})
}

component.Health.SetValue(airplane, component.HealthData{
Health: 3,
DamageIndicatorTimer: engine.NewTimer(time.Millisecond * 100),
DamageIndicator: newDamageIndicator(w, airplane),
})
health := component.Health.Get(airplane)
health.Health = 3
health.DamageIndicator = newDamageIndicator(w, airplane)

NewShadow(w, airplane)
}
Expand All @@ -111,7 +109,6 @@ func NewEnemyTank(
),
)

transform.Reset(tank)
t := transform.Transform.Get(tank)
t.LocalPosition = position
t.LocalRotation = rotation
Expand Down Expand Up @@ -146,11 +143,9 @@ func NewEnemyTank(
})
}

component.Health.SetValue(tank, component.HealthData{
Health: 5,
DamageIndicatorTimer: engine.NewTimer(time.Millisecond * 100),
DamageIndicator: newDamageIndicator(w, tank),
})
health := component.Health.Get(tank)
health.Health = 5
health.DamageIndicator = newDamageIndicator(w, tank)

gun := w.Entry(
w.Create(
Expand All @@ -163,7 +158,6 @@ func NewEnemyTank(
)

originalRotation := 90.0
transform.Reset(gun)
gunT := transform.Transform.Get(gun)
gunT.LocalPosition = position
gunT.LocalRotation = originalRotation + rotation
Expand All @@ -180,13 +174,138 @@ func NewEnemyTank(
})

component.Shooter.SetValue(gun, component.ShooterData{
Type: component.ShooterTypeRocket,
Type: component.ShooterTypeBullet,
ShootTimer: engine.NewTimer(time.Millisecond * 2500),
})

transform.AppendChild(tank, gun, true)
}

func NewEnemyTurretBeam(
w donburi.World,
position math.Vec2,
rotation float64,
) {
turret := newEnemyTurret(w, position, rotation)

gun := w.Entry(
w.Create(
transform.Transform,
component.Sprite,
component.Despawnable,
component.Observer,
component.Shooter,
),
)

originalRotation := 90.0
gunT := transform.Transform.Get(gun)
gunT.LocalPosition = position
gunT.LocalRotation = originalRotation + rotation

component.Sprite.SetValue(gun, component.SpriteData{
Image: assets.TurretGunSingle,
Layer: component.SpriteLayerGroundGuns,
Pivot: component.SpritePivotCenter,
OriginalRotation: originalRotation,
})

component.Observer.SetValue(gun, component.ObserverData{
LookFor: query.NewQuery(filter.Contains(component.PlayerAirplane)),
})

component.Shooter.SetValue(gun, component.ShooterData{
Type: component.ShooterTypeBeam,
ShootTimer: engine.NewTimer(time.Millisecond * 5000),
})

transform.AppendChild(turret, gun, true)
}

func NewEnemyTurretMissiles(
w donburi.World,
position math.Vec2,
rotation float64,
) {
turret := newEnemyTurret(w, position, rotation)

gun := w.Entry(
w.Create(
transform.Transform,
component.Sprite,
component.Despawnable,
component.Observer,
component.Shooter,
),
)

originalRotation := 90.0
gunT := transform.Transform.Get(gun)
gunT.LocalPosition = position
gunT.LocalRotation = originalRotation + rotation

component.Sprite.SetValue(gun, component.SpriteData{
Image: assets.TurretGunDouble,
Layer: component.SpriteLayerGroundGuns,
Pivot: component.SpritePivotCenter,
OriginalRotation: originalRotation,
})

component.Observer.SetValue(gun, component.ObserverData{
LookFor: query.NewQuery(filter.Contains(component.PlayerAirplane)),
})

component.Shooter.SetValue(gun, component.ShooterData{
Type: component.ShooterTypeMissile,
ShootTimer: engine.NewTimer(time.Millisecond * 5000),
})

transform.AppendChild(turret, gun, true)
}

func newEnemyTurret(
w donburi.World,
position math.Vec2,
rotation float64,
) *donburi.Entry {
turret := w.Entry(
w.Create(
transform.Transform,
component.Sprite,
component.AI,
component.Despawnable,
component.Collider,
component.Health,
),
)

t := transform.Transform.Get(turret)
t.LocalPosition = position
t.LocalRotation = rotation

image := assets.TurretBase
component.Sprite.SetValue(turret, component.SpriteData{
Image: image,
Layer: component.SpriteLayerGroundUnits,
Pivot: component.SpritePivotCenter,
OriginalRotation: 0,
})

width, height := image.Size()

component.Collider.SetValue(turret, component.ColliderData{
Width: float64(width),
Height: float64(height),
Layer: component.CollisionLayerGroundEnemies,
})

health := component.Health.Get(turret)
health.Health = 5
health.DamageIndicator = newDamageIndicator(w, turret)

return turret
}

func newDamageIndicator(w donburi.World, parent *donburi.Entry) *component.SpriteData {
indicator := w.Entry(
w.Create(
Expand Down
25 changes: 20 additions & 5 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ var (
TankBase *ebiten.Image
TankGun *ebiten.Image

TurretBase *ebiten.Image
TurretGunSingle *ebiten.Image
TurretGunDouble *ebiten.Image

LaserSingle *ebiten.Image
Rocket *ebiten.Image
Bullet *ebiten.Image
Missile *ebiten.Image

Health *ebiten.Image
WeaponUpgrade *ebiten.Image
Expand All @@ -59,8 +64,10 @@ var (
)

const (
EnemyClassAirplane = "enemy-airplane"
EnemyClassTank = "enemy-tank"
EnemyClassAirplane = "enemy-airplane"
EnemyClassTank = "enemy-tank"
EnemyClassTurretBeam = "enemy-turret-beam"
EnemyClassTurretMissiles = "enemy-turret-missiles"

TilesetClassTiles = "tiles"
TilesetClassAirplanes = "airplanes"
Expand Down Expand Up @@ -129,8 +136,13 @@ func MustLoadAssets() {
TankBase = loader.MustFindTile(TilesetClassTiles, "tank-base")
TankGun = loader.MustFindTile(TilesetClassTiles, "tank-gun")

TurretBase = loader.MustFindTile(TilesetClassTiles, "turret-base")
TurretGunSingle = loader.MustFindTile(TilesetClassTiles, "turret-gun-single")
TurretGunDouble = loader.MustFindTile(TilesetClassTiles, "turret-gun-double")

LaserSingle = loader.MustFindTile(TilesetClassTiles, "laser-single")
Rocket = loader.MustFindTile(TilesetClassTiles, "rocket")
Bullet = loader.MustFindTile(TilesetClassTiles, "bullet")
Missile = loader.MustFindTile(TilesetClassTiles, "missile")

Health = loader.MustFindTile(TilesetClassTiles, "health")
WeaponUpgrade = loader.MustFindTile(TilesetClassTiles, "weapon-upgrade")
Expand Down Expand Up @@ -237,7 +249,10 @@ func (l *levelLoader) MustLoadLevel(levelPath string) Level {

for _, og := range levelMap.ObjectGroups {
for _, o := range og.Objects {
if o.Class == EnemyClassAirplane || o.Class == EnemyClassTank {
if o.Class == EnemyClassAirplane ||
o.Class == EnemyClassTank ||
o.Class == EnemyClassTurretBeam ||
o.Class == EnemyClassTurretMissiles {
enemy := Enemy{
Class: o.Class,
Position: math.Vec2{
Expand Down
4 changes: 3 additions & 1 deletion assets/levels/level01.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.2" orientation="orthogonal" renderorder="right-down" width="30" height="140" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="81">
<map version="1.9" tiledversion="1.9.2" orientation="orthogonal" renderorder="right-down" width="30" height="140" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="84">
<tileset firstgid="1" source="tiles.tsx"/>
<tileset firstgid="121" source="airplanes.tsx"/>
<layer id="1" name="Background" width="30" height="140">
Expand Down Expand Up @@ -168,6 +168,8 @@
<object id="61" x="104" y="1576">
<polygon points="0,0 -80,0 -80,80 48,80 48,0"/>
</object>
<object id="81" class="enemy-turret-beam" gid="19" x="248" y="1848" width="16" height="16"/>
<object id="82" class="enemy-turret-missiles" gid="18" x="376" y="1880" width="16" height="16"/>
</objectgroup>
<objectgroup id="2" name="Air Units">
<object id="4" class="enemy-airplane" gid="142" x="120" y="1472" width="32" height="32" rotation="180"/>
Expand Down
6 changes: 5 additions & 1 deletion assets/levels/tiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<tileset version="1.9" tiledversion="1.9.2" name="tiles" class="tiles" tilewidth="16" tileheight="16" spacing="1" tilecount="120" columns="12" objectalignment="center">
<image source="../tiles/tiles.png" width="203" height="169"/>
<tile id="0" class="laser-single"/>
<tile id="12" class="rocket"/>
<tile id="3" class="bullet"/>
<tile id="12" class="missile"/>
<tile id="16" class="turret-base"/>
<tile id="17" class="turret-gun-double"/>
<tile id="18" class="turret-gun-single"/>
<tile id="24" class="health"/>
<tile id="25" class="weapon-upgrade"/>
<tile id="26" class="shield"/>
Expand Down
15 changes: 15 additions & 0 deletions component/follower.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package component

import (
"github.com/yohamta/donburi"

"github.com/m110/airplanes/engine"
)

type FollowerData struct {
Target *donburi.Entry
FollowingSpeed float64
FollowingTimer *engine.Timer
}

var Follower = donburi.NewComponentType[FollowerData]()
6 changes: 5 additions & 1 deletion component/health.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package component

import (
"time"

"github.com/yohamta/donburi"

"github.com/m110/airplanes/engine"
Expand Down Expand Up @@ -29,4 +31,6 @@ func (d *HealthData) HideDamageIndicator() {
d.DamageIndicator.Hidden = true
}

var Health = donburi.NewComponentType[HealthData]()
var Health = donburi.NewComponentType[HealthData](HealthData{
DamageIndicatorTimer: engine.NewTimer(time.Millisecond * 100),
})
Loading

0 comments on commit 109f47f

Please sign in to comment.