|
001 package examples.multiview;
002
003 import java.awt.geom.Point2D;
004
005 import fang.*;
006
007
008 /**Demonstrates scrolling by moving the
009 * mouse in the direction desired from
010 * the center of the screen. This example
011 * extends on the Navigator example by
012 * adding extra players, each with their
013 * own view.
014 * @author Jam Jenkins*/
015 public class MultiView extends Game
016 {
017 /** tiles image reflected vertically and
018 * horizontally to make it seamless.*/
019 private ImageSprite background;
020
021 /** the players*/
022 private StringSprite[] playerSprites;
023
024 /** the trackers for the players*/
025 private ProjectileTransformer[] playerTransformers;
026
027 /**the tracker used for the scrolling*/
028 private ScrollTracker tracker;
029
030 public MultiView()
031 {
032 setNumberOfPlayers(2);
033 }
034
035 /** makes and adds the sprites */
036 public void setup()
037 {
038 makeSprites();
039 addSprites();
040 setHelp("resources/MultiViewHelp.txt");
041 }
042
043 /** makes the image sprites for the background
044 * and sets a scrolling tracker on it. Also
045 * sets each player's sprite to have a
046 * ProjectileTracker on it.*/
047 private void makeSprites()
048 {
049 playerSprites=new StringSprite[getNumberOfPlayers()];
050 playerTransformers=new ProjectileTransformer[getNumberOfPlayers()];
051 for(int i=0; i<playerSprites.length; i++)
052 {
053 playerSprites[i]=new StringSprite(getPlayer(i).getName());
054 playerSprites[i].setWidth(0.25);
055 playerSprites[i].setLocation(0.5, 0.5);
056 playerTransformers[i]=new ProjectileTransformer(new Point2D.Double());
057 playerSprites[i].addTransformer(playerTransformers[i]);
058 }
059
060 background=new ImageSprite("resources/road.jpg");
061 background.setScale(1);
062 tracker=new ScrollTracker(
063 true,
064 new Point2D.Double(),
065 background,
066 3);
067 background.setTracker(tracker);
068 }
069
070 /**adds the sprites to the canvas*/
071 private void addSprites()
072 {
073 addSprite(background);
074 addSprite(playerSprites);
075 }
076
077 /**
078 * gets the velocity to move the player at.
079 * The player only moves on mouse click. If
080 * you want the player to move like in the
081 * Navigator example, change the first line
082 * to just get the mouse location instead of
083 * the mouse click location.
084 * @param index the index of the player's mouse
085 * @return a velocity equivalent to the vector
086 * from the mouse position to the center of the
087 * screen.
088 */
089 private Location2D getVelocity(int index)
090 {
091 Location2D mouse=getClick2D(index);
092 if(mouse==null) return new Location2D();
093 Location2D center=new Location2D(0.5, 0.5);
094 double distance=mouse.distance(center);
095 double maxDistance=center.distance(1, 1);
096 Location2D v;
097 /**only move if more than 10% the distance from
098 * the center to the corner*/
099 if(distance/maxDistance>0.1)
100 {
101 v=new Location2D(
102 center.x-mouse.x,
103 center.y-mouse.y);
104 }
105 else
106 v=new Location2D();
107 return v;
108 }
109
110 /**uses the position of the mouse in relation
111 * to the center of the screen to move the
112 * background in the direction and speed
113 * indicated. The angle from the center to the
114 * mouse if the direction the background moves
115 * and as the distance from the center of the
116 * screen increases, so too does the scroll speed*/
117 public void advance(double timeAdvanced)
118 {
119 Location2D v=getVelocity(getID());
120 for(int i=0; i<playerTransformers.length; i++)
121 {
122 Location2D velocity=getVelocity(i);
123 if(i!=getID())
124 {
125 //other players movements are affected not only
126 //by their own mouse, but also by the scrolling
127 //of this player
128 playerTransformers[i].setVelocity(new Location2D(
129 v.x-velocity.x, v.y-velocity.y));
130 }
131 else
132 {
133 //this player never moves. Instead, the other
134 //players and background scroll by.
135 tracker.setVelocity(new Location2D(
136 velocity.x, velocity.y));
137 }
138 }
139
140 }
141
142 /**
143 * sets up the game for 2 or more players
144 * @param argv not used.
145 */
146 public static void main(String[] argv)
147 {
148 MultiView multiView=new MultiView();
149 multiView.runAsApplication();
150 }
151 }
|