xxxxxxxxxx
196
// Forked and slightly modified from original work by Bryan Madole (bmad)
let elevation = -300
let groundElevation = 200
let treeLeavesRadius = 50
let treeLeavesHeight = -2.5 * treeLeavesRadius
let treeTrunkWidth = 0.25 * treeLeavesRadius
let treeTrunkHeight = 0.2 * treeLeavesHeight
let treeTrunkOffset = -((treeLeavesHeight / 2) + (treeTrunkHeight / 2))
let grassImage
let treeImage
let trunkImage
let moonImage
let starImage
let starAngle = 0
let moonAngle = 0
let ufoAngle = 0
let speed = 1
let gravity = 0.09
let gravity2 = 0.03
let descentStage = 0
let rocketColors = ['orange', 'yellow', 'red']
function preload() {
grassImage = loadImage('grass.png')
treeImage = loadImage('xmas.jpg')
trunkImage = loadImage('wood.jpg')
starImage = loadImage('stars.jpg')
moonImage = loadImage('moon.jpg')
rocketImage = loadImage('explosion.jpg')
}
function setup() {
createCanvas(700, 700, WEBGL)
noStroke()
}
function draw() {
orbitControl()
background('midnightblue');
directionalLight(255, 255, 255, -1, 1, -1)
pointLight(255, 255, 255, 0, elevation - 30, 0)
drawStars()
drawGround()
drawUfo()
drawForest()
drawMoon()
moveUfo()
}
function moveUfo() {
elevation += speed
speed += gravity
if (elevation >= 100 && descentStage === 0) {
speed = 1
speed -= 5
descentStage = 1
} else if (descentStage === 1 && speed >= 0) {
speed = 1
gravity = 0
}
if (elevation >= 165) {
elevation = 165
noLoop()
}
}
function drawGround() {
push()
texture(grassImage)
spotLight(255, 255, 255, 0, elevation, 0, 0, 1, 0, Math.PI / 6, 3)
translate(0, 200, 0)
rotateX(Math.PI / 2)
plane(550, 550)
pop()
}
function drawStars() {
push()
rotateZ(starAngle)
translate(0, -200, -1000)
texture(starImage)
plane(width * 8, height * 3)
pop()
starAngle += 0.0004
}
function drawMoon() {
push()
translate(width * -0.6, height * -0.6, -500)
rotateY(moonAngle)
texture(moonImage)
sphere()
pop()
moonAngle += 0.003
}
function drawTree(x, y, z) {
push()
translate(x, y, z)
texture(treeImage)
cone(treeLeavesRadius, treeLeavesHeight)
translate(0, treeTrunkOffset, 0)
texture(trunkImage)
cylinder(treeTrunkWidth, treeTrunkHeight)
pop()
}
function drawForest() {
const treeElevation = groundElevation + (treeLeavesHeight / 2) + treeTrunkHeight
drawTree(width * 0.25, treeElevation, -110)
drawTree(width * -0.25, treeElevation, -110)
drawTree(width * 0.25, treeElevation, 110)
drawTree(width * -0.25, treeElevation, 110)
}
function drawUfo() {
push()
rotateY(ufoAngle)
drawUfoBody()
drawUfoTop()
drawUfoTailLight()
drawUfoRunningLights()
drawUfoLandingFin()
drawUfoBoosters()
pop()
ufoAngle += 0.015
}
function drawUfoBody() {
push()
translate(0, elevation, 0)
specularMaterial(250, 250, 250, 1000)
shininess(20)
ellipsoid(80, 20, 80)
pop()
}
function drawUfoTop() {
push()
translate(0, elevation - 20, 0)
specularMaterial(250, 250, 250, 1000)
shininess(20)
ellipsoid(40, 20, 40)
pop()
}
function drawUfoTailLight() {
push()
translate(0, elevation + 20, 0)
emissiveMaterial('white')
sphere(5)
pop()
}
function drawUfoRunningLights() {
push()
for (let theta = 0; theta < 360; theta += Math.PI / 8) {
push()
emissiveMaterial('white')
translate(76 * cos(theta), elevation, 76 * sin(theta))
sphere(5)
pop()
}
pop()
}
function drawUfoLandingFin() {
push()
for (let theta = 0; theta < 360; theta += Math.PI / 1.5) {
push()
translate(50 * cos(theta), elevation + 18, 50 * sin(theta))
specularMaterial(250, 250, 250, 1000)
shininess(20)
cylinder(10, 36)
pop()
}
pop()
}
function drawUfoBoosters() {
if (descentStage === 1) {
for (let theta = 0; theta < 360; theta += Math.PI / 1.5) {
push()
translate(50 * cos(theta), elevation + 46, 50 * sin(theta))
emissiveMaterial(random(rocketColors))
cone(10, 20)
pop()
}
}
}