Skip to content

Commit 69ec988

Browse files
committed
equip
1 parent c7ca705 commit 69ec988

File tree

5 files changed

+180
-79
lines changed

5 files changed

+180
-79
lines changed

rpg/game/game.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
TakeAll
4747
TakeItem
4848
DropItem
49+
EquipItem
4950
QuitGame
5051
CloseWindow
5152
Search //temporary
@@ -98,6 +99,8 @@ type Character struct {
9899
ActionPoints float64
99100
SightRange int
100101
Items []*Item
102+
Helmet *Item
103+
Weapon *Item
101104
}
102105

103106
type Player struct {
@@ -160,10 +163,17 @@ func (level *Level) MoveItem(itemToMove *Item, character *Character) {
160163
func (level *Level) Attack(c1, c2 *Character) {
161164
c1.ActionPoints--
162165
c1AttackPower := c1.Strength
163-
c2.Hitpoints -= c1AttackPower
166+
if c1.Weapon != nil {
167+
c1AttackPower = int(float64(c1AttackPower) * c1.Weapon.power)
168+
}
169+
damage := c1AttackPower
170+
if c2.Helmet != nil {
171+
damage = int(float64(damage) * (1.0 - c2.Helmet.power))
172+
}
173+
c2.Hitpoints -= damage
164174

165175
if c2.Hitpoints > 0 {
166-
level.AddEvent(c1.Name + " Attacked " + c2.Name + " for " + strconv.Itoa(c1AttackPower))
176+
level.AddEvent(c1.Name + " Attacked " + c2.Name + " for " + strconv.Itoa(damage))
167177
} else {
168178
level.AddEvent(c1.Name + " Killed " + c2.Name)
169179
}
@@ -524,6 +534,22 @@ func (game *Game) resolveMovement(pos Pos) {
524534
}
525535
}
526536

537+
func equip(c *Character, itemtoEquip *Item) {
538+
for i, item := range c.Items {
539+
if item == itemtoEquip {
540+
c.Items = append(c.Items[:i], c.Items[i+1:]...)
541+
if itemtoEquip.Typ == Helmet {
542+
c.Helmet = itemtoEquip
543+
544+
} else if itemtoEquip.Typ == Weapon {
545+
c.Weapon = itemtoEquip
546+
}
547+
return
548+
}
549+
}
550+
panic("someone tried to equip a thing they don't have")
551+
}
552+
527553
func (game *Game) handleInput(input *Input) {
528554
level := game.CurrentLevel
529555
p := level.Player
@@ -548,6 +574,8 @@ func (game *Game) handleInput(input *Input) {
548574
case TakeItem:
549575
level.MoveItem(input.Item, &level.Player.Character)
550576
level.LastEvent = PickUp
577+
case EquipItem:
578+
equip(&level.Player.Character, input.Item)
551579
case DropItem:
552580
level.DropItem(input.Item, &level.Player.Character)
553581
level.LastEvent = Drop

rpg/game/items.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
package game
22

3+
type ItemType int
4+
5+
const (
6+
Weapon ItemType = iota
7+
Helmet
8+
Other
9+
)
10+
311
type Item struct {
12+
Typ ItemType
413
Entity
14+
power float64
515
// TODO
616
// weapon - attack bonus
717
// armor - armor class
818
}
919

1020
func NewSword(p Pos) *Item {
11-
return &Item{Entity{p, "Sword", 's'}}
21+
return &Item{Weapon, Entity{p, "Sword", 's'}, 2.0}
1222
}
1323

1424
func NewHelmet(p Pos) *Item {
15-
return &Item{Entity{p, "Helmet", 'h'}}
25+
return &Item{Helmet, Entity{p, "Helmet", 'h'}, .5}
1626
}

rpg/game/maps/level1.map

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#############|##############|###############|#############
1414
#........................................................#
1515
#........................................................#
16-
#........................................................#
16+
#........................S.......S.......................#
1717
#.........h..............................................#
1818
#........................................................#
1919
#.@...........d.............R............................#

rpg/ui2d/inventory.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package ui2d
2+
3+
import (
4+
"fmt"
5+
"github.com/jackmott/rpg/game"
6+
"github.com/veandco/go-sdl2/sdl"
7+
)
8+
9+
func (ui *ui) DrawInventory(level *game.Level) {
10+
11+
playerSrcRect := ui.textureIndex[level.Player.Rune][0]
12+
invRect := ui.getInventoryRect()
13+
ui.renderer.Copy(ui.groundInventoryBackground, nil, invRect)
14+
offset := int32(float64(invRect.H) * 0.05)
15+
16+
ui.renderer.Copy(ui.textureAtlas, &playerSrcRect, &sdl.Rect{invRect.X + invRect.X/4, invRect.Y + offset, invRect.W / 2, invRect.H / 2})
17+
ui.renderer.Copy(ui.slotBackground, nil, ui.getHelmetSlotRect())
18+
if level.Player.Helmet != nil {
19+
ui.renderer.Copy(ui.textureAtlas, &ui.textureIndex[level.Player.Helmet.Rune][0], ui.getHelmetSlotRect())
20+
}
21+
ui.renderer.Copy(ui.slotBackground, nil, ui.getWeaponSlotRect())
22+
if level.Player.Weapon != nil {
23+
ui.renderer.Copy(ui.textureAtlas, &ui.textureIndex[level.Player.Weapon.Rune][0], ui.getWeaponSlotRect())
24+
}
25+
26+
for i, item := range level.Player.Items {
27+
itemSrcRect := ui.textureIndex[item.Rune][0]
28+
if item == ui.draggedItem {
29+
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
30+
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, &sdl.Rect{int32(ui.currentMouseState.pos.X), int32(ui.currentMouseState.pos.Y), itemSize, itemSize})
31+
} else {
32+
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, ui.getInventoryItemRect(i))
33+
}
34+
}
35+
}
36+
37+
func (ui *ui) getHelmetSlotRect() *sdl.Rect {
38+
invRect := ui.getInventoryRect()
39+
slotSize := int32(ItemSizeRatio * float32(ui.winWidth) * 1.05)
40+
41+
r := sdl.Rect{(invRect.X*2+invRect.W)/2 - slotSize/2, invRect.Y, slotSize, slotSize}
42+
fmt.Println("x:", r.X, "y:", r.Y, "w:", r.W, "h:", r.H)
43+
return &r
44+
}
45+
46+
func (ui *ui) getWeaponSlotRect() *sdl.Rect {
47+
invRect := ui.getInventoryRect()
48+
slotSize := int32(ItemSizeRatio * float32(ui.winWidth) * 1.05)
49+
yoffset := int32(float32(invRect.H) * 0.18)
50+
xoffset := int32(float32(invRect.W) * 0.18)
51+
r := sdl.Rect{invRect.X + xoffset, invRect.Y + yoffset, slotSize, slotSize}
52+
fmt.Println("x:", r.X, "y:", r.Y, "w:", r.W, "h:", r.H)
53+
return &r
54+
}
55+
56+
func (ui *ui) getInventoryRect() *sdl.Rect {
57+
invWidth := int32(float32(ui.winWidth) * 0.40)
58+
invHeight := int32(float32(ui.winHeight) * 0.75)
59+
offsetX := (int32(ui.winWidth) - invWidth) / 2
60+
offsetY := (int32(ui.winHeight) - invHeight) / 2
61+
return &sdl.Rect{offsetX, offsetY, invWidth, invHeight}
62+
}
63+
64+
func (ui *ui) getInventoryItemRect(i int) *sdl.Rect {
65+
invRect := ui.getInventoryRect()
66+
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
67+
return &sdl.Rect{invRect.X + int32(i)*itemSize, invRect.Y + invRect.H - itemSize, itemSize, itemSize}
68+
}
69+
70+
func (ui *ui) CheckEquippedItem() *game.Item {
71+
mousePos := ui.currentMouseState.pos
72+
if ui.draggedItem.Typ == game.Weapon {
73+
r := ui.getWeaponSlotRect()
74+
if r.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
75+
return ui.draggedItem
76+
}
77+
} else if ui.draggedItem.Typ == game.Helmet {
78+
r := ui.getHelmetSlotRect()
79+
if r.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
80+
return ui.draggedItem
81+
}
82+
}
83+
return nil
84+
}
85+
86+
func (ui *ui) CheckDroppedItem() *game.Item {
87+
invRect := ui.getInventoryRect()
88+
mousePos := ui.currentMouseState.pos
89+
if invRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
90+
return nil
91+
}
92+
return ui.draggedItem
93+
}
94+
95+
func (ui *ui) CheckInventoryItems(level *game.Level) *game.Item {
96+
if ui.currentMouseState.leftButton {
97+
mousePos := ui.currentMouseState.pos
98+
for i, item := range level.Player.Items {
99+
itemRect := ui.getInventoryItemRect(i)
100+
if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
101+
return item
102+
}
103+
}
104+
}
105+
return nil
106+
}
107+
108+
func (ui *ui) CheckGroundItems(level *game.Level) *game.Item {
109+
if !ui.currentMouseState.leftButton && ui.prevMouseState.leftButton {
110+
items := level.Items[level.Player.Pos]
111+
mousePos := ui.currentMouseState.pos
112+
for i, item := range items {
113+
itemRect := ui.getGroundItemRect(i)
114+
if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
115+
return item
116+
}
117+
}
118+
}
119+
return nil
120+
}

rpg/ui2d/ui2d.go

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type ui struct {
7575

7676
eventBackground *sdl.Texture
7777
groundInventoryBackground *sdl.Texture
78+
slotBackground *sdl.Texture
7879

7980
str2TexSmall map[string]*sdl.Texture
8081
str2TexMedium map[string]*sdl.Texture
@@ -143,6 +144,8 @@ func NewUI(inputChan chan *game.Input, levelChan chan *game.Level) *ui {
143144
ui.groundInventoryBackground = ui.GetSinglePixelTex(sdl.Color{149, 84, 19, 200})
144145
ui.groundInventoryBackground.SetBlendMode(sdl.BLENDMODE_BLEND)
145146

147+
ui.slotBackground = ui.GetSinglePixelTex(sdl.Color{0, 0, 0, 0})
148+
146149
//if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
147150
err = mix.OpenAudio(22050, mix.DEFAULT_FORMAT, 2, 4096)
148151
if err != nil {
@@ -336,47 +339,6 @@ func init() {
336339
}*/
337340
}
338341

339-
func (ui *ui) DrawInventory(level *game.Level) {
340-
341-
playerSrcRect := ui.textureIndex[level.Player.Rune][0]
342-
invRect := ui.getInventoryRect()
343-
ui.renderer.Copy(ui.groundInventoryBackground, nil, invRect)
344-
ui.renderer.Copy(ui.textureAtlas, &playerSrcRect, &sdl.Rect{invRect.X + invRect.X/4, invRect.Y, invRect.W / 2, invRect.H / 2})
345-
346-
for i, item := range level.Player.Items {
347-
itemSrcRect := ui.textureIndex[item.Rune][0]
348-
if item == ui.draggedItem {
349-
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
350-
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, &sdl.Rect{int32(ui.currentMouseState.pos.X), int32(ui.currentMouseState.pos.Y), itemSize, itemSize})
351-
} else {
352-
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, ui.getInventoryItemRect(i))
353-
}
354-
}
355-
}
356-
357-
func (ui *ui) getInventoryRect() *sdl.Rect {
358-
invWidth := int32(float32(ui.winWidth) * 0.40)
359-
invHeight := int32(float32(ui.winHeight) * 0.75)
360-
offsetX := (int32(ui.winWidth) - invWidth) / 2
361-
offsetY := (int32(ui.winHeight) - invHeight) / 2
362-
return &sdl.Rect{offsetX, offsetY, invWidth, invHeight}
363-
}
364-
365-
func (ui *ui) getInventoryItemRect(i int) *sdl.Rect {
366-
invRect := ui.getInventoryRect()
367-
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
368-
return &sdl.Rect{invRect.X + int32(i)*itemSize, invRect.Y + invRect.H - itemSize, itemSize, itemSize}
369-
}
370-
371-
func (ui *ui) CheckDroppedItem(level *game.Level) *game.Item {
372-
invRect := ui.getInventoryRect()
373-
mousePos := ui.currentMouseState.pos
374-
if invRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
375-
return nil
376-
}
377-
return ui.draggedItem
378-
}
379-
380342
func (ui *ui) Draw(level *game.Level) {
381343

382344
if ui.centerX == -1 && ui.centerY == -1 {
@@ -523,33 +485,6 @@ func (ui *ui) GetSinglePixelTex(color sdl.Color) *sdl.Texture {
523485
return tex
524486
}
525487

526-
func (ui *ui) CheckInventoryItems(level *game.Level) *game.Item {
527-
if ui.currentMouseState.leftButton {
528-
mousePos := ui.currentMouseState.pos
529-
for i, item := range level.Player.Items {
530-
itemRect := ui.getInventoryItemRect(i)
531-
if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
532-
return item
533-
}
534-
}
535-
}
536-
return nil
537-
}
538-
539-
func (ui *ui) CheckGroundItems(level *game.Level) *game.Item {
540-
if !ui.currentMouseState.leftButton && ui.prevMouseState.leftButton {
541-
items := level.Items[level.Player.Pos]
542-
mousePos := ui.currentMouseState.pos
543-
for i, item := range items {
544-
itemRect := ui.getGroundItemRect(i)
545-
if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
546-
return item
547-
}
548-
}
549-
}
550-
return nil
551-
}
552-
553488
func (ui *ui) Run() {
554489
var newLevel *game.Level
555490
ui.prevMouseState = getMouseState()
@@ -591,16 +526,24 @@ func (ui *ui) Run() {
591526

592527
//have we stopped dragging?
593528
if ui.draggedItem != nil && !ui.currentMouseState.leftButton && ui.prevMouseState.leftButton {
594-
item := ui.CheckDroppedItem(newLevel)
529+
530+
item := ui.CheckEquippedItem()
595531
if item != nil {
596-
input.Typ = game.DropItem
597-
input.Item = ui.draggedItem
532+
input.Typ = game.EquipItem
533+
input.Item = item
598534
ui.draggedItem = nil
599535
}
600-
}
601-
if ui.currentMouseState.leftButton && ui.draggedItem != nil {
536+
if ui.draggedItem != nil {
537+
item := ui.CheckDroppedItem()
538+
if item != nil {
539+
input.Typ = game.DropItem
540+
input.Item = item
541+
ui.draggedItem = nil
542+
}
543+
}
602544

603-
} else {
545+
}
546+
if !ui.currentMouseState.leftButton || ui.draggedItem == nil {
604547
ui.draggedItem = ui.CheckInventoryItems(newLevel)
605548
}
606549
ui.DrawInventory(newLevel)

0 commit comments

Comments
 (0)