Author: darkantoine
(2010/03/20 13:33) About 2 years ago
6
7
namespace RPG
8
{
9
class Character
public class Character: GameObject
10
11
public String Name;
12
public Vector2 Position;
public int[] Coord;
13
14
public DisplayObject DO;
public string Direction;
15
public int step;
16
public Character(DisplayObject D)
17
public Character(DisplayObject DO, Display D):base(DO,D)
18
Name = "MyCharacter";
19
Position = new Vector2(0, 0);
20
Coord = new int[2];
Texture = "perso";
21
Coord[0] = Coord[1] = 0;
22
this.DO=DO;
23
this.DO.Position.X = 0;
24
this.DO.Position.Y = 0;
25
DO = D;
26
if (DO.SpriteList["Default"].Width >= Globals.GAME_WIDTH/Globals.NB_CASES_X)
}
27
28
DO.scale = ( float) Globals.GAME_WIDTH/Globals.NB_CASES_X / (float)DO.SpriteList["Default"].Width;
29
30
step = 0;
31
Direction = "None";
32
33
34
35
36
public void move(String S)
37
38
if(Direction.Equals("None"))
39
40
Direction = S;
41
42
43
44
45
public new void update()
46
47
if (Direction.Equals("None"))
48
49
return;
50
51
if (step < 50)
52
53
54
if (Direction.Equals("Left"))
55
56
this.DO.Shift.X += 7.0f;
57
58
59
if (Direction.Equals("Right"))
60
61
this.DO.Shift.X -= 7.0f;
62
63
64
65
if (Direction.Equals("Up"))
66
67
this.DO.Shift.Y += 7.0f;
68
69
70
71
if (Direction.Equals("Down"))
72
73
this.DO.Shift.Y -= 7.0f;
74
75
step++;
76
77
78
else
79
80
DO.Shift = Vector2.Zero;
81
82
83
84
Coord[0] = Coord[0] - 1 % Globals.NB_CASES_X;
85
this.DO.Position.X -= (float)Globals.GAME_WIDTH / Globals.NB_CASES_X;
86
87
88
89
90
Coord[0] = Coord[0] + 1 % Globals.NB_CASES_X;
91
this.DO.Position.X += (float)Globals.GAME_WIDTH / Globals.NB_CASES_X;
92
93
94
95
96
97
Coord[1] = Coord[1] - 1 % Globals.NB_CASES_Y;
98
this.DO.Position.Y -= (float)Globals.GAME_HEIGHT / Globals.NB_CASES_Y;
99
100
101
102
103
104
Coord[1] = Coord[1] + 1 % Globals.NB_CASES_Y;
105
this.DO.Position.Y += (float)Globals.GAME_HEIGHT / Globals.NB_CASES_Y;
106
107
108
109
110
111
112
113
114
115
116
<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="background.jpg">
<Name>background</Name>
<Importer>TextureImporter</Importer>
<Compile Include="Basketball.png">
<Name>Basketball</Name>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
class Display
public class Display
public Rectangle viewportRect;
...
public GraphicsDeviceManager graphics;
List<DisplayObject> Objects;
Texture2D background;
public Display(Game1 G)
game = G;
throw new NullReferenceException();
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 800;
background = null;
public void Initialize()
Objects = new List<DisplayObject>(10);
//Create a Rectangle that represents the full
//drawable area of the game screen.
viewportRect = new Rectangle(0, 0,
public bool removeDisplayObject(DisplayObject DO)
return Objects.Remove(DO);
public void setBG(string BG)
if (background != null)
background.Dispose();
background = game.Content.Load<Texture2D>(BG);
public void Draw()
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
//spriteBatch.Draw(backgroundTexture, viewportRect, Color.White);
spriteBatch.Draw(background, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
foreach (DisplayObject GO in Objects)
null,
Color.White,
GO.Rotation,
Vector2.Zero, 1.0f,
GO.Shift, GO.scale,
SpriteEffects.None, 0);
120
spriteBatch.Draw(TextureToDraw,
121
GO.Position,
122
123
null ,
124
125
126
127
128
spriteBatch.DrawString(game.Font1, ((DisplayText)GO).Text, GO.Position, ((DisplayText)GO).TextColor,
129
GO.Rotation, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
130
GO.Rotation, GO.Shift, 1.0f, SpriteEffects.None, 0.5f);
131
132
133
class DisplayObject
public class DisplayObject
public Dictionary<String, Texture2D> SpriteList;
protected String SpriteToUse;
public float Rotation;
public Vector2 Center;
public Vector2 Shift;
public float scale;
public DisplayObject()
Rotation = 0.0f;
Position = Vector2.Zero;
SpriteList = new Dictionary<string, Texture2D>();
Center = Vector2.Zero;
Shift = Vector2.Zero;
scale = 1;
public DisplayObject(Texture2D loadedTexture)
SpriteList["Default"]= loadedTexture;
SpriteToUse = "Default";
Center = new Vector2( loadedTexture.Width / 2, loadedTexture.Height / 2);
public void setShift(Vector2 newShift)
Shift= newShift;
public int addSprite(Texture2D NewSprite, String SpriteName)
if (NewSprite != null && SpriteName != null)
Font = font;
135
TextColor = Color.Lime;
136
SpriteList = null;
137
Center = new Vector2 ( Font.MeasureString(S).X/2 , Font.MeasureString(S).Y /2);
138
Shift = new Vector2 ( Font.MeasureString(S).X/2 , Font.MeasureString(S).Y /2);
139
140
141
SpriteList["Default"] = loadedTexture;
150
142
151
143
152
144
Center = new Vector2(Math.Max(loadedTexture.Width / 2, Font.MeasureString(S).X), Math.Max(Font.MeasureString(S).Y, loadedTexture.Height / 2));
153
Shift = new Vector2(Math.Max(loadedTexture.Width / 2, Font.MeasureString(S).X), Math.Max(Font.MeasureString(S).Y, loadedTexture.Height / 2));
145
154
146
155
147
156
GameStates GameState;
string output;
Random rand;
Carte MaCarte;
Character MyCharacter;
GameMotor GM;
public Game1()
/*
//DisplayObject balle2 = new DisplayObject(Content.Load<Texture2D>("e"));
DisplayObject perso= new DisplayObject(Content.Load<Texture2D>("perso"));
DisplayObject perso= new DisplayObject(Content.Load<Texture2D>("Basketball"));
DisplayObject balle2 = new DisplayText("Hello World", Font1);
MyCharacter= new Character(perso, display);
MyCharacter.setVisible(true);
MaCarte = new Carte("background", display );
MaCarte.Cases[0, 0].wall[(int)Directions.RIGHT] = true;
MaCarte.setActive();
GM = new GameMotor(this, MyCharacter, MaCarte);
balle2.Position = new Vector2(display.viewportRect.Width/3, display.viewportRect.Height/3);
balle = new GameObject(balle2, display);
balle.setVisible(true);
Infos = new DisplayText("Init", Font1);
Vector2 FontPos = new Vector2(50,
display.graphics.GraphicsDevice.Viewport.Height - 100);
Infos.Position = FontPos;
117
display.addDisplayObject(Infos);
118
119
*/
base.LoadContent();
134
// Needed for keyboard events
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
TotalElapsedTime += elapsed;
// KeyboardState ks= Keyboard.GetState();
KeyboardState keyboardState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
157
158
159
GM.update(gameTime);
160
161
Infos.Text = MyCharacter.step.ToString();
162
163
// Normal game loop
164
if (GameState == GameStates.Normal)
165
166
183
balle.acceleration = new Vector2(0.0f, 0.0f);
206
184
207
185
208
209
186
210
187
211
188
double ANorm = Math.Sqrt((float)Math.Pow(mouseState.X - balle.DO.Position.X , 2)+(float)Math.Pow(mouseState.Y - balle.DO.Position.Y, 2) );
212
190
double newAy = (mouseState.Y - balle.DO.Position.Y) / Math.Pow(ANorm/50, 5);
214
191
balle.acceleration = new Vector2((float)newAx, (float)newAy);
215
192
216
217
193
balle.update();
218
194
219
195
220
225
250
226
251
227
252
253
228
254
229
255
230
256
3
using System.Linq;
4
using System.Text;
5
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
public class GameMotor
Character Ch;
Carte C;
GameTime LastUpdate;
Game game;
float time;
public GameMotor (GameTime G, Character newCh, Carte newC)
public GameMotor (Game G, Character newCh, Carte newC)
LastUpdate= G;
Ch = newCh;
newC = C;
C = newC;
time = 0;
void update()
public void update(GameTime gametime)
if (KeyboardState.IsKeyDown(Keys.Right) && C.Case.GO != null){
float elapsed = (float)gametime.ElapsedGameTime.TotalSeconds;
Ch.Move("right");
time += elapsed;
};
if (KeyboardState.IsKeyDown(Keys.Left)){
Ch.Move("left");
if (time > 0.1f)
if (keyboardState.IsKeyDown(Keys.Right) && Ch.Coord[0] < Globals.NB_CASES_X - 1)
if(!C.Cases[Ch.Coord[0],Ch.Coord[1]].wall[(int)Directions.RIGHT])
Ch.move("Right");
if (KeyboardState.IsKeyDown(Keys.Up)){
if (keyboardState.IsKeyDown(Keys.Left) && Ch.Coord[0] > 0)
Ch.Move("up");
if (!C.Cases[Ch.Coord[0], Ch.Coord[1]].wall[(int)Directions.LEFT])
Ch.move("Left");
if (KeyboardState.IsKeyDown(Keys.Down)){
if (keyboardState.IsKeyDown(Keys.Up) && Ch.Coord[1] > 0)
Ch.Move("down");
if (!C.Cases[Ch.Coord[0], Ch.Coord[1]].wall[(int)Directions.UP])
Ch.move("Up");
if (keyboardState.IsKeyDown(Keys.Down) && Ch.Coord[1] < Globals.NB_CASES_Y - 1)
if (!C.Cases[Ch.Coord[0], Ch.Coord[1]].wall[(int)Directions.DOWN])
Ch.move("Down");
Ch.update();
class GameObject
public class GameObject
static Vector2 zero2= new Vector2(0,0);
Dimension = new Vector2(DO.getSprite().Width, DO.getSprite().Height);
}*/
Dimension = DO.Center * 2;
Dimension = DO.Shift * 2;
public void setVisible(bool V)
class Globals
public enum Directions { LEFT, RIGHT, UP, DOWN };
public class Globals
public static int NB_CASES_X = 16;
public static int NB_CASES_Y = 12;
public static int GAME_WIDTH = 800;
public static int GAME_HEIGHT = 600;
<Compile Include="Character.cs" />
<Compile Include="Display.cs" />
<Compile Include="DisplayObject.cs" />
<Compile Include="GameMotor.cs" />
<Compile Include="GameObject.cs" />
<Compile Include="Globals.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
class Zone
public class Zone
public string Name;
Carte[] Cartes;
public Zone()
Name= null;
Name = null;
Cartes= null;
Cartes = null;
public int addCarte (Carte C)
/*Ajoute une carte dans la zone.
class Carte
public class Carte
public string Background;
Case[,] Cases;
public Case[,] Cases;
Display Disp;
public Carte()
public Carte(String Back)
public Carte(String Back, Display D)
Disp= D;
Background = Back;
Cases = new Case[Globals.NB_CASES_X,Globals.NB_CASES_Y];
for(int i=0; i<Globals.NB_CASES_X; i++)
public void setActive()
Disp.setBG(Background);
public int put_Object(GameObject Obj, int X, int Y)
if (Cases[X,Y] != null)
class Case
public class Case
public string Texture;
public GameObject GO;
GO=null;
Texture=null;
wall = new bool[4];
for (int i = 0; i < 4; i++)
wall[i] = false;
public Case(string T)
GO = null;
Texture=T;
for(int i=0; i<4; i++)
wall[i]=false;
bool[] wall; //Tableau des murs, wall[i]=true s'il y'a un mur sur l'arrĂȘte.
public bool[] wall; //Tableau des murs, wall[i]=true s'il y'a un mur sur l'arrĂȘte.
//wall de taille 4, dans l'ordre : haut, droite, bas, gauche.
148
149
{{class Characterpublic class Character: GameObject{{public Vector2 Position;public int[] Coord;public DisplayObject DO;public string Direction;public int step;public Character(DisplayObject DO, Display D):base(DO,D){{Position = new Vector2(0, 0);Coord = new int[2];Texture = "perso";Coord[0] = Coord[1] = 0;this.DO=DO;this.DO.Position.X = 0;this.DO.Position.Y = 0;DO = D;if (DO.SpriteList["Default"].Width >= Globals.GAME_WIDTH/Globals.NB_CASES_X)}{DO.scale = ( float) Globals.GAME_WIDTH/Globals.NB_CASES_X / (float)DO.SpriteList["Default"].Width;}step = 0;Direction = "None";{if(Direction.Equals("None")){{if (Direction.Equals("None")){{if (Direction.Equals("Left")){if (Direction.Equals("Right")){if (Direction.Equals("Up")){if (Direction.Equals("Down")){{if (Direction.Equals("Left")){if (Direction.Equals("Right")){if (Direction.Equals("Up")){if (Direction.Equals("Down")){{{public class Display{{{{{{{{{{{{if (background != null)spriteBatch.Draw(background, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);{{Vector2.Zero, 1.0f,GO.Shift, GO.scale,{{null,null ,Vector2.Zero, 1.0f,GO.Shift, GO.scale,GO.Rotation, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);GO.Rotation, GO.Shift, 1.0f, SpriteEffects.None, 0.5f);{{public class DisplayObject{{public Vector2 Center;public Vector2 Shift;Center = Vector2.Zero;Shift = Vector2.Zero;scale = 1;{{Center = new Vector2( loadedTexture.Width / 2, loadedTexture.Height / 2);Shift = Vector2.Zero;{{{Center = new Vector2 ( Font.MeasureString(S).X/2 , Font.MeasureString(S).Y /2);Shift = new Vector2 ( Font.MeasureString(S).X/2 , Font.MeasureString(S).Y /2);Center = new Vector2(Math.Max(loadedTexture.Width / 2, Font.MeasureString(S).X), Math.Max(Font.MeasureString(S).Y, loadedTexture.Height / 2));Shift = new Vector2(Math.Max(loadedTexture.Width / 2, Font.MeasureString(S).X), Math.Max(Font.MeasureString(S).Y, loadedTexture.Height / 2));{{//DisplayObject balle2 = new DisplayObject(Content.Load<Texture2D>("e"));//DisplayObject balle2 = new DisplayObject(Content.Load<Texture2D>("e"));DisplayObject perso= new DisplayObject(Content.Load<Texture2D>("perso"));DisplayObject perso= new DisplayObject(Content.Load<Texture2D>("Basketball"));DisplayObject balle2 = new DisplayText("Hello World", Font1);MyCharacter= new Character(perso, display);MyCharacter.setVisible(true);MaCarte = new Carte("background", display );MaCarte.Cases[0, 0].wall[(int)Directions.RIGHT] = true;MaCarte.setActive();GM = new GameMotor(this, MyCharacter, MaCarte);/*DisplayObject balle2 = new DisplayText("Hello World", Font1);Infos = new DisplayText("Init", Font1);Infos = new DisplayText("Init", Font1);Infos = new DisplayText("Init", Font1);Vector2 FontPos = new Vector2(50,display.graphics.GraphicsDevice.Viewport.Height - 100);Infos.Position = FontPos;display.addDisplayObject(Infos);float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;TotalElapsedTime += elapsed;{{{{{{Carte C;GameTime LastUpdate;Game game;float time;public GameMotor (GameTime G, Character newCh, Carte newC){{LastUpdate= G;game = G;Ch = newCh;newC = C;C = newC;time = 0;void update()public void update(GameTime gametime){{if (KeyboardState.IsKeyDown(Keys.Right) && C.Case.GO != null){float elapsed = (float)gametime.ElapsedGameTime.TotalSeconds;Ch.Move("right");time += elapsed;};if (KeyboardState.IsKeyDown(Keys.Left)){KeyboardState keyboardState = Keyboard.GetState();Ch.Move("left");if (time > 0.1f){time = 0;if (keyboardState.IsKeyDown(Keys.Right) && Ch.Coord[0] < Globals.NB_CASES_X - 1){if(!C.Cases[Ch.Coord[0],Ch.Coord[1]].wall[(int)Directions.RIGHT])Ch.move("Right");if (KeyboardState.IsKeyDown(Keys.Up)){if (keyboardState.IsKeyDown(Keys.Left) && Ch.Coord[0] > 0)Ch.Move("up");{if (!C.Cases[Ch.Coord[0], Ch.Coord[1]].wall[(int)Directions.LEFT])Ch.move("Left");if (KeyboardState.IsKeyDown(Keys.Down)){if (keyboardState.IsKeyDown(Keys.Up) && Ch.Coord[1] > 0)Ch.Move("down");{if (!C.Cases[Ch.Coord[0], Ch.Coord[1]].wall[(int)Directions.UP])Ch.move("Up");{Ch.move("Down");{{{public class GameObject{{{{Dimension = DO.Center * 2;Dimension = DO.Shift * 2;{{class Globalspublic enum Directions { LEFT, RIGHT, UP, DOWN };public class Globals{{{{public class Zone{{{{Name = null;Cartes = null;}public class Carte{{public Case[,] Cases;Display Disp;{{public Carte(String Back, Display D){{{{{public class Case{{{{{{{{bool[] wall; //Tableau des murs, wall[i]=true s'il y'a un mur sur l'arrĂȘte.public bool[] wall; //Tableau des murs, wall[i]=true s'il y'a un mur sur l'arrĂȘte.