func (hv *HouseViewer) Think(g *gui.Gui, t int64) { dt := t - hv.last_timestamp if hv.last_timestamp == 0 { dt = 0 } hv.last_timestamp = t scale := 1 - float32(math.Pow(0.005, float64(dt)/1000)) if hv.target_on { f := mathgl.Vec2{hv.fx, hv.fy} v := mathgl.Vec2{hv.targetx, hv.targety} v.Subtract(&f) v.Scale(scale) f.Add(&v) hv.fx = f.X hv.fy = f.Y } if hv.target_zoom_on { exp := math.Log(float64(hv.zoom)) exp += (float64(hv.targetzoom) - exp) * float64(scale) hv.zoom = float32(math.Exp(exp)) } }
func (e *Entity) TurnToFace(x, y int) { target := mathgl.Vec2{float32(x), float32(y)} source := mathgl.Vec2{float32(e.X), float32(e.Y)} var seg mathgl.Vec2 seg.Assign(&target) seg.Subtract(&source) target_facing := facing(seg) f_diff := target_facing - e.sprite.sp.StateFacing() if f_diff != 0 { f_diff = (f_diff + 6) % 6 if f_diff > 3 { f_diff -= 6 } for f_diff < 0 { e.sprite.sp.Command("turn_left") f_diff++ } for f_diff > 0 { e.sprite.sp.Command("turn_right") f_diff-- } } }
func (hv *HouseViewer) Think(g *gui.Gui, t int64) { dt := t - hv.last_timestamp if hv.last_timestamp == 0 { dt = 0 } hv.last_timestamp = t if hv.target_on { f := mathgl.Vec2{hv.fx, hv.fy} v := mathgl.Vec2{hv.targetx, hv.targety} v.Subtract(&f) scale := 1 - float32(math.Pow(0.005, float64(dt)/1000)) v.Scale(scale) f.Add(&v) hv.fx = f.X hv.fy = f.Y } }
// Advances ent up to dist towards the target cell. Returns the distance // traveled. func (e *Entity) DoAdvance(dist float32, x, y int) float32 { if dist <= 0 { e.sprite.sp.Command("stop") return 0 } e.sprite.sp.Command("move") source := mathgl.Vec2{float32(e.X), float32(e.Y)} target := mathgl.Vec2{float32(x), float32(y)} var seg mathgl.Vec2 seg.Assign(&target) seg.Subtract(&source) e.TurnToFace(x, y) var traveled float32 if seg.Length() > dist { seg.Scale(dist / seg.Length()) traveled = dist } else { traveled = seg.Length() } seg.Add(&source) e.X = float64(seg.X) e.Y = float64(seg.Y) return dist - traveled }
func (wt *WallTexture) setupGlStuff(x, y, dx, dy int, gl_ids *wallTextureGlIds) { if gl_ids.vbuffer != 0 { gl.DeleteBuffers(1, &gl_ids.vbuffer) gl.DeleteBuffers(1, &gl_ids.left_buffer) gl.DeleteBuffers(1, &gl_ids.right_buffer) gl.DeleteBuffers(1, &gl_ids.floor_buffer) gl_ids.vbuffer = 0 gl_ids.left_buffer = 0 gl_ids.right_buffer = 0 gl_ids.floor_buffer = 0 } // All vertices for both walls and the floor will go here and get sent to // opengl all at once var vs []roomVertex // Conveniently casted values frx := float32(x) fry := float32(y) frdx := float32(dx) frdy := float32(dy) tdx := float32(wt.Texture.Data().Dx()) / 100 tdy := float32(wt.Texture.Data().Dy()) / 100 wtx := wt.X wty := wt.Y wtr := wt.Rot if wtx > frdx { wtr -= 3.1415926535 / 2 } // Floor verts := []mathgl.Vec2{ {-tdx / 2, -tdy / 2}, {-tdx / 2, tdy / 2}, {tdx / 2, tdy / 2}, {tdx / 2, -tdy / 2}, } var m, run mathgl.Mat3 run.Identity() m.Translation(wtx, wty) run.Multiply(&m) m.RotationZ(wtr) run.Multiply(&m) if wt.Flip { m.Scaling(-1, 1) run.Multiply(&m) } for i := range verts { verts[i].Transform(&run) } p := mathgl.Poly(verts) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{0, 0}, B: mathgl.Vec2{0, frdy}}) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{0, frdy}, B: mathgl.Vec2{frdx, frdy}}) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{frdx, frdy}, B: mathgl.Vec2{frdx, 0}}) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{frdx, 0}, B: mathgl.Vec2{0, 0}}) if len(p) >= 3 { // floor indices var is []uint16 for i := 1; i < len(p)-1; i++ { is = append(is, uint16(len(vs)+0)) is = append(is, uint16(len(vs)+i)) is = append(is, uint16(len(vs)+i+1)) } gl.GenBuffers(1, &gl_ids.floor_buffer) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl_ids.floor_buffer) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.Sizeiptr(int(unsafe.Sizeof(is[0]))*len(is)), gl.Pointer(&is[0]), gl.STATIC_DRAW) gl_ids.floor_count = gl.Sizei(len(is)) run.Inverse() for i := range p { v := mathgl.Vec2{p[i].X, p[i].Y} v.Transform(&run) vs = append(vs, roomVertex{ x: p[i].X, y: p[i].Y, u: v.X/tdx + 0.5, v: -(v.Y/tdy + 0.5), los_u: (fry + p[i].Y) / LosTextureSize, los_v: (frx + p[i].X) / LosTextureSize, }) } } // Left Wall verts = []mathgl.Vec2{ {-tdx / 2, -tdy / 2}, {-tdx / 2, tdy / 2}, {tdx / 2, tdy / 2}, {tdx / 2, -tdy / 2}, } run.Identity() m.Translation(wtx, wty) run.Multiply(&m) m.RotationZ(wtr) run.Multiply(&m) if wt.Flip { m.Scaling(-1, 1) run.Multiply(&m) } for i := range verts { verts[i].Transform(&run) } p = mathgl.Poly(verts) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{0, 0}, B: mathgl.Vec2{0, frdy}}) p.Clip(&mathgl.Seg2{B: mathgl.Vec2{0, frdy}, A: mathgl.Vec2{frdx, frdy}}) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{frdx, frdy}, B: mathgl.Vec2{frdx, 0}}) if len(p) >= 3 { // floor indices var is []uint16 for i := 1; i < len(p)-1; i++ { is = append(is, uint16(len(vs)+0)) is = append(is, uint16(len(vs)+i)) is = append(is, uint16(len(vs)+i+1)) } gl.GenBuffers(1, &gl_ids.left_buffer) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl_ids.left_buffer) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.Sizeiptr(int(unsafe.Sizeof(is[0]))*len(is)), gl.Pointer(&is[0]), gl.STATIC_DRAW) gl_ids.left_count = gl.Sizei(len(is)) run.Inverse() for i := range p { v := mathgl.Vec2{p[i].X, p[i].Y} v.Transform(&run) vs = append(vs, roomVertex{ x: p[i].X, y: frdy, z: frdy - p[i].Y, u: v.X/tdx + 0.5, v: -(v.Y/tdy + 0.5), los_u: (fry + frdy - 0.5) / LosTextureSize, los_v: (frx + p[i].X) / LosTextureSize, }) } } // Right Wall verts = []mathgl.Vec2{ {-tdx / 2, -tdy / 2}, {-tdx / 2, tdy / 2}, {tdx / 2, tdy / 2}, {tdx / 2, -tdy / 2}, } run.Identity() m.Translation(wtx, wty) run.Multiply(&m) m.RotationZ(wtr) run.Multiply(&m) if wt.Flip { m.Scaling(-1, 1) run.Multiply(&m) } for i := range verts { verts[i].Transform(&run) } p = mathgl.Poly(verts) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{0, frdy}, B: mathgl.Vec2{frdx, frdy}}) p.Clip(&mathgl.Seg2{B: mathgl.Vec2{frdx, frdy}, A: mathgl.Vec2{frdx, 0}}) p.Clip(&mathgl.Seg2{A: mathgl.Vec2{frdx, 0}, B: mathgl.Vec2{0, 0}}) if len(p) >= 3 { // floor indices var is []uint16 for i := 1; i < len(p)-1; i++ { is = append(is, uint16(len(vs)+0)) is = append(is, uint16(len(vs)+i)) is = append(is, uint16(len(vs)+i+1)) } gl.GenBuffers(1, &gl_ids.right_buffer) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl_ids.right_buffer) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.Sizeiptr(int(unsafe.Sizeof(is[0]))*len(is)), gl.Pointer(&is[0]), gl.STATIC_DRAW) gl_ids.right_count = gl.Sizei(len(is)) run.Inverse() for i := range p { v := mathgl.Vec2{p[i].X, p[i].Y} v.Transform(&run) vs = append(vs, roomVertex{ x: frdx, y: p[i].Y, z: frdx - p[i].X, u: v.X/tdx + 0.5, v: -(v.Y/tdy + 0.5), los_u: (fry + p[i].Y) / LosTextureSize, los_v: (frx + frdx - 0.5) / LosTextureSize, }) } } if len(vs) > 0 { gl.GenBuffers(1, &gl_ids.vbuffer) gl.BindBuffer(gl.ARRAY_BUFFER, gl_ids.vbuffer) size := int(unsafe.Sizeof(roomVertex{})) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(size*len(vs)), gl.Pointer(&vs[0].x), gl.STATIC_DRAW) } }