Skip to content

Commit

Permalink
unit: Added Robot with ability Hybrid
Browse files Browse the repository at this point in the history
  • Loading branch information
xescugc committed Jun 6, 2024
1 parent 12e327a commit 8cfd9df
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 30 deletions.
8 changes: 6 additions & 2 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,12 @@ type SyncStateUnitPayload struct {
PlayerLineID int
CurrentLineID int

MaxHealth float64
Health float64
MaxHealth float64
Health float64

MaxShield float64
Shield float64

MovementSpeed float64
Bounty int

Expand Down
Binary file added assets/ShieldBarMiniProgress.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,8 @@ var LifeBarMiniProgress_png []byte
//go:embed LifeBarMiniUnder.png
var LifeBarMiniUnder_png []byte

//go:embed ShieldBarMiniProgress.png
var ShieldBarMiniProgress_png []byte

//go:embed Arrow.png
var Arrow_png []byte
8 changes: 5 additions & 3 deletions assets/units.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@
"movement_speed": 50
},
"robot":{
"health": 10,
"gold": 10,
"health": 5,
"shield":5,
"gold": 20,
"keybind":"9",
"environment":"terrestrial",
"movement_speed": 50
"movement_speed": 40,
"abilities":["hybrid"]
},
"monkey_boxer":{
"health": 10,
Expand Down
22 changes: 22 additions & 0 deletions client/game/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const (
buffBurrowedReadyKey string = "buff-burrowed-ready"

buffResurrectingKey string = "buff-resurrecting"

lifeBarProgressKey = "life-bar-progress"
lifeBarUnderKey = "life-bar-under"
shieldBarProgressKey = "shield-bar-progress"
)

// ImagesCache is a simple cache for all the images, so instead
Expand Down Expand Up @@ -74,6 +78,24 @@ func init() {
panic(err)
}
imagesCache.images[buffResurrectingKey] = ebiten.NewImageFromImage(sdd)

lbpi, _, err := image.Decode(bytes.NewReader(assets.LifeBarMiniProgress_png))
if err != nil {
panic(err)
}
imagesCache.images[lifeBarProgressKey] = ebiten.NewImageFromImage(lbpi)

lbui, _, err := image.Decode(bytes.NewReader(assets.LifeBarMiniUnder_png))
if err != nil {
panic(err)
}
imagesCache.images[lifeBarUnderKey] = ebiten.NewImageFromImage(lbui)

sbpi, _, err := image.Decode(bytes.NewReader(assets.ShieldBarMiniProgress_png))
if err != nil {
panic(err)
}
imagesCache.images[shieldBarProgressKey] = ebiten.NewImageFromImage(sbpi)
}

// Get will return the image from 'key', if it does not
Expand Down
37 changes: 19 additions & 18 deletions client/game/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type Lines struct {
game *Game

tilesetLogicImage image.Image
lifeBarProgress image.Image
lifeBarUnder image.Image
}

var (
Expand All @@ -39,21 +37,9 @@ func NewLines(g *Game) (*Lines, error) {
return nil, err
}

lbpi, _, err := image.Decode(bytes.NewReader(assets.LifeBarMiniProgress_png))
if err != nil {
return nil, err
}

lbui, _, err := image.Decode(bytes.NewReader(assets.LifeBarMiniUnder_png))
if err != nil {
return nil, err
}

ls := &Lines{
game: g,
tilesetLogicImage: ebiten.NewImageFromImage(tli).SubImage(image.Rect(4*16, 5*16, 4*16+16, 5*16+16)),
lifeBarProgress: ebiten.NewImageFromImage(lbpi),
lifeBarUnder: ebiten.NewImageFromImage(lbui),
}

return ls, nil
Expand Down Expand Up @@ -124,13 +110,28 @@ func (ls *Lines) DrawUnit(screen *ebiten.Image, c *CameraStore, u *store.Unit) {

// Only draw the Health bar if the unit has been hit
if u.Health != u.MaxHealth {
lbui := imagesCache.Get(lifeBarUnderKey)
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(lbui.Bounds().Dy()))
screen.DrawImage(lbui, op)

lbpi := imagesCache.Get(lifeBarProgressKey)
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(lbpi.Bounds().Dy()))
screen.DrawImage(lbpi.SubImage(image.Rect(0, 0, int(float64(lbpi.Bounds().Dx())*(u.Health/u.MaxHealth)), lbpi.Bounds().Dy())).(*ebiten.Image), op)
}

// Only draw the Shield bar if the unit has been hit
if u.Shield != u.MaxShield && u.Shield != 0 {
lbui := imagesCache.Get(lifeBarUnderKey)
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(ls.lifeBarUnder.Bounds().Dy()))
screen.DrawImage(ls.lifeBarUnder.(*ebiten.Image), op)
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(lbui.Bounds().Dy()))
screen.DrawImage(lbui, op)

sbpi := imagesCache.Get(shieldBarProgressKey)
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(ls.lifeBarProgress.Bounds().Dy()))
screen.DrawImage(ls.lifeBarProgress.(*ebiten.Image).SubImage(image.Rect(0, 0, int(float64(ls.lifeBarProgress.Bounds().Dx())*(u.Health/u.MaxHealth)), ls.lifeBarProgress.Bounds().Dy())).(*ebiten.Image), op)
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(sbpi.Bounds().Dy()))
screen.DrawImage(sbpi.SubImage(image.Rect(0, 0, int(float64(sbpi.Bounds().Dx())*(u.Shield/u.MaxShield)), sbpi.Bounds().Dy())).(*ebiten.Image), op)
}

// TODO: Animation logic
Expand Down
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
53 changes: 50 additions & 3 deletions store/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ type Unit struct {
PlayerLineID int
CurrentLineID int

MaxHealth float64
Health float64
MaxHealth float64
Health float64

MaxShield float64
Shield float64

MovementSpeed float64
Bounty int

Expand Down Expand Up @@ -271,6 +275,32 @@ func (u *Unit) WasResurrected() bool {
return ab.Resurrected
}

func (u *Unit) Hybrid(cp, op int) {
if cp < op {
return
}
// This is the Percentage Difference
// TODO: Potentially show this as a Buff
p := float64(((cp - op) / ((cp + op) / 2)) * 100)
bu := unit.Units[u.Type]
uu := unitUpdate(u.Level, u.Type, bu.Stats)

var (
hp float64 = 1
sp float64 = 1
)
if u.Shield != u.MaxShield {
hp = (u.Health / u.MaxHealth)
sp = (u.Shield / u.MaxShield)
}
// base + base * % increase / 100
u.MaxHealth = (uu.Health + u.Health*p/100) * hp
u.Health = u.MaxHealth * hp
u.MovementSpeed = uu.MovementSpeed + uu.MovementSpeed*p/100
u.MaxShield = uu.Shield + uu.Shield*p/100
u.Shield = u.MaxShield * sp
}

type Player struct {
ID string
Name string
Expand Down Expand Up @@ -599,12 +629,18 @@ func (ls *Lines) Reduce(state, a interface{}) interface{} {
CurrentLineID: act.SummonUnit.CurrentLineID,
MaxHealth: float64(uu.Current.Health),
Health: float64(uu.Current.Health),
MaxShield: uu.Current.Shield,
Shield: uu.Current.Shield,
Level: uu.Level,
MovementSpeed: uu.Current.MovementSpeed,
Bounty: uu.Current.Income,
CreatedAt: time.Now(),
}

if u.HasAbility(ability.Hybrid) {
u.Hybrid(cp.Income, ls.findPlayerByLineID(act.SummonUnit.CurrentLineID).Income)
}

u.Path = l.Graph.AStar(u.X, u.Y, u.MovementSpeed, u.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, bu.Environment, atScale)
u.HashPath = graph.HashSteps(u.Path)
l.Units[u.ID] = u
Expand Down Expand Up @@ -914,7 +950,14 @@ func (ls *Lines) moveLineUnitsTo(lstate LinesState, lid int, t time.Time) {
}
if minDistUnit != nil {
// Tower Attack
minDistUnit.Health -= tw.Stats.Damage
if minDistUnit.Shield != 0 {
minDistUnit.Shield -= tw.Stats.Damage
if minDistUnit.Shield < 0 {
minDistUnit.Shield = 0
}
} else {
minDistUnit.Health -= tw.Stats.Damage
}
if minDistUnit.Health <= minDistUnit.MaxHealth/2 && minDistUnit.HasAbility(ability.Burrow) && !minDistUnit.WasBurrowed() {
if minDistUnit.Abilities == nil {
minDistUnit.Abilities = make(map[string]interface{})
Expand Down Expand Up @@ -1029,6 +1072,7 @@ func unitUpdate(nlvl int, ut string, u unit.Stats) unit.Stats {
u.Health = float64(levelToValue(nlvl, int(bu.Health)))
u.Gold = levelToValue(nlvl, bu.Gold)
u.Income = int(math.Round(float64(u.Gold) / float64(incomeFactor)))
u.Shield = float64(levelToValue(nlvl, int(bu.Shield)))

return u
}
Expand Down Expand Up @@ -1085,5 +1129,8 @@ func (ls *Lines) changeUnitLine(lstate LinesState, u *Unit, nlid int) {
u.HashPath = graph.HashSteps(u.Path)

u.CreatedAt = time.Now()
if u.HasAbility(ability.Hybrid) {
u.Hybrid(lstate.Players[u.PlayerID].Income, ls.findPlayerByLineID(nlid).Income)
}
nl.Units[u.ID] = u
}
1 change: 1 addition & 0 deletions unit/ability/ability.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ const (
Split Ability = iota
Burrow
Resurrection
Hybrid
)
12 changes: 8 additions & 4 deletions unit/ability/ability_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions unit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Unit struct {

type Stats struct {
Health float64 `json:"health"`
Shield float64 `json:"shield"`
Income int `json:"income"`
Gold int `json:"gold"`
MovementSpeed float64 `json:"movement_speed"`
Expand Down

0 comments on commit 8cfd9df

Please sign in to comment.