Skip to content

Commit c7ca705

Browse files
committed
drag and drop
1 parent 720c10e commit c7ca705

File tree

3 files changed

+111
-23
lines changed

3 files changed

+111
-23
lines changed

rpg/game/game.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
Right
4646
TakeAll
4747
TakeItem
48+
DropItem
4849
QuitGame
4950
CloseWindow
5051
Search //temporary
@@ -112,6 +113,7 @@ const (
112113
Hit
113114
Portal
114115
PickUp
116+
Drop
115117
)
116118

117119
type Level struct {
@@ -126,6 +128,18 @@ type Level struct {
126128
LastEvent GameEvent
127129
}
128130

131+
func (level *Level) DropItem(itemToDrop *Item, character *Character) {
132+
pos := character.Pos
133+
items := character.Items
134+
for i, item := range items {
135+
if item == itemToDrop {
136+
character.Items = append(character.Items[:i], character.Items[i+1:]...)
137+
level.Items[pos] = append(level.Items[pos], item)
138+
level.AddEvent(character.Name + " dropped:" + item.Name)
139+
return
140+
}
141+
}
142+
}
129143
func (level *Level) MoveItem(itemToMove *Item, character *Character) {
130144
fmt.Println("Move Item!")
131145
pos := character.Pos
@@ -534,6 +548,9 @@ func (game *Game) handleInput(input *Input) {
534548
case TakeItem:
535549
level.MoveItem(input.Item, &level.Player.Character)
536550
level.LastEvent = PickUp
551+
case DropItem:
552+
level.DropItem(input.Item, &level.Player.Character)
553+
level.LastEvent = Drop
537554
case CloseWindow:
538555
close(input.LevelChannel)
539556
chanIndex := 0

rpg/game/maps/level2.map

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
################
2-
#..............#
3-
#..............#
4-
#.......u......#
1+
################ ############
2+
#..............###h###......#
3+
#..............|.....|..ss..#
4+
#.......u......#############
55
################

rpg/ui2d/ui2d.go

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import (
1414
"strings"
1515
)
1616

17+
const ItemSizeRatio = .033
18+
1719
type mouseState struct {
1820
leftButton bool
1921
rightButton bool
2022
pos game.Pos
2123
}
2224

23-
func getMouseState() mouseState {
25+
func getMouseState() *mouseState {
2426
mouseX, mouseY, mouseButtonState := sdl.GetMouseState()
2527
leftButton := mouseButtonState & sdl.ButtonLMask()
2628
rightButton := mouseButtonState & sdl.ButtonRMask()
@@ -29,7 +31,7 @@ func getMouseState() mouseState {
2931
result.leftButton = !(leftButton == 0)
3032
result.rightButton = !(rightButton == 0)
3133

32-
return result
34+
return &result
3335
}
3436

3537
type sounds struct {
@@ -52,6 +54,7 @@ const (
5254

5355
type ui struct {
5456
state uiState
57+
draggedItem *game.Item
5558
sounds sounds
5659
winWidth int
5760
winHeight int
@@ -76,6 +79,9 @@ type ui struct {
7679
str2TexSmall map[string]*sdl.Texture
7780
str2TexMedium map[string]*sdl.Texture
7881
str2TexLarge map[string]*sdl.Texture
82+
83+
currentMouseState *mouseState
84+
prevMouseState *mouseState
7985
}
8086

8187
func NewUI(inputChan chan *game.Input, levelChan chan *game.Level) *ui {
@@ -102,7 +108,7 @@ func NewUI(inputChan chan *game.Input, levelChan chan *game.Level) *ui {
102108
panic(err)
103109
}
104110

105-
sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "1")
111+
//sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "1")
106112

107113
ui.textureAtlas = ui.imgFileToTexture("ui2d/assets/tiles.png")
108114
ui.loadTextureIndex()
@@ -134,7 +140,7 @@ func NewUI(inputChan chan *game.Input, levelChan chan *game.Level) *ui {
134140
ui.eventBackground = ui.GetSinglePixelTex(sdl.Color{0, 0, 0, 128})
135141
ui.eventBackground.SetBlendMode(sdl.BLENDMODE_BLEND)
136142

137-
ui.groundInventoryBackground = ui.GetSinglePixelTex(sdl.Color{255, 0, 0, 128})
143+
ui.groundInventoryBackground = ui.GetSinglePixelTex(sdl.Color{149, 84, 19, 200})
138144
ui.groundInventoryBackground.SetBlendMode(sdl.BLENDMODE_BLEND)
139145

140146
//if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
@@ -331,7 +337,44 @@ func init() {
331337
}
332338

333339
func (ui *ui) DrawInventory(level *game.Level) {
334-
ui.renderer.Copy(ui.groundInventoryBackground, nil, &sdl.Rect{100, 100, 500, 500})
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
335378
}
336379

337380
func (ui *ui) Draw(level *game.Level) {
@@ -411,7 +454,7 @@ func (ui *ui) Draw(level *game.Level) {
411454
}
412455

413456
// Render Player
414-
playerSrcRect := ui.textureIndex['@'][0]
457+
playerSrcRect := ui.textureIndex[level.Player.Rune][0]
415458
ui.renderer.Copy(ui.textureAtlas, &playerSrcRect, &sdl.Rect{int32(level.Player.X)*32 + offsetX, int32(level.Player.Y)*32 + offsetY, 32, 32})
416459

417460
// Event UI Begin
@@ -440,8 +483,10 @@ func (ui *ui) Draw(level *game.Level) {
440483
// Inventory UI
441484
groundInvStart := int32(float64(ui.winWidth) * .9)
442485
groundInvWidth := int32(ui.winWidth) - groundInvStart
443-
ui.renderer.Copy(ui.groundInventoryBackground, nil, &sdl.Rect{groundInvStart, int32(ui.winHeight - 32), groundInvWidth, int32(32)})
486+
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
487+
ui.renderer.Copy(ui.groundInventoryBackground, nil, &sdl.Rect{groundInvStart, int32(ui.winHeight) - itemSize, groundInvWidth, itemSize})
444488
items := level.Items[level.Player.Pos]
489+
445490
for i, item := range items {
446491
itemSrcRect := ui.textureIndex[item.Rune][0]
447492
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, ui.getGroundItemRect(i))
@@ -451,7 +496,8 @@ func (ui *ui) Draw(level *game.Level) {
451496
}
452497

453498
func (ui *ui) getGroundItemRect(i int) *sdl.Rect {
454-
return &sdl.Rect{int32(ui.winWidth - 32 - i*32), int32(ui.winHeight - 32), 32, 32}
499+
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
500+
return &sdl.Rect{int32(ui.winWidth) - itemSize - int32(i)*itemSize, int32(ui.winHeight) - itemSize, itemSize, itemSize}
455501
}
456502

457503
func (ui *ui) keyDownOnce(key uint8) bool {
@@ -477,11 +523,23 @@ func (ui *ui) GetSinglePixelTex(color sdl.Color) *sdl.Texture {
477523
return tex
478524
}
479525

480-
func (ui *ui) CheckItems(level *game.Level, prevMouseState, currentMouseState mouseState) *game.Item {
481-
if !currentMouseState.leftButton && prevMouseState.leftButton {
482-
fmt.Println("Got Click! X,Y:", currentMouseState.pos)
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 {
483541
items := level.Items[level.Player.Pos]
484-
mousePos := currentMouseState.pos
542+
mousePos := ui.currentMouseState.pos
485543
for i, item := range items {
486544
itemRect := ui.getGroundItemRect(i)
487545
if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
@@ -494,7 +552,7 @@ func (ui *ui) CheckItems(level *game.Level, prevMouseState, currentMouseState mo
494552

495553
func (ui *ui) Run() {
496554
var newLevel *game.Level
497-
prevMouseState := getMouseState()
555+
ui.prevMouseState = getMouseState()
498556

499557
for {
500558
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
@@ -507,7 +565,7 @@ func (ui *ui) Run() {
507565
}
508566
}
509567
}
510-
currentMouseState := getMouseState()
568+
ui.currentMouseState = getMouseState()
511569

512570
// Suspect quick keypresses sometimes cause channel gridlock
513571
var ok bool
@@ -528,19 +586,32 @@ func (ui *ui) Run() {
528586
}
529587

530588
ui.Draw(newLevel)
589+
var input game.Input
531590
if ui.state == UIInventory {
532-
fmt.Println("Drawing Inventory")
591+
592+
//have we stopped dragging?
593+
if ui.draggedItem != nil && !ui.currentMouseState.leftButton && ui.prevMouseState.leftButton {
594+
item := ui.CheckDroppedItem(newLevel)
595+
if item != nil {
596+
input.Typ = game.DropItem
597+
input.Item = ui.draggedItem
598+
ui.draggedItem = nil
599+
}
600+
}
601+
if ui.currentMouseState.leftButton && ui.draggedItem != nil {
602+
603+
} else {
604+
ui.draggedItem = ui.CheckInventoryItems(newLevel)
605+
}
533606
ui.DrawInventory(newLevel)
534607
}
535608
ui.renderer.Present()
536609

537-
var input game.Input
538-
item := ui.CheckItems(newLevel, prevMouseState, currentMouseState)
610+
item := ui.CheckGroundItems(newLevel)
539611
if item != nil {
540612
input.Typ = game.TakeItem
541613
input.Item = item
542614
}
543-
544615
if sdl.GetKeyboardFocus() == ui.window || sdl.GetMouseFocus() == ui.window {
545616

546617
if ui.keyDownOnce(sdl.SCANCODE_UP) {
@@ -570,7 +641,7 @@ func (ui *ui) Run() {
570641
ui.inputChan <- &input
571642
}
572643
}
573-
prevMouseState = currentMouseState
644+
ui.prevMouseState = ui.currentMouseState
574645
sdl.Delay(10)
575646

576647
}

0 commit comments

Comments
 (0)