Пример #1
0
func commandToAction(command command, subjectID world.ActorID) ia.Action {
	var action ia.Action
	switch command {
	// If an action is what an actor does when it's its turn to play, then
	// maybe we don't want turning to be one.  Moving yes, turning no.  We'll
	// see.
	case commandTurnLeft:
		action = ia.ActionTurn{
			SubjectID: subjectID,
			Direction: world.LEFT(),
			Steps:     1,
		}
	case commandTurnRight:
		action = ia.ActionTurn{
			SubjectID: subjectID,
			Direction: world.RIGHT(),
			Steps:     1,
		}
	case commandForward:
		action = ia.ActionMoveRelative{
			SubjectID: subjectID,
			Direction: world.FRONT(),
			Steps:     1,
		}
	case commandStrafeLeft:
		action = ia.ActionMoveRelative{
			SubjectID: subjectID,
			Direction: world.LEFT(),
			Steps:     1,
		}
	case commandBackward:
		action = ia.ActionMoveRelative{
			SubjectID: subjectID,
			Direction: world.BACK(),
			Steps:     1,
		}
	case commandStrafeRight:
		action = ia.ActionMoveRelative{
			SubjectID: subjectID,
			Direction: world.RIGHT(),
			Steps:     1,
		}
	}
	return action
}
Пример #2
0
func levelCommand(level world.Level, position world.Position, command command) world.Level {
	hereX, hereY := position.X, position.Y
	there := position.MoveForward(1)
	thereX, thereY := there.X, there.Y
	switch command {
	case commandPlaceFloor:
		floor := world.MakeFloor(floorID, world.EAST(), true)
		level.Floors = level.Floors.Set(thereX, thereY, floor)
	case commandPlaceCeiling:
		ceiling := world.MakeOrientedBuilding(ceilingID, world.EAST())
		level.Ceilings = level.Ceilings.Set(thereX, thereY, ceiling)
	case commandPlaceWall:
		{
			// If the player faces North, then the wall must face South in order
			// to face the player.
			facing := position.F.Add(world.BACK())
			index := facing.Value()
			wall := world.MakeWall(wallID, false)
			level.Walls[index] = level.Walls[index].Set(hereX, hereY, wall)
		}
	case commandPlaceColumn:
		level.Columns = level.Columns.Set(thereX, thereY, world.MakeOrientedBuilding(columnID, world.EAST()))
	case commandRotateFloorDirect, commandRotateFloorRetrograde:
		{
			building := level.Floors[world.Location{X: thereX, Y: thereY}]
			floor, ok := building.(world.Floor)
			if ok {
				var relDir world.RelativeDirection
				if command == commandRotateFloorDirect {
					relDir = world.LEFT()
				} else {
					relDir = world.RIGHT()
				}
				floor.F = floor.F.Add(relDir)
				level.Floors = level.Floors.Set(thereX, thereY, floor)
			} else {
				fmt.Println("You cannot rotate that.")
			}
		}
	case commandRotateColumnDirect, commandRotateColumnRetrograde:
		{
			var relDir world.RelativeDirection
			if command == commandRotateColumnDirect {
				relDir = world.LEFT()
			} else {
				relDir = world.RIGHT()
			}
			column := level.Columns[world.Location{X: thereX, Y: thereY}]
			orientable, ok := column.(world.OrientedBuilding)
			if ok {
				orientable.F = orientable.F.Add(relDir)
				level.Columns = level.Columns.Set(thereX, thereY, orientable)
			} else {
				fmt.Println("You cannot rotate that.")
			}
		}
	case commandRotateCeilingDirect, commandRotateCeilingRetrograde:
		{
			var relDir world.RelativeDirection
			if command == commandRotateCeilingDirect {
				relDir = world.LEFT()
			} else {
				relDir = world.RIGHT()
			}
			ceiling := level.Ceilings[world.Location{X: thereX, Y: thereY}]
			orientable, ok := ceiling.(world.OrientedBuilding)
			if ok {
				orientable.F = orientable.F.Add(relDir)
				level.Ceilings = level.Ceilings.Set(thereX, thereY, orientable)
			} else {
				fmt.Println("You cannot rotate that.")
			}
		}
	case commandRemoveFloor:
		level.Floors = level.Floors.Delete(thereX, thereY)
	case commandRemoveColumn:
		level.Columns = level.Columns.Delete(thereX, thereY)
	case commandRemoveCeiling:
		level.Ceilings = level.Ceilings.Delete(thereX, thereY)
	case commandRemoveWall:
		{
			// If the player faces North, then the wall must face South in order
			// to face the player.
			facing := position.F.Add(world.BACK())
			index := facing.Value()
			level.Walls[index] = level.Walls[index].Delete(hereX, hereY)
		}

	case commandPlaceMonster:
		{
			creature := world.MakeCreature()
			creature.F = position.F
			creatures, creatureID := level.Creatures.Add(creature)
			actors, actorID := level.Actors.Add(world.MakeActor())
			creatureActors, err := level.CreatureActor.Add(creatureID, actorID)
			if err != nil {
				fmt.Println(err)
				break
			}
			creatureLocations, err := level.CreatureLocation.Add(creatureID, there.ToLocation())
			if err != nil {
				fmt.Println(err)
				break
			}
			level.Creatures = creatures
			level.Actors = actors
			level.CreatureActor = creatureActors
			level.CreatureLocation = creatureLocations
		}
	case commandRemoveMonster:
		{
			fmt.Printf("Not implemented.")
		}
	}
	return level
}