Beispiel #1
0
// returns if we are done or not
func key_handler() bool {
	var keys []uint8 = sdl.GetKeyState()

	if keys[sdl.K_i] != 0 {
		view_z += 1
	}
	if keys[sdl.K_o] != 0 {
		view_z -= 1
	}
	if keys[sdl.K_ESCAPE] != 0 {
		return true
	}
	if keys[sdl.K_UP] != 0 {
		view_rotx += 1
	}
	if keys[sdl.K_DOWN] != 0 {
		view_rotx -= 1.0
	}
	if keys[sdl.K_LEFT] != 0 {
		view_roty += 1.0
	}
	if keys[sdl.K_RIGHT] != 0 {
		view_roty -= 1.0
	}
	if keys[sdl.K_z] != 0 {
		if (sdl.GetModState() & sdl.KMOD_RSHIFT) != 0 {
			view_rotz -= 1.0
		} else {
			view_rotz += 1.0
		}
	}
	if keys[sdl.K_w] != 0 {
		gl.Translatef(0.0, 0.0, 1)
	}
	if keys[sdl.K_s] != 0 {
		gl.Translatef(0.0, 0.0, -1)
	}
	if keys[sdl.K_a] != 0 {
		gl.Translatef(-1, 0, 0)
	}
	if keys[sdl.K_d] != 0 {
		gl.Translatef(1, 0, 0)
	}
	if keys[sdl.K_q] != 0 {
		gl.Translatef(0, 1, 0)
	}
	if keys[sdl.K_e] != 0 {
		gl.Translatef(0, -1, 0)
	}
	return false
}
// handle key press events
func handleKeyPress(keysym sdl.Keysym) {
	keys := sdl.GetKeyState()

	if keys[sdl.K_ESCAPE] == 1 {
		Quit(0)
	}

	if keys[sdl.K_F1] == 1 {
		sdl.WM_ToggleFullScreen(surface)
	}

	if keys[sdl.K_f] == 1 {
		filter = (filter + 1) % 3
	}

	if keys[sdl.K_RIGHT] == 1 {
		yrot -= 1.5
	}

	if keys[sdl.K_LEFT] == 1 {
		yrot += 1.5
	}

	if keys[sdl.K_UP] == 1 {
		xpos -= math.Sin(yrot*PiOver100) * 0.05
		zpos -= math.Cos(yrot*PiOver100) * 0.05
		if walkbiasangle >= 359.0 {
			walkbiasangle = 0.0
		} else {
			walkbiasangle += 10.0
		}
		walkbias = math.Sin(walkbiasangle*PiOver100) / 20.0
	}

	if keys[sdl.K_DOWN] == 1 {
		xpos += math.Sin(yrot*PiOver100) * 0.05
		zpos += math.Cos(yrot*PiOver100) * 0.05
		if walkbiasangle <= 1.0 {
			walkbiasangle = 359.0
		} else {
			walkbiasangle -= 10.0
		}
		walkbias = math.Sin(walkbiasangle*PiOver100) / 20.0
	}
}
Beispiel #3
0
func main() {

	flag.Parse()

	var done bool
	var keys []uint8

	sdl.Init(sdl.INIT_VIDEO)

	var screen = sdl.SetVideoMode(300, 300, 16, sdl.OPENGL|sdl.RESIZABLE)

	if screen == nil {
		sdl.Quit()
		panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n")
	}

	if gl.Init() != 0 {
		panic("gl error")
	}

	sdl.WM_SetCaption("Gears", "gears")

	init_()
	reshape(int(screen.W), int(screen.H))
	done = false
	for !done {

		idle()
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.ResizeEvent:
				re := e.(*sdl.ResizeEvent)
				screen = sdl.SetVideoMode(int(re.W), int(re.H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					reshape(int(screen.W), int(screen.H))
				} else {
					panic("we couldn't set the new video mode??")
				}
				break

			case *sdl.QuitEvent:
				done = true
				break
			}
		}
		keys = sdl.GetKeyState()

		if keys[sdl.K_ESCAPE] != 0 {
			done = true
		}
		if keys[sdl.K_UP] != 0 {
			view_rotx += 5.0
		}
		if keys[sdl.K_DOWN] != 0 {
			view_rotx -= 5.0
		}
		if keys[sdl.K_LEFT] != 0 {
			view_roty += 5.0
		}
		if keys[sdl.K_RIGHT] != 0 {
			view_roty -= 5.0
		}
		if keys[sdl.K_z] != 0 {
			if (sdl.GetModState() & sdl.KMOD_RSHIFT) != 0 {
				view_rotz -= 5.0
			} else {
				view_rotz += 5.0
			}
		}

		draw()
	}
	sdl.Quit()
	return

}
Beispiel #4
0
// Checks if ESC is pressed
func escPressed() bool {
	keyMap := sdl.GetKeyState()
	return 1 == keyMap[sdl.K_ESCAPE]
}
Beispiel #5
0
func (self *Inventory) HandleMouseButton(re *sdl.MouseButtonEvent) {
	if re.Button == 1 && re.State == 1 { // LEFT, DOWN
		x, y := viewport.ScreenCoordsToWorld2D(re.X, re.Y)

		for i := range self.inventoryRects {
			if self.inventoryRects[i].Contains(x, y) {
				if self.inventorySlots[i] != 0 {
					keys := sdl.GetKeyState()
					if keys[sdl.K_LCTRL] != 0 || keys[sdl.K_RCTRL] != 0 {
						ThePlayer.EquipItem(self.inventorySlots[i])

					} else {
						// Add to component slots
						for j := range self.componentSlots {
							if self.componentSlots[j] == self.inventorySlots[i] {
								return // Already in component slot
							}
						}
						for j := range self.componentSlots {
							if self.componentSlots[j] == 0 {
								self.componentSlots[j] = self.inventorySlots[i]
								self.UpdateProducts()
								return
							}
						}
					}

				}
				return
			}
		}
		for i := range self.componentRects {
			if self.componentRects[i].Contains(x, y) {
				if self.componentSlots[i] != 0 {
					self.componentSlots[i] = 0
					self.UpdateProducts()
				}
				return
			}
		}
		for i := range self.productRects {
			if self.productRects[i].Contains(x, y) {
				if self.productSlots[i] != nil {
					recipe := self.productSlots[i]
					if self.HasRecipeComponents(recipe) {
						for _, rc := range recipe.components {
							ThePlayer.inventory[rc.item] -= rc.quantity
						}
						ThePlayer.inventory[recipe.product.item] += recipe.product.quantity
						self.UpdateProducts()

						for j := range self.componentSlots {
							if ThePlayer.inventory[self.componentSlots[j]] <= 0 {
								self.componentSlots[j] = 0
							}
						}

					}
				}
				return
			}
		}

		hit, pos := picker.HitTest(x, y)
		if hit && pos > 2 {
			keys := sdl.GetKeyState()
			if keys[sdl.K_LCTRL] != 0 || keys[sdl.K_RCTRL] != 0 {
				// Remove from picker
				ThePlayer.equippedItems[uint16(pos)-3] = ITEM_NONE
			}
		}

	}

	// type MouseButtonEvent struct {
	// 	Type   uint8
	// 	Which  uint8
	// 	Button uint8
	// 	State  uint8
	// 	X      uint16
	// 	Y      uint16
	// }

}
Beispiel #6
0
func HandleUserInput() bool {
	for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
		switch e.(type) {
		case *sdl.QuitEvent:
			return true
		case *sdl.ResizeEvent:
			re := e.(*sdl.ResizeEvent)
			screen := sdl.SetVideoMode(int(re.W), int(re.H), 16,
				sdl.OPENGL|sdl.RESIZABLE)
			if screen != nil {
				viewport.Reshape(int(screen.W), int(screen.H))
			} else {
				panic("Could not set video mode")
			}
			break

		case *sdl.MouseButtonEvent:
			re := e.(*sdl.MouseButtonEvent)
			HandleMouseButton(re)

		case *sdl.KeyboardEvent:
			re := e.(*sdl.KeyboardEvent)

			if re.Keysym.Sym == sdl.K_ESCAPE && re.Type == sdl.KEYDOWN {
				if inventory.visible {
					inventory.Hide()
				} else {
					if pause.visible {
						pause.visible = false
					} else {
						pause.visible = true
					}
				}
			}

			if re.Keysym.Sym == sdl.K_F3 && re.Type == sdl.KEYDOWN {
				console.visible = !console.visible
			}

			if !pause.visible {
				if re.Keysym.Sym == sdl.K_i && re.Type == sdl.KEYDOWN {
					if inventory.visible {
						inventory.Hide()
					} else {
						inventory.Show(nil, nil)
					}
				}

				if inventory.visible {
					inventory.HandleKeyboard(re)
				}
			}
		}
	}

	keys := sdl.GetKeyState()

	if console.visible {
		console.HandleKeys(keys)
	}

	if pause.visible {
		pause.HandleKeys(keys)
	} else if inventory.visible {
		inventory.HandleKeys(keys)
	} else {
		viewport.HandleKeys(keys)
		ThePlayer.HandleKeys(keys)
		picker.HandleKeys(keys)
	}

	return false
}
Beispiel #7
0
func gameLoop() {
	var startTime int64 = time.Now().UnixNano()
	var currentTime, accumulator int64 = 0, 0
	var t, dt int64 = 0, 1e9 / 40
	var drawFrame, computeFrame int64 = 0, 0

	update500ms := new(Timer)
	update500ms.interval = 500 * 1e6
	update500ms.Start()

	update150ms := new(Timer)
	update150ms.interval = 50 * 1e6
	update150ms.Start()

	update2000ms := new(Timer)
	update2000ms.interval = 2000 * 1e6
	update2000ms.Start()

	var interactingBlock *InteractingBlockFace

	done := false
	for !done {

		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.QuitEvent:
				done = true
			case *sdl.ResizeEvent:
				re := e.(*sdl.ResizeEvent)
				screen := sdl.SetVideoMode(int(re.W), int(re.H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					viewport.Reshape(int(screen.W), int(screen.H))
				} else {
					panic("Could not set video mode")
				}
				break

			case *sdl.MouseButtonEvent:
				re := e.(*sdl.MouseButtonEvent)
				if inventory.visible {
					inventory.HandleMouseButton(re)
				} else {
					picker.HandleMouseButton(re)

					if re.Button == 1 && re.State == 1 { // LEFT, DOWN
						if ThePlayer.CanInteract() {
							selectedBlockFace := viewport.SelectedBlockFace()
							if selectedBlockFace != nil {
								if interactingBlock == nil || interactingBlock.blockFace.pos != selectedBlockFace.pos {
									interactingBlock = new(InteractingBlockFace)
									interactingBlock.blockFace = selectedBlockFace
									interactingBlock.hitCount = 0
								}
								ThePlayer.Interact(interactingBlock)
							}
							// println("Click:", re.X, re.Y, re.State, re.Button, re.Which)
						}
					}
				}

			case *sdl.KeyboardEvent:
				re := e.(*sdl.KeyboardEvent)

				if re.Keysym.Sym == sdl.K_ESCAPE {
					if re.Type == sdl.KEYDOWN {
						inventory.visible = false
						if pause.visible {
							pause.visible = false
							update2000ms.Unpause()
							update500ms.Unpause()
							update150ms.Unpause()
						} else {
							pause.visible = true
							update2000ms.Pause()
							update500ms.Pause()
							update150ms.Pause()
						}
					}
				}

				if re.Keysym.Sym == sdl.K_F3 {
					if re.Type == sdl.KEYDOWN {
						if console.visible == true {
							console.visible = false
						} else {
							console.visible = true
						}
					}
				}

				if !pause.visible {
					if re.Keysym.Sym == sdl.K_i {
						if re.Type == sdl.KEYDOWN {
							if inventory.visible == true {
								inventory.visible = false
							} else {
								inventory.visible = true
							}
						}
					}

					if inventory.visible {
						inventory.HandleKeyboard(re)
					}
				}
			}
		}

		keys := sdl.GetKeyState()

		if console.visible {
			console.HandleKeys(keys)
		}

		if pause.visible {
			pause.HandleKeys(keys)
		} else if inventory.visible {
			inventory.HandleKeys(keys)
		} else {
			viewport.HandleKeys(keys)
			ThePlayer.HandleKeys(keys)
			picker.HandleKeys(keys)
		}

		if update150ms.PassedInterval() {

			if !inventory.visible {
				// If player is breaking a block then allow them to hold mouse down to continue action
				if interactingBlock != nil && ThePlayer.currentAction == ACTION_BREAK {
					mouseState := sdl.GetMouseState(nil, nil)
					if mouseState == 1 {
						if ThePlayer.CanInteract() {
							selectedBlockFace := viewport.SelectedBlockFace()
							if selectedBlockFace != nil {
								if interactingBlock == nil || !interactingBlock.blockFace.pos.Equals(&selectedBlockFace.pos) {
									interactingBlock = new(InteractingBlockFace)
									interactingBlock.blockFace = selectedBlockFace
									interactingBlock.hitCount = 0
								}
								ThePlayer.Interact(interactingBlock)
							}
							// println("Click:", re.X, re.Y, re.State, re.Button, re.Which)
						}
					}
				}
			}

			PreloadChunks(50)
			update150ms.Start()
		}

		if update500ms.PassedInterval() {
			console.fps = float64(drawFrame) / (float64(update500ms.GetTicks()) / float64(1e9))
			console.Update()

			UpdateTimeOfDay()
			UpdateCampfires()
			PreloadChunks(220)

			drawFrame, computeFrame = 0, 0
			update500ms.Start()

		}

		if update2000ms.PassedInterval() {
			CullChunks()
			UpdatePlayerStats()
			update2000ms.Start()
		}

		if !pause.visible {
			newTime := time.Now().UnixNano()
			deltaTime := newTime - currentTime
			currentTime = newTime
			if deltaTime > 1e9/4 {
				deltaTime = 1e9 / 4
			}

			accumulator += deltaTime

			for accumulator > dt {
				accumulator -= dt

				TheWorld.ApplyForces(ThePlayer, float64(dt)/1e9)

				ThePlayer.Update(float64(dt) / 1e9)
				TheWorld.Simulate(float64(dt) / 1e9)

				computeFrame++
				t += dt
			}
		}
		//interpolate(previous, current, accumulator/dt)

		Draw(currentTime - startTime)
		drawFrame++
	}

}
Beispiel #8
0
func main() {
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	v_info := sdl.GetVideoInfo()

	var screen = sdl.SetVideoMode(
		int(v_info.Current_w),
		int(v_info.Current_h),
		32,
		sdl.HWSURFACE|sdl.DOUBLEBUF)

	rows = v_info.Current_w / SIZE
	columns = v_info.Current_h / SIZE

	if v_info.Current_w%SIZE != 0 {
		rows += 1
	}

	if v_info.Current_h%SIZE != 0 {
		columns += 1
	}

	// Initialize our world
	world = make([][]Field, rows)
	for i := range world {
		world[i] = make([]Field, columns)
		for j := range world[i] {
			world[i][j].X = i
			world[i][j].Y = j
			world[i][j].T = OPEN
			world[i][j].lsize = 0
			world[i][j].rsize = 0
			world[i][j].o = false
			world[i][j].c = false
		}
	}

	// Once we're done, free screen object and quit sdl.
	defer sdl.Quit()
	defer screen.Free()

	if screen == nil {
		panic(sdl.GetError())
	}

	// Set window title
	sdl.WM_SetCaption("A* algorithm demo", "")

	// Give the screen an initially and update display
	screen.FillRect(nil, OPEN)
	screen.Flip()

	/* Draw the grid on our display */
	drawGrid(screen)

	/* Create the different channels we need */
	paint_chan = make(chan *Paint, 2000)
	read_field = make(chan *Field)
	field_chan = make(chan *Field)

	// Fillbox runs asynchronously, start it here
	go initFillBox(screen)

	// Getneighbours process runs all the time, starts here.
	go GetNeighbours(world, read_field)

	for true {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {

			switch e := ev.(type) {
			case *sdl.QuitEvent:
				return
			case *sdl.KeyboardEvent:
				if e.Keysym.Sym == sdl.K_ESCAPE {
					/* Quit when escape is pressed */
					return
				} else if e.Keysym.Sym == sdl.K_r &&
					(e.Keysym.Mod&sdl.KMOD_LCTRL) != 0 &&
					(e.Keysym.Mod&sdl.KMOD_LSHIFT) != 0 {
					resetComplete()
				} else if e.Keysym.Sym == sdl.K_r && (e.Keysym.Mod&sdl.KMOD_LCTRL) != 0 {
					resetAllPaths()
				} else if e.Keysym.Sym == sdl.K_r {
					resetPaths()
				} else if e.Keysym.Sym == sdl.K_RETURN {
					/* If RETURN is pressed, run pathfinding */
					if start != nil && goal != nil {
						//resetPaths();
						go aStar(world, screen)
					}
				}
			case *sdl.MouseMotionEvent:
				if e.State == sdl.BUTTON_LEFT || e.State == sdl.BUTTON_WHEELUP {
					drawMouseMotion(world, screen, e)
				}
			case *sdl.MouseButtonEvent:
				if e.Type == sdl.MOUSEBUTTONDOWN {
					state := sdl.GetKeyState()
					if e.Button == sdl.BUTTON_LEFT {
						r := getRect(e)

						if state[sdl.K_s] == 1 {
							// Left mouse button with s, set new start point
							if start == nil {
								start = &world[int(r.X)/SIZE][int(r.Y)/SIZE]

								fillBox(start, START)
							} else {
								fillBox(start, OPEN)

								start = &world[int(r.X)/SIZE][int(r.Y)/SIZE]
								fillBox(start, START)
							}
						} else if state[sdl.K_g] == 1 {
							// Left mouse button with g, set new goal point
							if goal == nil {
								goal = &world[int(r.X)/SIZE][int(r.Y)/SIZE]

								fillBox(goal, GOAL)
							} else {
								fillBox(goal, OPEN)

								goal = &world[int(r.X)/SIZE][int(r.Y)/SIZE]
								fillBox(goal, GOAL)
							}
						} else {
							// No relevant modifiers were pressed, color the field.
							var f *Field = &world[(r.X / SIZE)][(r.Y / SIZE)]
							//fmt.Println("Click on", f);

							if f.T == OPEN {
								fillBox(f, WALL)
							} else {
								fillBox(f, OPEN)
							}
						}
					}
				}
			default:
			}
		}

		// Delay for 15 milliseconds
		sdl.Delay(30)
		screen.Flip()
	}

	fmt.Println("Exiting")
}
Beispiel #9
0
func (self *Inventory) HandleMouseButton(re *sdl.MouseButtonEvent) {
	if re.Button == 1 && re.State == 1 { // LEFT, DOWN
		x, y := viewport.ScreenCoordsToWorld2D(re.X, re.Y)

		// Nothing selected, left click == pick one up
		// Nothing selected, shift left click == pick all up

		// Item selected, same source, same item in slot, left click == pick another up
		// Item selected, same source, same item in slot, shift left click == pick all up
		// Item selected, same source, same item in slot, ctrl left click == drop one
		// Item selected, same source, same item in slot, ctrl shift left click == drop all

		// Item selected, same source, empty slot, left click == drop one
		// Item selected, same source, empty slot, shift left click == drop all
		// Item selected, same source, empty slot, ctrl left click == N/A
		// Item selected, same source, empty slot, ctrl shift left click == N/A

		// Item selected, diff source, same item in slot, left click == drop one
		// Item selected, diff source, same item in slot, shift left click == drop all
		// Item selected, diff source, same item in slot, ctrl left click == pick one up
		// Item selected, diff source, same item in slot, ctrl shift left click == pick all up

		// Item selected, diff source, empty slot, left click == drop one
		// Item selected, diff source, empty slot, shift left click == drop all
		// Item selected, diff source, empty slot, ctrl left click == pick one up
		// Item selected, diff source, empty slot, ctrl shift left click == pick all up

		keys := sdl.GetKeyState()

		bulk := false
		invert := false
		if keys[sdl.K_LSHIFT] != 0 || keys[sdl.K_RSHIFT] != 0 {
			bulk = true
		}
		if keys[sdl.K_LCTRL] != 0 || keys[sdl.K_RCTRL] != 0 {
			invert = true
		}

		for i := range self.inventoryRects {
			if self.inventoryRects[i].Contains(x, y) {
				slot := &self.inventorySlots[i]

				if self.selectedItem == nil {
					// pick up item, if any
					if slot.item != 0 && slot.quantity > 0 {
						if bulk {
							// Act on all items in slot
							self.selectedItem = &SelectedItem{ItemQuantity{slot.item, slot.quantity}, ItemSlot{AREA_INVENTORY, i}}
							slot.quantity = 0
							slot.item = 0
						} else {
							// Act on a single item
							self.selectedItem = &SelectedItem{ItemQuantity{slot.item, 1}, ItemSlot{AREA_INVENTORY, i}}
							slot.quantity -= 1
							if slot.quantity == 0 {
								slot.item = 0
							}
						}
					}
				} else if slot.item == self.selectedItem.item {
					// Clicked on slot containing same item as current selection
					// Is this the same slot as the source of items
					if self.selectedItem.area == AREA_INVENTORY && self.selectedItem.index == i {
						// Same slot
						if invert {
							// drop back into same slot
							if bulk {
								// Act on all items in slot
								slot.quantity += self.selectedItem.quantity
								self.selectedItem.quantity = 0
							} else {
								// Act on a single item
								self.selectedItem.quantity -= 1
								slot.quantity += 1
							}

						} else {
							// pick more up
							if bulk {
								// Act on all items in slot
								self.selectedItem.quantity += slot.quantity
								slot.quantity = 0
							} else {
								// Act on a single item
								self.selectedItem.quantity += 1
								slot.quantity -= 1
							}
						}
					} else {
						// Different slot
						if invert {
							if bulk {
								self.selectedItem.quantity += slot.quantity
								slot.quantity = 0
							} else {
								self.selectedItem.quantity += 1
								slot.quantity -= 1
							}
						} else {
							if bulk {
								slot.quantity += self.selectedItem.quantity
								self.selectedItem.quantity = 0
							} else {
								self.selectedItem.quantity -= 1
								slot.quantity += 1
							}
						}
					}
				} else if slot.item == 0 {
					quantity := uint16(1)
					if bulk {
						quantity = self.selectedItem.quantity
					}

					self.selectedItem.quantity -= quantity
					slot.quantity += quantity
					slot.item = self.selectedItem.item
				}

				if self.selectedItem != nil && self.selectedItem.quantity == 0 {
					self.selectedItem = nil
				}

				if slot.quantity == 0 {
					slot.item = 0
				}

				return
			}
		}
		for i := range self.componentRects {
			if self.componentRects[i].Contains(x, y) {
				slot := &self.componentSlots[i]
				slotQuantity := slot.quantity
				if self.selectedItem == nil {
					// pick up item, if any
					if slot.item != 0 && slot.quantity > 0 {
						if bulk {
							// Act on all items in slot
							self.selectedItem = &SelectedItem{ItemQuantity{slot.item, slot.quantity}, ItemSlot{AREA_HANDHELD_COMPONENT, i}}
							slot.quantity = 0
							slot.item = 0
						} else {
							// Act on a single item
							self.selectedItem = &SelectedItem{ItemQuantity{slot.item, 1}, ItemSlot{AREA_HANDHELD_COMPONENT, i}}
							slot.quantity -= 1
							if slot.quantity == 0 {
								slot.item = 0
							}
						}
					}
				} else if slot.item == self.selectedItem.item {
					// Clicked on slot containing same item as current selection
					// Is this the same slot as the source of items
					if self.selectedItem.area == AREA_HANDHELD_COMPONENT && self.selectedItem.index == i {
						// Same slot
						if invert {
							// drop back into same slot
							if bulk {
								// Act on all items in slot
								slot.quantity += self.selectedItem.quantity
								self.selectedItem.quantity = 0
							} else {
								// Act on a single item
								self.selectedItem.quantity -= 1
								slot.quantity += 1
							}

						} else {
							// pick more up
							if bulk {
								// Act on all items in slot
								self.selectedItem.quantity += slot.quantity
								slot.quantity = 0
							} else {
								// Act on a single item
								self.selectedItem.quantity += 1
								slot.quantity -= 1
							}
						}
					} else if self.selectedItem.area != AREA_HANDHELD_COMPONENT {
						// Different slot
						if invert {
							if bulk {
								self.selectedItem.quantity += slot.quantity
								slot.quantity = 0
							} else {
								self.selectedItem.quantity += 1
								slot.quantity -= 1
							}
						} else {
							if bulk {
								slot.quantity += self.selectedItem.quantity
								self.selectedItem.quantity = 0
							} else {
								self.selectedItem.quantity -= 1
								slot.quantity += 1
							}
						}

					}
				} else if slot.item == 0 {
					// Check for duplicates
					for j := range self.componentSlots {
						if j != i && self.componentSlots[j].item == self.selectedItem.item {
							return
						}
					}

					quantity := uint16(1)
					if bulk {
						quantity = self.selectedItem.quantity
					}

					self.selectedItem.quantity -= quantity
					slot.quantity += quantity
					slot.item = self.selectedItem.item
				}

				if self.selectedItem != nil && self.selectedItem.quantity == 0 {
					self.selectedItem = nil
				}

				if slot.quantity == 0 {
					slot.item = 0
				}

				if slotQuantity != slot.quantity {
					self.UpdateProducts()
				}

				return

			}
		}
		for i := range self.productRects {
			if self.productRects[i].Contains(x, y) {
				if self.productSlots[i] != nil {
					recipe := self.productSlots[i]

					itemid := recipe.product.item
					if self.selectedItem == nil {
						if self.HasRecipeComponents(recipe) {
							for _, rc := range recipe.components {
								for k := range self.componentSlots {
									if self.componentSlots[k].item == rc.item {
										self.componentSlots[k].quantity -= rc.quantity
									}
								}
							}
							self.selectedItem = &SelectedItem{ItemQuantity{recipe.product.item, recipe.product.quantity}, ItemSlot{AREA_HANDHELD_PRODUCT, i}}
							self.UpdateProducts()

						}
					} else if itemid == self.selectedItem.item {
						if self.HasRecipeComponents(recipe) {
							for _, rc := range recipe.components {
								for k := range self.componentSlots {
									if self.componentSlots[k].item == rc.item {
										self.componentSlots[k].quantity -= rc.quantity
									}
									if self.componentSlots[k].quantity == 0 {
										self.componentSlots[k].item = 0
									}
								}
							}
							self.selectedItem.quantity += recipe.product.quantity
							if self.selectedItem.quantity > MAX_ITEMS_IN_INVENTORY {
								self.selectedItem.quantity = MAX_ITEMS_IN_INVENTORY
							}
							self.UpdateProducts()
						}
					}

					return
				}
			}
		}

		if self.currentContainer != nil {

			for i := range self.containerRects {
				if self.containerRects[i].Contains(x, y) {

					if self.selectedItem == nil {
						if self.currentContainer.CanTake() {
							// pick up item, if any
							if self.currentContainer.Item(i).item != 0 && self.currentContainer.Item(i).quantity > 0 {
								if bulk {
									// Act on all items in slot
									self.selectedItem = &SelectedItem{ItemQuantity{self.currentContainer.Item(i).item, self.currentContainer.Item(i).quantity}, ItemSlot{AREA_CONTAINER, i}}
									self.currentContainer.Take(i, self.currentContainer.Item(i).quantity)
								} else {
									// Act on a single item
									self.selectedItem = &SelectedItem{ItemQuantity{self.currentContainer.Item(i).item, 1}, ItemSlot{AREA_CONTAINER, i}}
									self.currentContainer.Take(i, 1)
								}
							}
						}
					} else if self.currentContainer.Item(i).item == self.selectedItem.item {
						// Clicked on slot containing same item as current selection
						// Is this the same slot as the source of items
						if self.selectedItem.area == AREA_CONTAINER && self.selectedItem.index == i {
							if self.currentContainer.CanTake() {
								// Same slot
								if invert {
									// drop back into same slot
									if bulk {
										// Act on all items in slot
										self.currentContainer.Place(i, &ItemQuantity{self.selectedItem.item, self.selectedItem.quantity})
										self.selectedItem.quantity = 0
									} else {
										// Act on a single item
										self.selectedItem.quantity -= 1
										self.currentContainer.Place(i, &ItemQuantity{self.selectedItem.item, 1})
									}

								} else {
									// pick more up
									if bulk {
										// Act on all items in slot
										self.selectedItem.quantity += self.currentContainer.Item(i).quantity
										self.currentContainer.Take(i, self.currentContainer.Item(i).quantity)
									} else {
										// Act on a single item
										self.selectedItem.quantity += 1
										self.currentContainer.Take(i, 1)
									}
								}
							}
						} else {
							// Different slot
							if invert {
								if self.currentContainer.CanTake() {
									if bulk {
										// Act on all items in slot
										self.selectedItem.quantity += self.currentContainer.Item(i).quantity
										self.currentContainer.Take(i, self.currentContainer.Item(i).quantity)
									} else {
										// Act on a single item
										self.selectedItem.quantity += 1
										self.currentContainer.Take(i, 1)
									}
								}
							} else {
								if self.currentContainer.CanPlace(self.selectedItem.item) {
									if bulk {
										// Act on all items in slot
										self.currentContainer.Place(i, &ItemQuantity{self.selectedItem.item, self.selectedItem.quantity})
										self.selectedItem.quantity = 0
									} else {
										// Act on a single item
										self.selectedItem.quantity -= 1
										self.currentContainer.Place(i, &ItemQuantity{self.selectedItem.item, 1})
									}
								}
							}
						}
					} else if self.currentContainer.Item(i).item == 0 {
						if self.currentContainer.CanPlace(self.selectedItem.item) {
							quantity := uint16(1)
							if bulk {
								quantity = self.selectedItem.quantity
							}

							self.selectedItem.quantity -= quantity
							self.currentContainer.Place(i, &ItemQuantity{self.selectedItem.item, quantity})
						}
					}

					if self.selectedItem != nil && self.selectedItem.quantity == 0 {
						self.selectedItem = nil
					}

					return
				}
			}
		}

		// Add/remove items from picker
		hit, pos := picker.HitTest(x, y)
		if hit && pos > 2 {
			if self.selectedItem == nil {
				// Remove from picker
				ThePlayer.equippedItems[uint16(pos)-3] = ITEM_NONE
			} else {
				// Set the picker
				ThePlayer.equippedItems[uint16(pos)-3] = self.selectedItem.item
				if self.selectedItem.area == AREA_INVENTORY {
					self.inventorySlots[self.selectedItem.index].quantity += self.selectedItem.quantity
				} else {
					println("Looking for somewhere in inventory to place some ", self.selectedItem.item)
					for i := range self.inventorySlots {
						if self.inventorySlots[i].item == self.selectedItem.item {
							println("Found some ", self.selectedItem.item, " in inventory slot ", i)
							if self.inventorySlots[i].quantity+self.selectedItem.quantity < MAX_ITEMS_IN_INVENTORY {
								self.inventorySlots[i].quantity += self.selectedItem.quantity
								break
							} else {
								self.selectedItem.quantity -= MAX_ITEMS_IN_INVENTORY - self.inventorySlots[i].quantity
								self.inventorySlots[i].quantity = MAX_ITEMS_IN_INVENTORY
							}
						} else if self.inventorySlots[i].item == 0 {
							self.inventorySlots[i].item = self.selectedItem.item
							self.inventorySlots[i].quantity = self.selectedItem.quantity
							break
						}
					}
				}
				self.selectedItem = nil

			}

		}

	}

	// type MouseButtonEvent struct {
	// 	Type   uint8
	// 	Which  uint8
	// 	Button uint8
	// 	State  uint8
	// 	X      uint16
	// 	Y      uint16
	// }

}
Beispiel #10
0
func HandleEvents() {
	for Running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				Running = false
			case *sdl.MouseMotionEvent:
				MouseMotion(e)
			case *sdl.MouseButtonEvent:
				switch e.Type {
				case sdl.MOUSEBUTTONDOWN:
					MouseDown(e)
				case sdl.MOUSEBUTTONUP:
					MouseUp(e)
				}
			}
		}

		keys := sdl.GetKeyState()
		for n, i := range keys {
			if i == 1 {
				if Paused {
					switch n {
					case sdl.K_RETURN, sdl.K_SPACE:
						Paused = false
					case sdl.K_q:
						Running = false
					}
				} else if GameOver || GameWin {
					switch n {
					case sdl.K_RETURN, sdl.K_SPACE:
						Player.Reset()
					case sdl.K_q:
						Running = false
					}
				} else {
					switch n {
					case sdl.K_LSHIFT, sdl.K_RSHIFT, sdl.K_LCTRL, sdl.K_RCTRL:
						Player.FireWeapon()
					case sdl.K_0:
						Player.weapon = Player.weapons['0']
					case sdl.K_1:
						Player.weapon = Player.weapons['1']
					case sdl.K_2:
						Player.weapon = Player.weapons['2']
					case sdl.K_3:
						Player.weapon = Player.weapons['3']
					case sdl.K_5:
						Player.weapon = Player.weapons['5']
					case sdl.K_9:
						Player.weapon = Player.weapons['9']
					case sdl.K_MINUS:
						Player.weapon = Player.weapons['-']
					case sdl.K_a, sdl.K_LEFT:
						Player.GoLeft()
					case sdl.K_d, sdl.K_RIGHT:
						Player.GoRight()
					case sdl.K_s, sdl.K_DOWN:
						Player.GoDown()
					case sdl.K_w, sdl.K_UP:
						Player.GoUp()
					case sdl.K_ESCAPE:
						Running = false
					case sdl.K_p:
						Paused = true
					}
				}
			}
		}

		sdl.Delay(25)
	}
}