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

Revision 2412, 3.8 KB checked in by teeevasa, 8 years ago (diff)

some final changes, such as removing unnecessary files etc.

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