Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSoC: Add Turtle Class #17

Merged
merged 4 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/PiCraft.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module PiCraft

include("blocks.jl")
include("turtle.jl")

export World, Block, connectToWorld, mc_send, getBlock, setBlock, setBlocks, getHeight, getPlayerIds
export setting, saveWorld, restoreWorld, post, getTile, setTile, getPos, setPos, pollBlockHits
export clearEvents, camera
export turtle, move, yaw, pitch, roll

type World
s::TCPSocket
Expand Down
64 changes: 64 additions & 0 deletions src/turtle.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
turtle

A graphics turtle meant to move in all 3-Dimensions. It contains the following fields:

* `pos` : The coordinates of the turtle.
* `direction` : The direction in which the turtle is facing.
* `normal` : The direction normal to the turtle's body.
* `penBlock` : The block which will be used to draw.
* `stepSize` : The size of a turtle's step.
* `penDown` : Activation state of `penBlock`.

"""
mutable struct turtle
pos::Tuple{Float64, Float64, Float64}
direction::Array{Float64, 1} # Direction turtle is facing, Roll axis
normal::Array{Float64, 1} # Direction normal to the turtle's body, Yaw axis
penBlock::PiCraft.Block
stepSize::Float64
penDown::Bool

turtle(;pos = getPos(), direction = [1.0; 0.0; 0.0], normal = [0.0; -1.0; 0.0], penBlock = Block(41), stepSize = 0.5, penDown = true) = new(pos, direction, normal, penBlock, stepSize, penDown)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a keyword constructor, so that people can use it as turtle(;stepSize = 0.5) etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, this looks good now, but please document the constructor.


"""
move(t::turtle, s::Int)

Move the turtle `t` forward `s` steps.
"""
function move(t::turtle, s::Real)
s = floor(Int, s)
for i in 1:s
t.penDown && setBlock(t.pos, t.penBlock)
t.pos = t.pos .+ Tuple(t.direction*t.stepSize)
end
end

"""
pitch(t::turtle, θ::Real)

Pitch the turtle `t` by `θ` degrees.
"""
function pitch(t::turtle, θ::Real)
yawAxis = cross(t.normal, t.direction)
t.normal, t.direction = normalize(t.normal*cosd(θ) + cross(yawAxis, t.normal)*sind(θ)), normalize(t.direction*cosd(θ) + cross(yawAxis, t.direction)*sind(θ))
end

"""
yaw(t::turtle, θ::Real)

Yaw the turtle `t` by `θ` degrees.
"""
function yaw(t::turtle, θ::Real)
t.direction = normalize(t.direction*cosd(θ) + cross(t.normal, t.direction)*sind(θ))
end

"""
roll(t::turtle, θ::Real)

Roll the turtle `t` by `θ` degrees.
"""
function roll(t::turtle, θ::Real)
t.normal = normalize(t.normal*cosd(θ) + cross(t.direction, t.normal)*sind(θ))
end