Exemplo n.º 1
0
func UpdateSimulation() {
	cellw := SCREEN_WIDTH / (SEA_SUBDIVISION - 1)

	var col1, col2 color.ColorRGB

	// Update time of day
	if speed == 0.0 {
		timet = GetTime()
	} else {
		timet += Delta() * speed
		if timet >= 24.0 {
			timet -= 24.0
		}
	}

	seq_id = int(timet / 3)
	seq_residue = timet/3 - float64(seq_id)
	zenith := -(timet/12.0*hge.Pi - hge.Pi_2)

	// Interpolate sea and sky colors
	col1.SetHWColor(skyTopColors[seq[seq_id]])
	col2.SetHWColor(skyTopColors[seq[seq_id+1]])
	colSkyTop = col2.MulScalar(seq_residue).Add(col1.MulScalar(1.0 - seq_residue))

	col1.SetHWColor(skyBtmColors[seq[seq_id]])
	col2.SetHWColor(skyBtmColors[seq[seq_id+1]])
	colSkyBtm = col2.MulScalar(seq_residue).Add(col1.MulScalar(1.0 - seq_residue))

	col1.SetHWColor(seaTopColors[seq[seq_id]])
	col2.SetHWColor(seaTopColors[seq[seq_id+1]])
	colSeaTop = col2.MulScalar(seq_residue).Add(col1.MulScalar(1.0 - seq_residue))

	col1.SetHWColor(seaBtmColors[seq[seq_id]])
	col2.SetHWColor(seaBtmColors[seq[seq_id+1]])
	colSeaBtm = col2.MulScalar(seq_residue).Add(col1.MulScalar(1.0 - seq_residue))

	var a float64
	// Update stars
	if seq_id >= 6 || seq_id < 2 {
		for i := 0; i < NUM_STARS; i++ {
			a = 1.0 - starY[i]/STARS_HEIGHT
			a *= rand.Float64(0.6, 1.0)
			if seq_id >= 6 {
				a *= math.Sin((timet - 18.0) / 6.0 * hge.Pi_2)
			} else {
				a *= math.Sin((1.0 - timet/6.0) * hge.Pi_2)
			}
			starA[i] = a
		}
	}

	// Calculate sun position, scale and colors
	if seq_id == 2 {
		a = math.Sin(seq_residue * hge.Pi_2)
	} else if seq_id == 5 {
		a = math.Cos(seq_residue * hge.Pi_2)
	} else if seq_id > 2 && seq_id < 5 {
		a = 1.0
	} else {
		a = 0.0
	}

	colSun.SetHWColor(0xFFEAE1BE)
	colSun = colSun.MulScalar(1 - a).Add(colWhite.MulScalar(a))

	a = (math.Cos(timet/6.0*hge.Pi) + 1.0) / 2.0
	if seq_id >= 2 && seq_id <= 6 {
		colSunGlow = colWhite.MulScalar(a)
		colSunGlow.A = 1.0
	} else {
		colSunGlow.SetHWColor(0xFF000000)
	}

	sunX = SCREEN_WIDTH*0.5 + math.Cos(zenith)*ORBITS_RADIUS
	sunY = SKY_HEIGHT*1.2 + math.Sin(zenith)*ORBITS_RADIUS
	sunS = 1.0 - 0.3*math.Sin((timet-6.0)/12.0*hge.Pi)
	sunGlowS = 3.0*(1.0-a) + 3.0

	// Calculate moon position, scale and colors
	if seq_id >= 6 {
		a = math.Sin((timet - 18.0) / 6.0 * hge.Pi_2)
	} else {
		a = math.Sin((1.0 - timet/6.0) * hge.Pi_2)
	}
	colMoon.SetHWColor(0x20FFFFFF)
	colMoon = colMoon.MulScalar(1 - a).Add(colWhite.MulScalar(a))

	colMoonGlow = colWhite
	colMoonGlow.A = 0.5 * a

	moonX = SCREEN_WIDTH*0.5 + math.Cos(zenith-hge.Pi)*ORBITS_RADIUS
	moonY = SKY_HEIGHT*1.2 + math.Sin(zenith-hge.Pi)*ORBITS_RADIUS
	moonS = 1.0 - 0.3*math.Sin((timet+6.0)/12.0*hge.Pi)
	moonGlowS = a*0.4 + 0.5

	// Calculate sea glow
	if timet > 19.0 || timet < 4.5 { // moon
		a = 0.2 // intensity
		if timet > 19.0 && timet < 20.0 {
			a *= (timet - 19.0)
		} else if timet > 3.5 && timet < 4.5 {
			a *= 1.0 - (timet - 3.5)
		}

		colSeaGlow = colMoonGlow
		colSeaGlow.A = a
		seaGlowX = moonX
		seaGlowSX = moonGlowS * 3.0
		seaGlowSY = moonGlowS * 2.0
	} else if timet > 6.5 && timet < 19.0 { // sun
		a = 0.3 // intensity
		if timet < 7.5 {
			a *= (timet - 6.5)
		} else if timet > 18.0 {
			a *= 1.0 - (timet - 18.0)
		}

		colSeaGlow = colSunGlow
		colSeaGlow.A = a
		seaGlowX = sunX
		seaGlowSX = sunGlowS
		seaGlowSY = sunGlowS * 0.6
	} else {
		colSeaGlow.A = 0.0
	}

	var dwCol1 hge.Dword
	// Move waves and update sea color
	for i := 1; i < SEA_SUBDIVISION-1; i++ {
		a = float64(i) / (SEA_SUBDIVISION - 1)
		col1 = colSeaTop.MulScalar(1 - a).Add(colSeaBtm.MulScalar(a))
		dwCol1 = col1.HWColor()
		fTime := 2.0 * Time()
		a *= 20

		for j := 0; j < SEA_SUBDIVISION; j++ {
			sea.SetColor(j, i, dwCol1)

			dy := a * math.Sin(seaP[i]+(float64(j)/(SEA_SUBDIVISION-1)-0.5)*hge.Pi*16.0-fTime)
			sea.SetDisplacement(j, i, 0.0, dy, dist.DISP_NODE)
		}
	}

	dwCol1 = colSeaTop.HWColor()
	dwCol2 := colSeaBtm.HWColor()

	for j := 0; j < SEA_SUBDIVISION; j++ {
		sea.SetColor(j, 0, dwCol1)
		sea.SetColor(j, SEA_SUBDIVISION-1, dwCol2)
	}

	var posX float64
	// Calculate light path
	if timet > 19.0 || timet < 5.0 { // moon
		a = 0.12 // intensity
		if timet > 19.0 && timet < 20.0 {
			a *= (timet - 19.0)
		} else if timet > 4.0 && timet < 5.0 {
			a *= 1.0 - (timet - 4.0)
		}
		posX = moonX
	} else if timet > 7.0 && timet < 17.0 { // sun
		a = 0.14 // intensity
		if timet < 8.0 {
			a *= (timet - 7.0)
		} else if timet > 16.0 {
			a *= 1.0 - (timet - 16.0)
		}
		posX = sunX
	} else {
		a = 0.0
	}

	if a != 0.0 {
		k := int(math.Floor(posX / float64(cellw)))
		s1 := (1.0 - (posX-float64(k*cellw))/float64(cellw))
		s2 := (1.0 - (float64((k+1)*cellw)-posX)/float64(cellw))

		if s1 > 0.7 {
			s1 = 0.7
		}
		if s2 > 0.7 {
			s2 = 0.7
		}

		s1 *= a
		s2 *= a

		for i := 0; i < SEA_SUBDIVISION; i += 2 {
			a = math.Sin(float64(i) / (SEA_SUBDIVISION - 1) * hge.Pi_2)

			col1.SetHWColor(sea.Color(k, i))
			col1.AddEqual(colSun.MulScalar(s1 * (1 - a)))
			col1.Clamp()
			sea.SetColor(k, i, col1.HWColor())

			col1.SetHWColor(sea.Color(k+1, i))
			col1.AddEqual(colSun.MulScalar(s2 * (1 - a)))
			col1.Clamp()
			sea.SetColor(k+1, i, col1.HWColor())
		}
	}
}
Exemplo n.º 2
0
func NewGUIMenuItem(id int, font *Font.Font, snd *Effect, x, y, delay float64, title string) *gui.GUIObject {
	mi := new(GUIMenuItem)

	mi.GUIObject.Initialize()

	mi.GUIObject.Id = id
	mi.font = font
	mi.snd = snd
	mi.delay = delay
	mi.title = title

	mi.color.SetHWColor(0xFFFFE060)
	mi.shadow.SetHWColor(0x30000000)
	mi.offset = 0.0
	mi.timer = -1.0
	mi.timer2 = -1.0

	mi.GUIObject.Static = false
	mi.GUIObject.Visible = true
	mi.GUIObject.Enabled = true

	w := font.GetStringWidth(title)
	mi.GUIObject.Rect.Set(x-w/2, y, x+w/2, y+mi.font.GetHeight())

	mi.GUIObject.Render = func() {
		mi.font.SetColor(mi.shadow.HWColor())
		mi.font.Render(mi.GUIObject.Rect.X1+mi.offset+3, mi.GUIObject.Rect.Y1+3, Font.TEXT_LEFT, mi.title)
		mi.font.SetColor(mi.color.HWColor())
		mi.font.Render(mi.GUIObject.Rect.X1-mi.offset, mi.GUIObject.Rect.Y1-mi.offset, Font.TEXT_LEFT, mi.title)
	}

	mi.GUIObject.Update = func(dt float64) {
		if mi.timer2 != -1.0 {
			mi.timer2 += dt
			if mi.timer2 >= mi.delay+0.1 {
				mi.color = mi.scolor2.Add(mi.dcolor2)
				mi.shadow = mi.sshadow.Add(mi.dshadow)
				mi.offset = 0.0
				mi.timer2 = -1.0
			} else {
				if mi.timer2 < mi.delay {
					mi.color = mi.scolor2
					mi.shadow = mi.sshadow
				} else {
					mi.color = mi.scolor2.Add(mi.dcolor2.MulScalar((mi.timer2 - mi.delay) * 10))
					mi.shadow = mi.sshadow.Add(mi.dshadow.MulScalar((mi.timer2 - mi.delay) * 10))
				}
			}
		} else if mi.timer != -1.0 {
			mi.timer += dt
			if mi.timer >= 0.2 {
				mi.color = mi.scolor.Add(mi.dcolor)
				mi.offset = mi.soffset + mi.doffset
				mi.timer = -1.0
			} else {
				mi.color = mi.scolor.Add(mi.dcolor.MulScalar(mi.timer * 5))
				mi.offset = mi.soffset + mi.doffset*mi.timer*5
			}
		}
	}

	mi.GUIObject.Enter = func() {
		var tcolor2 Color.ColorRGB

		mi.scolor2.SetHWColor(0x00FFE060)
		tcolor2.SetHWColor(0xFFFFE060)
		mi.dcolor2 = tcolor2.Sub(mi.scolor2)

		mi.sshadow.SetHWColor(0x00000000)
		tcolor2.SetHWColor(0x30000000)
		mi.dshadow = tcolor2.Sub(mi.sshadow)

		mi.timer2 = 0.0
	}

	mi.GUIObject.Leave = func() {
		var tcolor2 Color.ColorRGB

		mi.scolor2.SetHWColor(0xFFFFE060)
		tcolor2.SetHWColor(0x00FFE060)
		mi.dcolor2 = tcolor2.Sub(mi.scolor2)

		mi.sshadow.SetHWColor(0x30000000)
		tcolor2.SetHWColor(0x00000000)
		mi.dshadow = tcolor2.Sub(mi.sshadow)

		mi.timer2 = 0.0
	}

	mi.GUIObject.IsDone = func() bool {
		if mi.timer2 == -1.0 {
			return true
		}

		return false
	}

	mi.GUIObject.Focus = func(focused bool) {
		var tcolor Color.ColorRGB

		if focused {
			snd.Play()
			mi.scolor.SetHWColor(0xFFFFE060)
			tcolor.SetHWColor(0xFFFFFFFF)
			mi.soffset = 0
			mi.doffset = 4
		} else {
			mi.scolor.SetHWColor(0xFFFFFFFF)
			tcolor.SetHWColor(0xFFFFE060)
			mi.soffset = 4
			mi.doffset = -4
		}

		mi.dcolor = tcolor.Sub(mi.scolor)
		mi.timer = 0.0
	}

	mi.GUIObject.MouseOver = func(over bool) {
		if over {
			mi.GUIObject.GUI.SetFocus(mi.GUIObject.Id)
		}
	}

	mi.GUIObject.MouseLButton = func(down bool) bool {
		if !down {
			mi.offset = 4
			return true
		}

		snd.Play()
		mi.offset = 0
		return false
	}

	mi.GUIObject.KeyClick = func(key Key, chr int) bool {
		if key == K_ENTER || key == K_SPACE {
			mi.GUIObject.MouseLButton(true)
			return mi.GUIObject.MouseLButton(false)
		}

		return false
	}

	return &mi.GUIObject
}