source: 2011/26/JaakkoL/Rogue Agent 2372/Rogue Agent 2372/Rogue Agent 2372/Player.cs @ 2342

Revision 2342, 2.2 KB checked in by jaollipa, 8 years ago (diff)
Line 
1using System;
2using Jypeli;
3using Jypeli.Assets;
4using Jypeli.Controls;
5using Jypeli.Effects;
6using Jypeli.Widgets;
7
8public class Player : GenCharacter
9{
10    const int speed = 200;
11    const int jumpSpeed = 500;
12    const int mass = 10;
13    Image portrait = Game.LoadImage("Images/character");
14    SoundEffect walking = Game.LoadSoundEffect("Sounds/walkingsound");
15
16    Timer walker;
17
18        public Player(double width, double height, Shape shape)
19        : base(width, height, shape)
20        {
21        hitPoints = 100;
22        AddedToGame += addControls;
23        this.Image = portrait;
24        this.CollisionIgnoreGroup = 1;
25
26        walker = new Timer();
27        walker.Interval = 0.7;
28        walker.Trigger += playWalkingSound;
29
30        Pistol pistol = new Pistol(20.0, 10.0);
31        pistol.X = 5;
32        pistol.Y = 10;
33        this.Weapon = pistol;
34
35        pistol.ProjectileCollision += bulletReachedTarget;
36        }
37
38    private void addControls()
39    {
40        Game.Keyboard.Listen(Key.Right, ButtonState.Down, moveRight, "Moves right", speed);
41        Game.Keyboard.Listen(Key.Left, ButtonState.Down, moveLeft, "Moves left", speed);
42        Game.Keyboard.Listen(Key.Up, ButtonState.Down, jump, "Jumps", jumpSpeed);
43        Game.Keyboard.Listen(Key.Space, ButtonState.Pressed, shoot, "Shoots");
44    }
45
46    private void shoot()
47    {
48        PhysicsObject bullet = this.Weapon.Shoot();
49        //bullet.Hit(new Vector(1000, 0));
50
51        // As of now weapons are set to have infinite ammo, but that might change
52        if (bullet != null)
53        { 
54           
55        }
56    }
57
58    void bulletReachedTarget(PhysicsObject bullet, PhysicsObject target)
59    { 
60   
61    }
62
63    private void playWalkingSound(Timer sender)
64    {
65        this.walking.Play();
66    }
67
68    private void moveRight(int speed)
69    {
70        walker.Start();
71        this.Walk(speed);
72        //this.Image = Image.Mirror(portrait); // Doesn't work at all!
73    }
74
75    private void moveLeft(int speed)
76    {
77        walker.Start();
78        this.Walk(-speed);
79        this.Image = Image.Mirror(portrait);
80    }
81
82    public void jump(int jumpSpeed)
83    {
84        this.Jump(jumpSpeed);
85    }
86
87}
Note: See TracBrowser for help on using the repository browser.