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

Revision 2368, 3.5 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 = 200;
12    const int mass = 10;
13    Image portrait = Game.LoadImage("Images/character");
14    SoundEffect walking = Game.LoadSoundEffect("Sounds/walkingsound");
15
16    string currentWeapon;
17    Pistol pistol;
18    Rifle rifle;
19
20    Timer walker;
21
22        public Player(double width, double height, Shape shape, int HP = 100)
23        : base(width, height, shape, HP)
24        {
25        hitPoints = 100;
26        AddedToGame += addControls;
27        this.Image = portrait;
28        this.CollisionIgnoreGroup = 1;
29        this.Mass = mass;
30
31        // Creating the timer system that is *supposed* to enable a nice walking sound effect
32        walker = new Timer();
33        walker.Interval = 0.7;
34        walker.Trigger += playWalkingSound;
35
36        // Creating the weapon; for now only a pistol
37        pistol = new Pistol(20.0, 10.0);
38        pistol.X = 18;
39        pistol.Y = 11;
40        this.Weapon = pistol;
41        pistol.ProjectileCollision += bulletReachedTarget;
42
43        rifle = new Rifle(59.0, 11.0);
44        rifle.X = 14;
45        rifle.Y = 8;
46        rifle.ProjectileCollision += bulletReachedTarget;
47        // Collision handlers for enemies
48        //AddCollisionHandler();
49        }
50
51    private void addControls()
52    {
53        Game.Keyboard.Listen(Key.Right, ButtonState.Down, moveRight, "Moves right", speed);
54        Game.Keyboard.Listen(Key.Left, ButtonState.Down, moveLeft, "Moves left", speed);
55        Game.Keyboard.Listen(Key.Up, ButtonState.Down, jump, "Jumps", jumpSpeed);
56        Game.Keyboard.Listen(Key.Space, ButtonState.Down, shoot, "Shoots");
57        Game.Keyboard.Listen(Key.D1, ButtonState.Pressed, changeWeapon, "Changes weapon to the pistol", pistol.name());
58        Game.Keyboard.Listen(Key.D2, ButtonState.Pressed, changeWeapon, "Changes weapon to the rifle", rifle.name());
59    }
60
61    private void changeWeapon(string weaponName)
62    {
63        if (weaponName == "Pistol")
64        {
65            this.Weapon = pistol;
66        }
67        else if (weaponName == "Rifle")
68        {
69            this.Weapon = rifle;
70        }
71        else
72        {
73            throw new Exception("Invalid weapon name: '" + weaponName + "'");
74        }
75
76        // We only get here if the weaponName is valid
77        currentWeapon = weaponName;
78    }
79
80    // Because all the weapons have infinite ammo, we don't have to check for null
81    private void shoot()
82    {
83        PhysicsObject bullet = this.Weapon.Shoot();
84    }
85
86    private void bulletReachedTarget(PhysicsObject bullet, PhysicsObject target)
87    {
88        bullet.Destroy();
89
90        if (target.Tag == "MeleeAlien")
91        {
92            MeleeAlien alien = (MeleeAlien)target;
93            //alien.reduceHitPointsBy(this.Weapon.firePower());
94            alien.reduceHitPointsBy(10);
95        }
96    }
97
98    private void playWalkingSound(Timer sender)
99    {
100        this.walking.Play();
101    }
102
103    private void moveRight(int speed)
104    {
105        walker.Start();
106        this.Walk(speed);
107        //this.Image = Image.Mirror(portrait); // Doesn't work at all!
108    }
109
110    private void moveLeft(int speed)
111    {
112        walker.Start();
113        this.Walk(-speed);
114        this.Image = Image.Mirror(portrait);
115    }
116
117    public void jump(int jumpSpeed)
118    {
119        this.Jump(jumpSpeed);
120    }
121}
Note: See TracBrowser for help on using the repository browser.