{"size": 1990, "ext": "js", "max_stars_repo_path": "src/player.js", "max_stars_repo_name": "huaruiwu/snaketron.js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/player.js", "max_issues_repo_name": "huaruiwu/snaketron.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/player.js", "max_forks_repo_name": "huaruiwu/snaketron.js", "max_forks_repo_forks_event_min_datetime": "2018-08-30T19:49:39.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-30T19:49:39.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 17.7678571429, "max_line_length": 61, "alphanum_fraction": 0.5005025126}
const ctx = require('axel')

const { getGameWidth } = require('./utils')

class Player {
	constructor(
		color = [0, 255, 0],
		x = 0,
		y = 0,
		dir = 'down',
		size = 1,
		length = 10
	) {
		this.body = []
		for (let i = 0; i < length; i++) {
			this.body.unshift([x, y - i])
		}
		this.dir = dir
		this.color = color
		this.size = size
	}

	get x() {
		const { body } = this
		return body[body.length - 1][0]
	}

	get y() {
		const { body } = this
		return body[body.length - 1][1]
	}

	// powered wins in head to head collisions
	setPowered(powered) {
		this.powered = powered
	}

	setDir(dir) {
		this.dir = dir
	}

	setGrow() {
		this.grow = true
	}

	move() {
		const { body, size, grow } = this
		const [x, y] = body[body.length - 1]
		switch (this.dir) {
			case 'up':
				if (y - size < 0) {
					body.push([x, ctx.rows - size])
				} else {
					body.push([x, y - size])
				}
				break
			case 'down':
				if (y + size > ctx.rows) {
					body.push([x, size])
				} else {
					body.push([x, y + size])
				}
				break
			case 'left':
				if (x - 2 * size < 0) {
					body.push([getGameWidth() - 2 * size, y])
				} else {
					body.push([x - 2 * size, y])
				}
				break
			case 'right':
				if (x + 2 * size > getGameWidth()) {
					body.push([2 * size, y])
				} else {
					body.push([x + 2 * size, y])
				}
				break
		}
		if (!grow) {
			this.body = body.slice(1)
		} else {
			this.grow = false
		}
		for (let i = 0; i < this.body.length - 2; i++) {
			if (
				this.body[i][0] === this.body[this.body.length - 1][0] &&
				this.body[i][1] === this.body[this.body.length - 1][1]
			) {
				this.body = this.body.slice(i)
			}
		}
	}

	draw() {
		const { body, color, size, powered } = this
		body.forEach(([x, y], i) => {
			if (i === body.length - 1) {
				if (powered) {
					ctx.bg(232, 89, 12)
				} else {
					ctx.bg(255, 255, 255)
				}
			} else {
				ctx.bg(color[0], color[1], color[2])
			}
			ctx.box(x, y, size * 2, size)
		})
	}
}

module.exports = Player
