@@ -44,20 +44,14 @@ def __init__(self, controltype, speed=5):
44
44
}
45
45
46
46
def set_control (self , name , key ):
47
- if not type (name ) == Controls :
47
+ if not isinstance (name , Controls ) :
48
48
raise TypeError ("Name must be a Controls type" )
49
- try :
50
- self .controles [name ] = key
51
- except KeyError :
52
- raise ValueError ("Control unknown : " + name )
49
+ self .controles [name ] = key
53
50
54
51
def get_control (self , name ):
55
- if not type (name ) == Controls :
52
+ if not isinstance (name , Controls ) :
56
53
raise TypeError ("Name must be a Controls type" )
57
- try :
58
- return self .controles [name ]
59
- except KeyError :
60
- raise ValueError ("Control unknown : " + name )
54
+ return self .controles [name ]
61
55
62
56
def get_speed (self ):
63
57
return self .speed
@@ -70,27 +64,26 @@ def set_entity(self, entity):
70
64
71
65
def update (self ):
72
66
if self .controltype == ControlType .CLICKFOLLOW and self .goto != (- 1 , - 1 ):
73
- if not self .entity .has_component (PositionComponent ):
74
- raise NoObjectError ("Entity must have PositionComponent." )
75
- position = self .entity .get_component (PositionComponent )
76
- if position .x - 10 < self .goto [0 ] < position .x + 10 and position .y - 10 < self .goto [1 ] < position .y + 10 :
77
- self .goto = (- 1 , - 1 )
78
- else :
79
- pos = [position .x , position .y ]
80
- if position .x - 10 > self .goto [0 ]:
81
- pos [0 ] = position .x - self .speed
82
- elif position .x + 10 < self .goto [0 ]:
83
- pos [0 ] = position .x + self .speed
84
- if position .y - 10 > self .goto [1 ]:
85
- pos [1 ] = position .y - self .speed
86
- elif position .y + 10 < self .goto [1 ]:
87
- pos [1 ] = position .y + self .speed
88
-
89
- cango = True
90
- if self .entity .has_component (PhysicsComponent ):
91
- cango = self .entity .get_component (PhysicsComponent ).can_go (pos )
92
- if cango :
93
- self .entity .get_component (PositionComponent ).set_position (pos )
67
+ if self .entity .has_component (PositionComponent ):
68
+ position = self .entity .get_component (PositionComponent )
69
+ if position .x - 10 < self .goto [0 ] < position .x + 10 and position .y - 10 < self .goto [1 ] < position .y + 10 :
70
+ self .goto = (- 1 , - 1 )
71
+ else :
72
+ pos = [position .x , position .y ]
73
+ if position .x - 10 > self .goto [0 ]:
74
+ pos [0 ] = position .x - self .speed
75
+ elif position .x + 10 < self .goto [0 ]:
76
+ pos [0 ] = position .x + self .speed
77
+ if position .y - 10 > self .goto [1 ]:
78
+ pos [1 ] = position .y - self .speed
79
+ elif position .y + 10 < self .goto [1 ]:
80
+ pos [1 ] = position .y + self .speed
81
+
82
+ cango = True
83
+ if self .entity .has_component (PhysicsComponent ):
84
+ cango = self .entity .get_component (PhysicsComponent ).can_go (pos )
85
+ if cango :
86
+ self .entity .get_component (PositionComponent ).set_position (pos )
94
87
95
88
def mousepress (self , evt ):
96
89
if self .controltype == ControlType .CLICKFOLLOW and evt .button == MouseButton .LEFTCLICK .value :
@@ -104,51 +97,50 @@ def keyup(self, evt):
104
97
105
98
def keypress (self , evt ):
106
99
eventkey = evt .key
107
- if not self .entity .has_component (PositionComponent ):
108
- raise NoObjectError ("Entity must have PositionComponent." )
109
- position = self .entity .get_component (PositionComponent )
110
- pos = position .get_position ()
111
- cango = True
112
- if eventkey == self .controles [Controls .LEFT ]:
113
- if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .CLASSICJUMP \
114
- or self .controltype == ControlType .DOUBLEJUMP or self .controltype == ControlType .LEFTRIGHT :
115
- pos = [position .x - self .speed , position .y ]
116
- if self .entity .has_component (PhysicsComponent ):
117
- cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .LEFTCONTROL )
118
- if eventkey == self .controles [Controls .RIGHT ]:
119
- if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .CLASSICJUMP \
120
- or self .controltype == ControlType .DOUBLEJUMP or self .controltype == ControlType .LEFTRIGHT :
121
- pos = [position .x + self .speed , position .y ]
122
- if self .entity .has_component (PhysicsComponent ):
123
- cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .RIGHTCONTROL )
124
- if eventkey == self .controles [Controls .UPJUMP ]:
125
- if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .UPDOWN :
126
- pos = [position .x , position .y - self .speed ]
127
- if self .entity .has_component (PhysicsComponent ):
128
- cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .UPCONTROL )
129
- elif self .controltype == ControlType .CLASSICJUMP :
130
- if not self .entity .has_component (PhysicsComponent ):
131
- raise NoObjectError ("Entity must have PhysicsComponent" )
132
- phys = self .entity .get_component (PhysicsComponent )
133
- if phys .grounded and not self .jumping :
134
- phys .grounded = False
135
- self .jumping = True
136
- phys .gravity_force = - phys .max_gravity_force
137
- elif self .controltype == ControlType .DOUBLEJUMP :
138
- if not self .entity .has_component (PhysicsComponent ):
139
- raise NoObjectError ("Entity must have PhysicsComponent" )
140
- phys = self .entity .get_component (PhysicsComponent )
141
- if (phys .grounded or phys .doublejump ) and not self .jumping :
142
- if not phys .grounded :
143
- phys .doublejump = False
144
- phys .grounded = False
145
- self .jumping = True
146
- phys .gravity_force = - phys .max_gravity_force
147
- if eventkey == self .controles [Controls .DOWN ]:
148
- if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .UPDOWN :
149
- pos = [position .x , position .y + self .speed ]
150
- if self .entity .has_component (PhysicsComponent ):
151
- cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .DOWNCONTROL )
152
-
153
- if cango :
154
- self .entity .get_component (PositionComponent ).set_position (pos )
100
+ if self .entity .has_component (PositionComponent ):
101
+ position = self .entity .get_component (PositionComponent )
102
+ pos = position .get_position ()
103
+ cango = True
104
+ if eventkey == self .controles [Controls .LEFT ]:
105
+ if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .CLASSICJUMP \
106
+ or self .controltype == ControlType .DOUBLEJUMP or self .controltype == ControlType .LEFTRIGHT :
107
+ pos = [position .x - self .speed , position .y ]
108
+ if self .entity .has_component (PhysicsComponent ):
109
+ cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .LEFTCONTROL )
110
+ if eventkey == self .controles [Controls .RIGHT ]:
111
+ if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .CLASSICJUMP \
112
+ or self .controltype == ControlType .DOUBLEJUMP or self .controltype == ControlType .LEFTRIGHT :
113
+ pos = [position .x + self .speed , position .y ]
114
+ if self .entity .has_component (PhysicsComponent ):
115
+ cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .RIGHTCONTROL )
116
+ if eventkey == self .controles [Controls .UPJUMP ]:
117
+ if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .UPDOWN :
118
+ pos = [position .x , position .y - self .speed ]
119
+ if self .entity .has_component (PhysicsComponent ):
120
+ cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .UPCONTROL )
121
+ elif self .controltype == ControlType .CLASSICJUMP :
122
+ if not self .entity .has_component (PhysicsComponent ):
123
+ raise NoObjectError ("Entity must have PhysicsComponent" )
124
+ phys = self .entity .get_component (PhysicsComponent )
125
+ if phys .grounded and not self .jumping :
126
+ phys .grounded = False
127
+ self .jumping = True
128
+ phys .gravity_force = - phys .max_gravity_force
129
+ elif self .controltype == ControlType .DOUBLEJUMP :
130
+ if not self .entity .has_component (PhysicsComponent ):
131
+ raise NoObjectError ("Entity must have PhysicsComponent" )
132
+ phys = self .entity .get_component (PhysicsComponent )
133
+ if (phys .grounded or phys .doublejump ) and not self .jumping :
134
+ if not phys .grounded :
135
+ phys .doublejump = False
136
+ phys .grounded = False
137
+ self .jumping = True
138
+ phys .gravity_force = - phys .max_gravity_force
139
+ if eventkey == self .controles [Controls .DOWN ]:
140
+ if self .controltype == ControlType .FOURDIRECTION or self .controltype == ControlType .UPDOWN :
141
+ pos = [position .x , position .y + self .speed ]
142
+ if self .entity .has_component (PhysicsComponent ):
143
+ cango = self .entity .get_component (PhysicsComponent ).can_go (pos , CollisionCauses .DOWNCONTROL )
144
+
145
+ if cango :
146
+ self .entity .get_component (PositionComponent ).set_position (pos )
0 commit comments