Skip to content

Commit

Permalink
drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmott committed Apr 4, 2018
1 parent 720c10e commit c7ca705
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 23 deletions.
17 changes: 17 additions & 0 deletions rpg/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
Right
TakeAll
TakeItem
DropItem
QuitGame
CloseWindow
Search //temporary
Expand Down Expand Up @@ -112,6 +113,7 @@ const (
Hit
Portal
PickUp
Drop
)

type Level struct {
Expand All @@ -126,6 +128,18 @@ type Level struct {
LastEvent GameEvent
}

func (level *Level) DropItem(itemToDrop *Item, character *Character) {
pos := character.Pos
items := character.Items
for i, item := range items {
if item == itemToDrop {
character.Items = append(character.Items[:i], character.Items[i+1:]...)
level.Items[pos] = append(level.Items[pos], item)
level.AddEvent(character.Name + " dropped:" + item.Name)
return
}
}
}
func (level *Level) MoveItem(itemToMove *Item, character *Character) {
fmt.Println("Move Item!")
pos := character.Pos
Expand Down Expand Up @@ -534,6 +548,9 @@ func (game *Game) handleInput(input *Input) {
case TakeItem:
level.MoveItem(input.Item, &level.Player.Character)
level.LastEvent = PickUp
case DropItem:
level.DropItem(input.Item, &level.Player.Character)
level.LastEvent = Drop
case CloseWindow:
close(input.LevelChannel)
chanIndex := 0
Expand Down
8 changes: 4 additions & 4 deletions rpg/game/maps/level2.map
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
################
#..............#
#..............#
#.......u......#
################ ############
#..............###h###......#
#..............|.....|..ss..#
#.......u......#############
################
109 changes: 90 additions & 19 deletions rpg/ui2d/ui2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
"strings"
)

const ItemSizeRatio = .033

type mouseState struct {
leftButton bool
rightButton bool
pos game.Pos
}

func getMouseState() mouseState {
func getMouseState() *mouseState {
mouseX, mouseY, mouseButtonState := sdl.GetMouseState()
leftButton := mouseButtonState & sdl.ButtonLMask()
rightButton := mouseButtonState & sdl.ButtonRMask()
Expand All @@ -29,7 +31,7 @@ func getMouseState() mouseState {
result.leftButton = !(leftButton == 0)
result.rightButton = !(rightButton == 0)

return result
return &result
}

type sounds struct {
Expand All @@ -52,6 +54,7 @@ const (

type ui struct {
state uiState
draggedItem *game.Item
sounds sounds
winWidth int
winHeight int
Expand All @@ -76,6 +79,9 @@ type ui struct {
str2TexSmall map[string]*sdl.Texture
str2TexMedium map[string]*sdl.Texture
str2TexLarge map[string]*sdl.Texture

currentMouseState *mouseState
prevMouseState *mouseState
}

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

sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "1")
//sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "1")

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

ui.groundInventoryBackground = ui.GetSinglePixelTex(sdl.Color{255, 0, 0, 128})
ui.groundInventoryBackground = ui.GetSinglePixelTex(sdl.Color{149, 84, 19, 200})
ui.groundInventoryBackground.SetBlendMode(sdl.BLENDMODE_BLEND)

//if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
Expand Down Expand Up @@ -331,7 +337,44 @@ func init() {
}

func (ui *ui) DrawInventory(level *game.Level) {
ui.renderer.Copy(ui.groundInventoryBackground, nil, &sdl.Rect{100, 100, 500, 500})

playerSrcRect := ui.textureIndex[level.Player.Rune][0]
invRect := ui.getInventoryRect()
ui.renderer.Copy(ui.groundInventoryBackground, nil, invRect)
ui.renderer.Copy(ui.textureAtlas, &playerSrcRect, &sdl.Rect{invRect.X + invRect.X/4, invRect.Y, invRect.W / 2, invRect.H / 2})

for i, item := range level.Player.Items {
itemSrcRect := ui.textureIndex[item.Rune][0]
if item == ui.draggedItem {
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, &sdl.Rect{int32(ui.currentMouseState.pos.X), int32(ui.currentMouseState.pos.Y), itemSize, itemSize})
} else {
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, ui.getInventoryItemRect(i))
}
}
}

func (ui *ui) getInventoryRect() *sdl.Rect {
invWidth := int32(float32(ui.winWidth) * 0.40)
invHeight := int32(float32(ui.winHeight) * 0.75)
offsetX := (int32(ui.winWidth) - invWidth) / 2
offsetY := (int32(ui.winHeight) - invHeight) / 2
return &sdl.Rect{offsetX, offsetY, invWidth, invHeight}
}

func (ui *ui) getInventoryItemRect(i int) *sdl.Rect {
invRect := ui.getInventoryRect()
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
return &sdl.Rect{invRect.X + int32(i)*itemSize, invRect.Y + invRect.H - itemSize, itemSize, itemSize}
}

func (ui *ui) CheckDroppedItem(level *game.Level) *game.Item {
invRect := ui.getInventoryRect()
mousePos := ui.currentMouseState.pos
if invRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
return nil
}
return ui.draggedItem
}

func (ui *ui) Draw(level *game.Level) {
Expand Down Expand Up @@ -411,7 +454,7 @@ func (ui *ui) Draw(level *game.Level) {
}

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

// Event UI Begin
Expand Down Expand Up @@ -440,8 +483,10 @@ func (ui *ui) Draw(level *game.Level) {
// Inventory UI
groundInvStart := int32(float64(ui.winWidth) * .9)
groundInvWidth := int32(ui.winWidth) - groundInvStart
ui.renderer.Copy(ui.groundInventoryBackground, nil, &sdl.Rect{groundInvStart, int32(ui.winHeight - 32), groundInvWidth, int32(32)})
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
ui.renderer.Copy(ui.groundInventoryBackground, nil, &sdl.Rect{groundInvStart, int32(ui.winHeight) - itemSize, groundInvWidth, itemSize})
items := level.Items[level.Player.Pos]

for i, item := range items {
itemSrcRect := ui.textureIndex[item.Rune][0]
ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, ui.getGroundItemRect(i))
Expand All @@ -451,7 +496,8 @@ func (ui *ui) Draw(level *game.Level) {
}

func (ui *ui) getGroundItemRect(i int) *sdl.Rect {
return &sdl.Rect{int32(ui.winWidth - 32 - i*32), int32(ui.winHeight - 32), 32, 32}
itemSize := int32(ItemSizeRatio * float32(ui.winWidth))
return &sdl.Rect{int32(ui.winWidth) - itemSize - int32(i)*itemSize, int32(ui.winHeight) - itemSize, itemSize, itemSize}
}

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

func (ui *ui) CheckItems(level *game.Level, prevMouseState, currentMouseState mouseState) *game.Item {
if !currentMouseState.leftButton && prevMouseState.leftButton {
fmt.Println("Got Click! X,Y:", currentMouseState.pos)
func (ui *ui) CheckInventoryItems(level *game.Level) *game.Item {
if ui.currentMouseState.leftButton {
mousePos := ui.currentMouseState.pos
for i, item := range level.Player.Items {
itemRect := ui.getInventoryItemRect(i)
if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
return item
}
}
}
return nil
}

func (ui *ui) CheckGroundItems(level *game.Level) *game.Item {
if !ui.currentMouseState.leftButton && ui.prevMouseState.leftButton {
items := level.Items[level.Player.Pos]
mousePos := currentMouseState.pos
mousePos := ui.currentMouseState.pos
for i, item := range items {
itemRect := ui.getGroundItemRect(i)
if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) {
Expand All @@ -494,7 +552,7 @@ func (ui *ui) CheckItems(level *game.Level, prevMouseState, currentMouseState mo

func (ui *ui) Run() {
var newLevel *game.Level
prevMouseState := getMouseState()
ui.prevMouseState = getMouseState()

for {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
Expand All @@ -507,7 +565,7 @@ func (ui *ui) Run() {
}
}
}
currentMouseState := getMouseState()
ui.currentMouseState = getMouseState()

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

ui.Draw(newLevel)
var input game.Input
if ui.state == UIInventory {
fmt.Println("Drawing Inventory")

//have we stopped dragging?
if ui.draggedItem != nil && !ui.currentMouseState.leftButton && ui.prevMouseState.leftButton {
item := ui.CheckDroppedItem(newLevel)
if item != nil {
input.Typ = game.DropItem
input.Item = ui.draggedItem
ui.draggedItem = nil
}
}
if ui.currentMouseState.leftButton && ui.draggedItem != nil {

} else {
ui.draggedItem = ui.CheckInventoryItems(newLevel)
}
ui.DrawInventory(newLevel)
}
ui.renderer.Present()

var input game.Input
item := ui.CheckItems(newLevel, prevMouseState, currentMouseState)
item := ui.CheckGroundItems(newLevel)
if item != nil {
input.Typ = game.TakeItem
input.Item = item
}

if sdl.GetKeyboardFocus() == ui.window || sdl.GetMouseFocus() == ui.window {

if ui.keyDownOnce(sdl.SCANCODE_UP) {
Expand Down Expand Up @@ -570,7 +641,7 @@ func (ui *ui) Run() {
ui.inputChan <- &input
}
}
prevMouseState = currentMouseState
ui.prevMouseState = ui.currentMouseState
sdl.Delay(10)

}
Expand Down

0 comments on commit c7ca705

Please sign in to comment.