package Levels;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.swing.*;
import javax.vecmath.*;
import Library_3D.*;
import com.sun.j3d.utils.universe.*;
/**
*
* @author Kevin Riehl
*
*/
@SuppressWarnings("serial")
public class Level_1 extends JFrame implements KeyListener
{
//Attribute
//Dimension
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
SimpleUniverse u;
//KeyListener
boolean forward,backward,leftward,rightward;
boolean speed_true = false;
//All
Transform3D ball = new Transform3D();
Transform3D view = new Transform3D();
TransformGroup Ball = new TransformGroup();
float x,y,z;
float rot;
int ix,iy,iz = 0;
float speed = 0.5f;
public Level_1()
{
super("Black'n'White");
setSize(d);
setLayout(new BorderLayout());
setUndecorated(true);
getContentPane().setBackground(Color.black);
BranchGroup scene = createtheScene();
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(config);
add("Center", canvas);
canvas.addKeyListener(this);
u = new SimpleUniverse(canvas);
u.addBranchGraph(scene);
setVisible(true);
}
//Create the Scene
public BranchGroup createtheScene()
{
BranchGroup X = new BranchGroup();
Floor f = new Floor();
X.addChild(f.getChild());
ball = new Transform3D();
ball.setTranslation(new Vector3f(x,y,z-5));
Ball = new TransformGroup(ball);
X.addChild(Ball);
Ball.addChild(new SPHERE());
Ball.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
return X;
}
public void keyPressed(KeyEvent arg0)
{
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
forward = true;
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
backward = true;
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
leftward = true;
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
rightward = true;
}
doMoves();
}
public void keyReleased(KeyEvent arg0)
{
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
forward = false;
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
backward = false;
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
leftward = false;
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
rightward = false;
}
}
public void doMoves()
{
if(forward)
{
checkFor();
}
if(backward)
{
checkBack();
}
if(leftward)
{
checkLeft();
}
if(rightward)
{
checkRight();
}
}
public void keyTyped(KeyEvent arg0)
{
}
public void checkRight()
{
if(ix>=8)
{
;
}
else
{
ix++;
x=x+0.1f;
view.setTranslation(new Vector3f(0,y,z));
u.getViewingPlatform().getViewPlatformTransform().setTransform(view);
ball.setTranslation(new Vector3f(x,y,z-5));
Ball.setTransform(ball);
}
}
public void checkLeft()
{
if(ix<=-8)
{
;
}
else
{
ix--;
x=x-0.1f;
view.setTranslation(new Vector3f(0,y,z));
u.getViewingPlatform().getViewPlatformTransform().setTransform(view);
ball.setTranslation(new Vector3f(x,y,z-5));
Ball.setTransform(ball);
}
}
public void checkFor()
{
iz--;
z=z-speed;
rot--;rot--;
System.out.println(iz);
view.setTranslation(new Vector3f(0,y,z));
u.getViewingPlatform().getViewPlatformTransform().setTransform(view);
ball.rotX(Math.toRadians(rot));
ball.setTranslation(new Vector3f(x,y,z-5));
Ball.setTransform(ball);
}
public void checkBack()
{
if(ix==0&&iz==-10)
{
iz++;
z=z-speed;
System.out.println(iz);
rot--;rot--;
view.setTranslation(new Vector3f(0,y,z));
u.getViewingPlatform().getViewPlatformTransform().setTransform(view);
ball.rotX(Math.toRadians(rot));
ball.setTranslation(new Vector3f(x,y,z-5));
Ball.setTransform(ball);
}
else
{
iz++;
z=z+speed;
rot++;rot++;
view.setTranslation(new Vector3f(0,y,z));
u.getViewingPlatform().getViewPlatformTransform().setTransform(view);
ball.rotX(Math.toRadians(rot));
ball.setTranslation(new Vector3f(x,y,z-5));
Ball.setTransform(ball);
}
}
public static void main(String[]args)
{
new Level_1();
}
}