|
|
@ -44,7 +44,7 @@ type Game struct { |
|
|
|
status string |
|
|
|
status string |
|
|
|
width int |
|
|
|
width int |
|
|
|
height int |
|
|
|
height int |
|
|
|
enemies map[Position]Enemy |
|
|
|
enemies map[Position]*Enemy |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) DrawStatus(msg string) { |
|
|
|
func (game *Game) DrawStatus(msg string) { |
|
|
@ -135,10 +135,42 @@ func (game *Game) Occupied(x int, y int) bool { |
|
|
|
is_player |
|
|
|
is_player |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Death() { |
|
|
|
|
|
|
|
is_dead := make([]Position, 0, len(game.enemies)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for pos, enemy := range game.enemies { |
|
|
|
|
|
|
|
if enemy.hp < 0 { |
|
|
|
|
|
|
|
is_dead = append(is_dead, pos) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range is_dead { |
|
|
|
|
|
|
|
delete(game.enemies, pos) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Attack(target Position) { |
|
|
|
|
|
|
|
enemy, hit_enemy := game.enemies[target] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if hit_enemy { |
|
|
|
|
|
|
|
damage := rand.Int() % 10 |
|
|
|
|
|
|
|
enemy.hp -= damage |
|
|
|
|
|
|
|
if damage == 0 { |
|
|
|
|
|
|
|
game.Status("YOU MISSED!") |
|
|
|
|
|
|
|
} else if enemy.hp > 0 { |
|
|
|
|
|
|
|
game.Status(fmt.Sprintf("HIT %d damage", damage)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
game.Status("ENEMY DEAD!") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
if !game.Occupied(game.player.x + x_delta, game.player.y + y_delta) { |
|
|
|
target := Position{game.player.x + x_delta, game.player.y + y_delta} |
|
|
|
game.player.x += x_delta |
|
|
|
if game.Occupied(target.x, target.y) { |
|
|
|
game.player.y += y_delta |
|
|
|
game.Attack(target) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
game.player = target |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -185,7 +217,7 @@ func MakeGame(width int, height int) (*Game) { |
|
|
|
|
|
|
|
|
|
|
|
game.width = width |
|
|
|
game.width = width |
|
|
|
game.height = height |
|
|
|
game.height = height |
|
|
|
game.enemies = make(map[Position]Enemy) |
|
|
|
game.enemies = make(map[Position]*Enemy) |
|
|
|
|
|
|
|
|
|
|
|
game.level = make(Map, height, height) |
|
|
|
game.level = make(Map, height, height) |
|
|
|
game.paths = make(Paths, height, height) |
|
|
|
game.paths = make(Paths, height, height) |
|
|
@ -464,7 +496,7 @@ func (game *Game) MakeMap() []Position { |
|
|
|
func (game *Game) PlaceEnemies(places []Position) { |
|
|
|
func (game *Game) PlaceEnemies(places []Position) { |
|
|
|
for _, pos := range places { |
|
|
|
for _, pos := range places { |
|
|
|
if rand.Int() % 2 == 0 { |
|
|
|
if rand.Int() % 2 == 0 { |
|
|
|
game.enemies[pos] = Enemy{10} |
|
|
|
game.enemies[pos] = &Enemy{10} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -480,6 +512,7 @@ func main() { |
|
|
|
game.Render() |
|
|
|
game.Render() |
|
|
|
|
|
|
|
|
|
|
|
for game.HandleEvents() { |
|
|
|
for game.HandleEvents() { |
|
|
|
|
|
|
|
game.Death() |
|
|
|
game.PathEnemies() |
|
|
|
game.PathEnemies() |
|
|
|
game.EnemyMovement() |
|
|
|
game.EnemyMovement() |
|
|
|
game.Render() |
|
|
|
game.Render() |
|
|
|