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

Revision 2343, 2.3 KB checked in by teeevasa, 9 years ago (diff)

weapon fire only destroys aliens

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
50        // As of now weapons are set to have infinite ammo, but that might change
51        if (bullet != null)
52        { 
53           
54        }
55    }
56
57    void bulletReachedTarget(PhysicsObject bullet, PhysicsObject target)
58    {
59        bullet.Destroy();
60
61        if (target.Tag == "MeleeAlien")
62        {
63            target.Destroy();
64        }
65    }
66
67    private void playWalkingSound(Timer sender)
68    {
69        this.walking.Play();
70    }
71
72    private void moveRight(int speed)
73    {
74        walker.Start();
75        this.Walk(speed);
76        //this.Image = Image.Mirror(portrait); // Doesn't work at all!
77    }
78
79    private void moveLeft(int speed)
80    {
81        walker.Start();
82        this.Walk(-speed);
83        this.Image = Image.Mirror(portrait);
84    }
85
86    public void jump(int jumpSpeed)
87    {
88        this.Jump(jumpSpeed);
89    }
90
91}
Note: See TracBrowser for help on using the repository browser.