Пример #1
0
func (m *Map) Draw(ctx *html.Context) {
	if m.dirty || m.cache == nil {
		m.dirty = false
		if m.cache == nil {
			m.cache = html.NewCanvas(m.Width*tileSize, m.Height*tileSize)
		}
		ctx := m.cache.Context()
		for x := 0; x < m.Width; x++ {
			for y := 0; y < m.Height; y++ {
				m.Tile(x, y).Draw(ctx, x, y)
			}
		}
	}
	ctx.DrawImageSection(m.cache, int(cameraX), int(cameraY), 400, 240, 0, 0, 400, 240)
}
Пример #2
0
func NewMap(path string) *Map {
	m := &Map{
		Width:  256,
		Height: 256,
	}
	m.Tiles = make([]Tile, m.Width*m.Height)
	for i := range m.Tiles {
		m.Tiles[i] = Empty
	}

	img := Images[path]
	canvas := html.NewCanvas(m.Width, m.Height)
	ctx := canvas.Context()
	ctx.DrawImage(img, 0, 0)
	id := ctx.GetImageData(0, 0, 256, 256)

	data := id.Data

	for x := 0; x < m.Width; x++ {
		for y := 0; y < m.Height; y++ {
			idx := (x + y*m.Width) * 4
			col := (int(data[idx]) << 16) | (int(data[idx+1]) << 8) | int(data[idx+2])
			switch col {
			case 0x000000:
				m.SetTile(x, y, InvisWall)
			case 0x824700:
				m.SetTile(x, y, DirtWall)
			case 0x432500:
				m.SetTile(x, y, DirtBack)
			case 0x917f69:
				m.SetTile(x, y, BrickWall)
			case 0x725d45:
				m.SetTile(x, y, BrickBack)
			case 0xffff00:
				m.SetTile(x, y, Window1)
			case 0x00ffcc:
				m.SetTile(x, y, Window2)
			case 0xff00ff:
				m.StartX, m.StartY = x, y
			}
		}
	}
	return m
}
Пример #3
0
func NewPerson(x, y float64) *Person {
	p := &Person{
		X: x, Y: y,
		lastDir: 1,
	}
	img := Images["player"]
	p.Sprite = html.NewCanvas(256, 4*32*2)
	ctx := p.Sprite.Context()
	ctx.DrawImageSection(img, 0, 32*2, 256, 32*2*4, 0, 0, 256, 32*2*4)
	id := ctx.GetImageData(0, 0, 256, 4*32*2)
	hairR, hairG, hairB := globalRand.Intn(256), globalRand.Intn(256), globalRand.Intn(256)
	bodyR, bodyG, bodyB := globalRand.Intn(256), globalRand.Intn(256), globalRand.Intn(256)
	legsR, legsG, legsB := globalRand.Intn(256), globalRand.Intn(256), globalRand.Intn(256)
	for y := 0; y < 32*2*4; y++ {
		if (y % (32 * 4)) < 32 {
			continue
		}
		var nR, nG, nB int
		if (y % (32 * 4)) < 64 {
			nR, nG, nB = hairR, hairG, hairB
		} else if (y % (32 * 4)) < 96 {
			nR, nG, nB = bodyR, bodyG, bodyB
		} else {
			nR, nG, nB = legsR, legsG, legsB
		}
		for x := 0; x < 256; x++ {
			r := id.DataIndex((x + y*256) * 4)
			a := id.DataIndex((x+y*256)*4 + 3)
			if a < 100 {
				continue
			}
			id.SetDataIndex((x+y*256)*4, byte((float64(nR)*float64(r))/256))
			id.SetDataIndex((x+y*256)*4+1, byte((float64(nG)*float64(r))/256))
			id.SetDataIndex((x+y*256)*4+2, byte((float64(nB)*float64(r))/256))
		}
	}

	ctx.PutImageData(id, 0, 0)
	return p
}
Пример #4
0
func realMain() {
	initMaps()
	canvas = html.NewCanvas(800, 480)
	canvas.AddTo(js.Global.Get("document").Get("body"))
	ctx = canvas.Context()
	ctx.SetImageSmoothingEnabled(false)

	audio := js.Global.Get("Audio").New()
	if audio.Call("canPlayType", "audio/mpeg").Bool() {
		audio.Set("type", "audio/mpeg")
		audio.Set("src", "music/music.mp3")
	} else {
		audio.Set("type", "audio/ogg")
		audio.Set("src", "music/music.ogg")
	}
	audio.Set("loop", true)
	audio.Set("volume", 0.4)
	audio.Call("play")

	instructions := js.Global.Get("document").Call("createElement", "pre")
	instructions.Set("innerHTML", `Instructions:
Try and kill as much as you can in one night (the time is takes for the moon to travel
across the sky).

Controls:
WASD - Move
W/Space - Jump
Hold W/Space whilst landing on a wall - Wall jump
Left click = Attack
Right click = Leap
		`)
	js.Global.Get("document").Get("body").Call("appendChild", instructions)

	SetLevel(0)
	html.RequestFrame(drawFrame)
}