Exemplo n.º 1
0
func NewGirdShape(size, count sf.Vector2f, color sf.Color) (shape *GridShape, err error) {
	varray, err := sf.NewVertexArray()
	shape = &GridShape{VertexArray: *varray, size: size, count: count, renderStates: sf.DefaultRenderStates()}
	shape.PrimitiveType = sf.PrimitiveQuads
	shape.cellSize = sf.Vector2f{size.X / count.X, size.Y / count.Y}
	shape.Vertices = make([]sf.Vertex, int(count.X)*int(count.Y)*4)

	shape.pointsOld = make([][]sf.Vector2f, int(count.Y))
	for i := 0; i < int(count.Y); i++ {
		shape.pointsOld[i] = make([]sf.Vector2f, int(count.X))
	}

	shape.ForEach(1, 1, func(x, y int) {
		vertex := sf.Vertex{}
		vertex.Position = sf.Vector2f{X: float32(x) * shape.cellSize.X, Y: float32(y) * shape.cellSize.Y}
		vertex.TexCoords = sf.Vector2f{X: float32(x) * shape.cellSize.X, Y: float32(y) * shape.cellSize.Y}
		vertex.Color = color
		shape.SetVertexs(x, y, vertex)
		if x < int(shape.count.X) && y < int(shape.count.Y) {
			shape.pointsOld[y][x] = vertex.Position
		}
	})
	shape.center = sf.Vector2f{size.X / 2, size.Y / 2}
	return
}
Exemplo n.º 2
0
func (this *Game) Draw() {
	this.RenderWindow.Clear(sf.Color{44, 176, 55, 255})
	if this.State == 0 {
		this.WelcomeText.Draw(this.RenderWindow, sf.DefaultRenderStates())
		this.PlayText.Draw(this.RenderWindow, sf.DefaultRenderStates())
	} else if this.State == 1 {
		for Enemy, _ := range this.EnemyList {
			this.EnemyList[Enemy].Draw(this.RenderWindow, sf.DefaultRenderStates())
		}
		this.Player.Draw(this.RenderWindow, sf.DefaultRenderStates())
		this.ChildsCollectedText.Draw(this.RenderWindow, sf.DefaultRenderStates())
		this.TimeText.Draw(this.RenderWindow, sf.DefaultRenderStates())
	} else if this.State == 2 || this.State == 3 {
		this.EndingText.Draw(this.RenderWindow, sf.DefaultRenderStates())
		this.EndingPointsText.Draw(this.RenderWindow, sf.DefaultRenderStates())
		this.RestartText.Draw(this.RenderWindow, sf.DefaultRenderStates())
		this.CreditsText.Draw(this.RenderWindow, sf.DefaultRenderStates())
	}
	this.RenderWindow.Display()
}
Exemplo n.º 3
0
func main() {
	ticker := time.NewTicker(time.Second / 30)
	setting := sf.DefaultContextSettings()
	setting.AntialiasingLevel = 8
	renderWindow := sf.NewRenderWindow(sf.VideoMode{800, 600, 32}, "Events (GoSFML2)", sf.StyleDefault, setting)

	grid, err := shapes.NewGirdShape(sf.Vector2f{500, 400}, sf.Vector2f{50, 40}, sf.ColorWhite())

	imageGot, _, err := images.ReadImage("./011.jpg", 0, 0, 500, 400)
	texture, err := images.ReadTextureFromImage(*imageGot)
	if err != nil {
		fmt.Println(err)
		return
	}
	texture.SetSmooth(true)
	grid.SetTexture(texture)
	grid.Move(sf.Vector2f{100, 100})
	grid.Rotate(15)
	var pointX, pointY int
	var gotPoint bool

	for renderWindow.IsOpen() {
		select {
		case <-ticker.C:
			mousePosi := sf.MouseGetPosition(renderWindow)
			mousePosf := sf.Vector2f{float32(mousePosi.X), float32(mousePosi.Y)}
			for event := renderWindow.PollEvent(); event != nil; event = renderWindow.PollEvent() {
				switch event.(type) {
				case sf.EventClosed:
					renderWindow.Close()
				case sf.EventMouseButtonPressed:
					pointX, pointY, gotPoint = grid.GetNearestPointIndex(mousePosf)
				}
			}
			if sf.IsMouseButtonPressed(0) {
				if gotPoint {
					grid.SmoothPointsTo(pointX, pointY, mousePosf, 10, func(data float32) float32 {
						return (mathUtil.Sin(data, -0.5, 1)/2 + 0.5) / 5
					})
				}
			}
		}
		renderWindow.Clear(sf.ColorWhite())
		renderWindow.Draw(grid, sf.DefaultRenderStates())
		renderWindow.Display()
	}
}
Exemplo n.º 4
0
//listUsables lists all the items that have an effect, and prompts the user to use one.
func (g *Game) listUsables() {
	letter := 'a'
	listText, _ := sf.NewText(Font)
	listText.SetCharacterSize(12)
	listText.SetPosition(sf.Vector2f{12, 12})
	usables := make(map[rune]*Item)
	names := make(map[*Item]string)
	for k, i := range g.player.inventory {
		if i.effect != nil {
			appendString(listText, strconv.QuoteRune(letter)+" - "+k+" x"+strconv.Itoa(i.stack))
			usables[letter] = i
			names[i] = k
			letter++
		}
	}

listLoop:
	for g.window.IsOpen() {
		for event := g.window.PollEvent(); event != nil; event = g.window.PollEvent() {
			switch et := event.(type) {
			case sf.EventTextEntered:
				done, used := g.inventoryInput(et.Char, usables, names)
				if used != "" {
					usedI := g.player.inventory[used]
					if usedI.stack > 1 {
						usedI.stack--
						break listLoop
					}
					delete(g.player.inventory, used)
					break listLoop
				}
				if done {
					break listLoop
				}
			}
		}
		g.window.Clear(sf.ColorBlack())

		g.window.SetView(g.logView)
		g.drawLog()
		g.window.SetView(g.gameView)
		listText.Draw(g.window, sf.DefaultRenderStates())
		g.window.Display()
	}

	g.state = PLAY
}
Exemplo n.º 5
0
func (g *Game) drawLog() {
	logFile, err := ioutil.ReadFile("log.txt")
	if err != nil {
		fmt.Println("Can't open the log file log.txt: ERR: ", err)
	}

	if g.state != LOG {
		g.logView.SetSize(sf.Vector2f{g.resW * 0.8, g.resH * 0.25})
		g.logView.SetViewport(sf.FloatRect{0.01, .70, .8, .25})
		glb := g.logText.GetGlobalBounds()
		lvCenterX := (g.resW * 0.8) / 2
		lvCenterY := (g.resH * 0.25) / 2
		g.logView.SetCenter(sf.Vector2f{lvCenterX, glb.Height - lvCenterY})
	}

	g.window.SetView(g.logView)
	g.logText.SetString(string(logFile))
	g.logText.Draw(g.window, sf.DefaultRenderStates())
}
Exemplo n.º 6
0
func main() {

	if len(os.Args) < 3 {
		fmt.Println("add the color range function: operiation x ,like + 2 ")
		return
	}

	colorRangeFunc := func(r, g, b, anivalue float32) (outr, outg, outb float32) {
		if os.Args[len(os.Args)-1] != "a" {
			anivalue = 0
		}
		outr = argsCheckAndAddColor(r, 1, anivalue)
		outg = argsCheckAndAddColor(g, 2, anivalue)
		outb = argsCheckAndAddColor(g, 3, anivalue)
		return
	}

	ticker := time.NewTicker(time.Second / 30)
	setting := sf.DefaultContextSettings()
	setting.AntialiasingLevel = 8
	renderWindow := sf.NewRenderWindow(sf.VideoMode{800, 600, 32}, "Events (GoSFML2)", sf.StyleDefault, setting)

	round, err := sf.NewCircleShape()

	imageGot, _, err := images.ReadImage("./011.jpg", -50, -50, 600, 600)
	if err != nil {
		fmt.Println(err)
		return
	}
	imageGot__ := images.ImageEnhanceRGBWithFunc(imageGot, func(r, g, b float32) (outr, outg, outb float32) {
		outr = 127 * float32(math.Sin(float64(r-127)/255*math.Pi+1))
		outg = g
		outb = b
		return
	})
	texture, err := images.ReadTextureFromImage(*imageGot__)
	if err != nil {
		fmt.Println(err)
		return
	}
	texture.SetSmooth(true)
	round.SetPosition(sf.Vector2f{100, 50})
	round.SetRadius(300)
	round.SetTexture(texture, true)

	clip := animations.NewLoopAnimation([]float32{0}, []float32{255}, -1, animations.Pingpong, func(values []float32) {
		imageGot__ = images.ImageEnhanceRGBWithFunc(imageGot, func(r, g, b float32) (outr, outg, outb float32) {
			outr, outg, outb = colorRangeFunc(r, g, b, values[0])
			return
		})
		texture, _ := images.ReadTextureFromImage(*imageGot__)
		round.SetTexture(texture, true)
	}, nil)
	clip.SetFrameCount(60)
	animation := animations.NewAnimation(0)
	animation.AddClip(clip)
	animation.Play()
	for renderWindow.IsOpen() {
		select {
		case <-ticker.C:
			for event := renderWindow.PollEvent(); event != nil; event = renderWindow.PollEvent() {
				switch event.(type) {
				case sf.EventClosed:
					renderWindow.Close()
				}
			}
		}
		animation.Animate()
		renderWindow.Clear(sf.ColorWhite())
		renderWindow.Draw(round, sf.DefaultRenderStates())
		renderWindow.Display()
	}
}
Exemplo n.º 7
0
func (g *Game) Draw() {
	g.window.Clear(sf.ColorMagenta())
	g.window.Draw(g.entity, sf.DefaultRenderStates())
	g.window.Display()
}
Exemplo n.º 8
0
func main() {
	ticker := time.NewTicker(time.Second / 30)
	setting := sf.DefaultContextSettings()
	setting.AntialiasingLevel = 8
	renderWindow := sf.NewRenderWindow(sf.VideoMode{800, 600, 32}, "Events (GoSFML2)", sf.StyleDefault, setting)

	shape, _ := shapeEX.NewCurveCicleShape(10, 100, 8)
	shape.SetFillColor(sf.Color{255, 100, 55, 255})

	animation1 := animations.NewAnimation(1)
	clip10 := animations.NewSingleAnimationClip(
		shape.GetPosition(),
		sf.Vector2f{500, 200},
		func(step interface{}) {
			stepv2, _ := step.(sf.Vector2f)
			shape.SetPosition(stepv2)
		}, func() {
		})
	clip11 := animations.NewSingleAnimationClip(
		sf.Vector2f{500, 200},
		sf.Vector2f{200, 400},
		func(step interface{}) {
			stepv2, _ := step.(sf.Vector2f)
			shape.SetPosition(stepv2)
		}, func() {
		})
	clip10.SetFrameCount(120)
	clip11.SetFrameCount(120)
	animation1.AddClip(clip10)
	animation1.AddClip(clip11)

	animation2 := animations.NewAnimation(1)
	clip20 := animations.NewLoopAnimation(
		0.2, 1.8, -1, animations.Pingpong,
		func(step interface{}) {
			stepv1, _ := step.(float32)
			for i := 0; i < 8; i++ {
				if i%2 == 0 {
					shape.ExpendPointTo(uint(i), stepv1)
				}

			}
		}, func() {
		})
	clip20.SetAnimationCurve(func(num float32) float32 {
		return num * num
	})
	clip20.SetFrameCount(20)
	animation2.AddClip(clip20)

	animation1.Play()
	animation2.Play()
	for renderWindow.IsOpen() {
		select {
		case <-ticker.C:
			for event := renderWindow.PollEvent(); event != nil; event = renderWindow.PollEvent() {
				switch event.(type) {
				case sf.EventClosed:
					renderWindow.Close()
				}
			}
		}
		animation1.Animate()
		animation2.Animate()
		renderWindow.Clear(sf.ColorWhite())
		renderWindow.Draw(shape, sf.DefaultRenderStates())
		renderWindow.Display()
	}
}
Exemplo n.º 9
0
//Draw draws the tiles sprite in the given position in the given window.
func (t *Tile) Draw(window *sf.RenderWindow, x, y int) {
	t.setPosition(x, y)
	t.Sprite.Draw(window, sf.DefaultRenderStates())
}
Exemplo n.º 10
0
func (g *Game) run() {

	for g.window.IsOpen() {

		wait := true
	pollLoop:
		for event := g.window.PollEvent(); event != nil; event = g.window.PollEvent() {
			switch et := event.(type) {
			case sf.EventClosed:
				g.window.Close()
			case sf.EventTextEntered:
				wait = g.handleInput(et.Char)
				break pollLoop
			}
		}
		g.window.Clear(sf.ColorBlack())

		// Draw status stuff.
		g.window.SetView(g.statusView)
		g.hpText.SetString("HP: " + strconv.Itoa(g.player.curhp) + "/" + strconv.Itoa(g.player.maxhp))
		g.hpText.Draw(g.window, sf.DefaultRenderStates())

		g.drawLog()

		if g.state != INVENTORY {
			g.window.SetView(g.gameView)

			g.Draw(g.area)

			//Draw items
			for _, i := range g.items {
				g.Draw(i)
			}

			//Process mobs Ai, check for deaths and draw them.
			for i, m := range g.mobs {
				if m.Mob == nil {
					mPos := m.Position()
					for i := 0; i < 3; i++ { // Spill blood.
						r := rand.Perm(3)
						g.area.tiles[(mPos.X+r[0]-1)+(mPos.Y+r[2]-1)*g.area.width].SetColor(sf.ColorRed())
					}
					g.items = append(g.items, m)
					g.mobs = removeFromList(g.mobs, i)
				}
				if !wait && m != g.player && m.Mob != nil {
					g.processAI(m)
				}
				g.Draw(m)
			}

			//Check if player died.
			if g.player.Mob == nil {
				fmt.Print("Game Over, you died.\n")
				g.window.Close()
				return
			}

			if g.state == LOOK {
				g.Draw(g.cursor)
				g.lookText.Draw(g.window, sf.DefaultRenderStates())
			}

			g.window.Display()
		}
	}

}
Exemplo n.º 11
0
//Draw draws the sprite on the window.
func (e *Entity) Draw(w *sf.RenderWindow) {
	e.sprite.Draw(w, sf.DefaultRenderStates())
}